Skip to content

Architecture

github-actions[bot] edited this page Dec 2, 2026 · 1 revision

Architecture

Discrete-Event Loop (SimEngine)

Faultline runs the entire simulation on a single thread via a deterministic event pump:

  1. Drain ready continuations: If ReadyQueue is non-empty, pick the next item using SimRandom (SimSchedulingPolicy.RandomInterleaving) or index 0 (SimSchedulingPolicy.Fifo), restore SimContext for its SimHost, and invoke it.
  2. Advance virtual time: When ReadyQueue is empty, dequeue the earliest (TimestampTicks, SequenceId) from the EventQueue priority queue, advance CurrentTime, and dispatch the event.
  3. Detect deadlocks: If both queues are empty while the root scenario task is incomplete, throw SimDeadlockException with the exact reproduction Seed.

Host Affinity (AsyncLocal<SimHost>)

Each virtual host is stored in SimContext.CurrentHost (AsyncLocal<SimHost?>). Because ExecutionContext flows automatically across await boundaries, every intercepted call (DateTime.UtcNow, Random.Shared, Task.Delay) automatically resolves to the calling host's skewed SimClock and forked SimRandom without passing context parameters through application code.

Deterministic Task.WhenAll / Combinators

When completing simulated TaskCompletionSource instances, SimEngine temporarily clears SynchronizationContext.Current around TrySetResult(). This ensures internal BCL combinators like Task.WhenAll resolve synchronously on the simulation thread (never escaping to the CLR ThreadPool), while async/await continuations that captured SimSynchronizationContext are forced back through ReadyQueue for seeded interleaving.

Clone this wiki locally