Skip to content

feat(moonpool-sim): in-flight faults and end-to-end TCP flow control - #202

Merged
PierreZ merged 2 commits into
mainfrom
claude/moonpool-network-sim-xrsu9f
Sep 5, 2026
Merged

PierreZ merged 2 commits into
mainfrom
claude/moonpool-network-sim-xrsu9f

Conversation

@PierreZ

@PierreZ PierreZ commented Sep 5, 2026 •

Copy link
Copy Markdown
Owner

Two related changes to the simulated network, one commit each.

1. Faults reach traffic already in flight

Before. A chunk that left the send queue travelled inside its scheduled DataDelivery { data } event. Once the write latency was sampled, nothing could touch it: handle_data_delivery checked the receiver's close flags and the black hole, never a partition. So a response written at t=0 with 100ms of latency crossed a partition that started at t=50ms, and so did an in-flight FIN. Only the chunk still sitting in the queue stalled.

After. The bytes live in the engine, in a per-connection in_flight queue owned by the sender (InFlight { seq, deliver_at, payload: Data | Fin }). The scheduler event is a payload-free wake-up, NetworkEvent::Delivery { connection_id, seq }. A delivery lands only from the head of the flight, only once deliver_at has passed, and never while the direction is held, so faults are judged at landing:

  • every partition insertion (pair, send-wide, receive-wide, random strategies) freezes the flight of each direction it cuts;
  • every heal (deadline, restore_partition, recovery mode) re-times each frozen item by exactly the time the direction spent cut and re-schedules it, so a cut of D delays every byte it caught by D, the stream stays FIFO, and no randomness is drawn;
  • a black hole opened mid-flight swallows the chunk at landing; an abort discards the flight with the queue;
  • the FIN is an in-flight item behind the data, so it can neither overtake the last bytes nor cross a cut ahead of them.

Semantics stay the documented FDB-style one: a partition stalls the stream and never punches a hole in it.

2. End-to-end TCP flow control

Before. The 64 KiB send buffer only bounded the local queue. ProcessSendBuffer released capacity the moment a chunk left the queue, so a writer could run forever as long as the engine kept moving bytes into the scheduler and then into an unbounded receive buffer. A reader that stopped reading never backed its writer up.

After. Each direction has one SendWindow (NetworkConfiguration::tcp_send_window_bytes, SimulationBuilder::tcp_send_window_bytes, default 64 KiB as before):

outstanding = queued + in_flight + unread_at_peer (+ vanished)
outstanding <= capacity

Credit is acquired when poll_write accepts bytes and released only when the peer's poll_read hands them to its application, byte for byte (a 137-byte partial read returns 137 bytes). Moving bytes between the queue, the wire and the receive buffer releases nothing. A partition returns no credit; a black hole never returns it, so a black-holed direction accepts a window's worth (the small RPC that must time out at the application still goes in) and then blocks; an abort discards everything and wakes a parked writer to the error; a peer that closes with bytes unread frees them. SendWindow::acquire/release assert instead of saturating, so an over-acquire or double release panics.

Backpressure is entropy-neutral: a Poll::Pending on a full window, the wake after a read, and every move between the three places consume no simulation randomness. The campaign test runs under check_determinism.

Tests

  • tests/network/in_flight.rs (fixed 10ms latency): partition after the send with exact landing time, heal by hand, directionality, send-wide and receive-wide cuts, FIFO across the cut boundary, in-flight FIN, close under the cut, recovery mode with zero RNG draws, mid-flight black hole.
  • tests/flow_control.rs: writer parks at 64 KiB even with every byte in the receive buffer and a 4 KiB read lets exactly 4 KiB through; partial-read credit; partition returns nothing until the read; black-hole backpressure; abort and peer close wake a parked writer; FIN behind data; a scripted run under 50ms latency, small partial reads and a temporary cut asserting the accounting identity after every event; and the SimulationBuilder campaign (streamer into a slow reader, 4 KiB window, partial I/O, latency, two scripted cuts) with the determinism canary green on every seed.
  • SendWindow unit tests pin the double-release and over-acquire panics.

API changes

  • NetworkEvent::DataDelivery / FinDelivery become NetworkEvent::Delivery { connection_id, seq }; NetworkEvent is now Copy.
  • SimWorld::send_buffer_capacity/send_buffer_used/available_send_buffer become send_window_bytes/outstanding_send_bytes/available_send_bytes; queued_send_bytes, in_flight_bytes, unread_bytes, is_in_flight_held added.
  • NetworkConfiguration::tcp_send_window_bytes (+ DEFAULT_TCP_SEND_WINDOW_BYTES) and SimulationBuilder::tcp_send_window_bytes.

Book: partition section documents the four-stage byte lifecycle and the freeze/thaw rule; new Flow control section; scope chapter no longer lists TCP backpressure as unmodelled; event-trace and config appendix updated.

Validation

cargo fmt, cargo clippy --workspace --all-targets -- -D warnings, clippy -p moonpool-sim --no-default-features --all-targets -- -D warnings, cargo check --target wasm32-unknown-unknown -p moonpool-sim --no-default-features, cargo nextest run --workspace (770 passed), mdbook build book/.

Known gaps, tracked separately

🤖 Generated with Claude Code

https://claude.ai/code/session_01VXnMuFCFvF66WYwXUyXDJz

PierreZ and others added 2 commits September 5, 2026 19:29
A chunk that left the send queue used to travel *inside* its scheduled
`DataDelivery` event: once the write latency had been sampled, nothing
could touch it, and `handle_data_delivery` landed it without asking
whether a partition had gone up in the meantime. A response written at
t=0 with 100ms of latency crossed a partition that started at t=50ms,
and so did an in-flight FIN. Only the queued (not yet drained) chunk
stalled.

The bytes now live in the engine, in a per-connection `in_flight` queue
owned by the sender (`InFlight { seq, deliver_at, payload }`, payload a
data chunk or the FIN), and the scheduler event is a wake-up that names
an item (`NetworkEvent::Delivery { connection_id, seq }`) rather than
carrying it. A delivery lands only from the head of the flight, only
once its `deliver_at` has passed, and never while the direction is held,
so a stale event is a no-op and faults are judged at landing:

- every partition insertion (pair, send-wide, receive-wide, the random
  strategies) freezes the flight of each direction it cuts
  (`in_flight_held_since`); nothing is put on the wire under a cut, and a
  FIN queued under one is frozen from that instant;
- every heal (deadline, `restore_partition`, recovery mode) thaws the
  flight by re-timing each item by exactly the time the direction spent
  cut and re-scheduling its event, so a cut of D delays every byte it
  caught by D, the stream stays FIFO (the floor for the next send moves
  with it), and no randomness is drawn — the latencies already sampled
  are reused;
- a black hole opened while a chunk is in flight swallows it at landing;
- an abort discards the flight with the queue.

The FIN is an in-flight item behind the data, so it can neither overtake
the last bytes nor cross a partition ahead of them. `NetworkEvent` no
longer carries payload and is `Copy`.

Tests (`tests/network/in_flight.rs`, fixed 10ms latency): partition after
the send with exact landing time, heal by hand with the remaining flight
time, directionality, send-wide and receive-wide cuts, FIFO across the
cut boundary, an in-flight FIN, a close issued under the cut, recovery
mode healing with zero RNG draws, and a mid-flight black hole. The book's
partition section documents the four-stage byte lifecycle and the
freeze/thaw rule.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VXnMuFCFvF66WYwXUyXDJz
…direction

The 64 KiB send buffer only bounded the *local* queue: `ProcessSendBuffer`
released its capacity the moment a chunk left the queue, so a writer could
run forever as long as the engine kept moving bytes into the scheduler and
then into an unbounded receive buffer. A reader that stopped reading never
backed its writer up, which is not TCP backpressure at all.

Each direction now has one `SendWindow` (`NetworkConfiguration::
tcp_send_window_bytes`, `SimulationBuilder::tcp_send_window_bytes`, default
64 KiB as before) with the invariant

    outstanding = queued + in_flight + unread_at_peer (+ vanished)
    outstanding <= capacity

Credit is acquired in `buffer_send`, when `poll_write` accepts the bytes,
and released only when the peer's `poll_read` hands them to its
application, byte for byte — a 137-byte partial read returns 137 bytes.
Moving bytes from the queue onto the wire or from the wire into the
receive buffer releases nothing. A write still accepts `min(len,
available)` and parks only at zero; a parked writer is woken by the peer's
read (outside the world lock, like every other waiter).

Faults: a partition returns no credit (delay is not consumption) and the
thawed bytes are released by the read that follows the heal; a black hole
never returns it, so a black-holed direction accepts a window's worth —
the small RPC that must time out at the application still goes in — and
then blocks instead of absorbing data forever; an abort discards the
queue, the flight and the unread bytes and wakes a parked writer to the
error; a peer that closes or shuts its receive side with bytes unread
frees them (the kernel acknowledged what it throws away), and bytes it
discards on arrival are released too, so no writer is ever parked on
credits nothing can return. The FIN sits behind outstanding data and
cannot overtake it.

`SendWindow::acquire` and `::release` assert rather than saturate: an
over-acquire or a double release panics with the counts, and unit tests
pin both.

Determinism: a `Poll::Pending` on a full window, the wake after a read,
and every move between the three places consume no simulation
randomness; the campaign test below runs under `check_determinism`.

Tests (`tests/flow_control.rs`): the central case (writer parks at
64 KiB even with every byte in the receive buffer; a 4 KiB read wakes it
and the next write accepts exactly 4 KiB), partial-read credit, a
partition returning nothing until the read, black-hole backpressure,
abort and peer-close waking a parked writer, FIN behind data, and a
scripted run under 50ms latency, small partial reads and a temporary cut
asserting the accounting identity after every event. The campaign — a
streamer into a slow reader over a 4 KiB window with latency, partial
writes and reads and two scripted cuts — proves the block/resume cycle
on every seed with the determinism canary green.

`SimWorld::send_buffer_capacity/used` and `available_send_buffer` become
`send_window_bytes`, `outstanding_send_bytes` and `available_send_bytes`;
`queued_send_bytes`, `in_flight_bytes` and `unread_bytes` expose the
three places for tests. The book gets a Flow control section, and the
scope chapter no longer lists TCP backpressure as unmodelled.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VXnMuFCFvF66WYwXUyXDJz
@PierreZ
PierreZ merged commit b396edc into main Sep 5, 2026
13 checks passed
@github-actions github-actions Bot mentioned this pull request Sep 5, 2026
PierreZ pushed a commit that referenced this pull request Oct 4, 2026
… paros

Checked against paros's call sites (paros#176, #202):

- paros's driver hooks (`BuggifyHooks`) live inside the provider-generic
  driver and never hold a `SimContext`, so `crash_self` could not reach
  the kill-during-sync location #202 wants. `SimContext::self_crash()`
  returns a `SelfCrash` handle (`Clone + Send + Sync + 'static`) whose
  `crash(kind, delay)` schedules the same kill; `crash_self` is now a
  shorthand for it.
- A scripted injector aiming `corrupt_bytes` at one journal record needs
  the record's bytes, e.g. `JournalAtlas::scan` on the crashed node's
  disk. `FaultContext::storage(ip)` returns a provider scoped to that
  process's disk.

paros at 29b23e3 builds unchanged (paros-sim, parosd, tests) against this
branch through a local [patch].

Part of #297, #296

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017Pv7ppXLHvimhFfSqMapGy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant