Skip to content

refactor(mux-h2): route the control-frame drain re-arms through signal_pending_write - #1491

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h2-signal-pending-write
Sep 24, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h2-signal-pending-write

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Closes #1462.

Two sites in ConnectionH2::flush_pending_control_frames set the WRITABLE event bit by hand, bypassing Readiness::signal_pending_write and the two debug_assert!s that make it a guard rather than a spelling:

-   self.readiness.event.insert(Ready::WRITABLE);
+   self.readiness.signal_pending_write();

There is no behaviour difference — both touch the event bit and insert cannot clear a neighbour. The point is which guard was missing: signal_pending_write is the one place the event/interest distinction is written down and asserted, and a site that bypasses it opts out invisibly, because grep signal_pending_write does not find it. An audit of "every place we signal a pending write" silently missed these two.

Three independent readings confirm both sites mean "pending write" in the helper's sense rather than something narrower:

  1. Both sit under if self.socket.socket_wants_write() — the helper's documented precondition verbatim.
  2. Both run right after flush_zero_to_socket() returned stalled, i.e. update_readiness_after_write(0, …) has just cleared WRITABLE. The insert is a re-signal after a stall, not an epoll edge being recorded.
  3. ConnectionH2::ensure_tls_flushed, called a few lines above in the same function, is byte-for-byte the same gate and body and is already spelled with the helper.

Production call sites: 13 → 15.

The guard was inert, and that is why tests were added

The swap alone buys nothing if nothing walks the paths. Measured rather than assumed: replacing both sites with panic!("REACHED-…-DRAIN"), confirming the literals compiled into the test binary via strings, and running the whole suite gave 0 probe hits — no existing test reached either stalled branch, so neither debug_assert! could ever execute there.

Re-measured after #1484 landed, since its six new tests drive the write path over a real ConnectionH2<FrontRustls> and could plausibly have reached these sites: still 0 hits against main at 9472cd96.

So two tests were added to activate the guard:

  • a_stalled_window_update_drain_re_signals_the_writable_event
  • a_stalled_rst_stream_drain_re_signals_the_writable_event

Each was seen red on its own assertion independently. Deleting only the WINDOW_UPDATE re-arm fails the WU test — the stalled WINDOW_UPDATE drain must re-signal the WRITABLE event … got Readiness { interest: -WEH, readiness: ---- } — while the RST test stays green; deleting only the RST re-arm gives the mirror.

Reaching the stalled branch needs a non-empty socket_write answering (0, WouldBlock), which a loopback socket never does for 13 bytes of WINDOW_UPDATE, so BackpressuredTlsSocket gains a write_script mirroring its existing vectored_script. The empty script remains the default and delegates to the real socket, so every earlier test is byte-for-byte unaffected.

The tests' own doc comments state plainly that they cannot distinguish the two spellings — they exist to make the assertion reachable, not to test the swap.

relay.rs sweep — the issue asked, and it is unaffected ×3

lib/src/protocol/proxy_protocol/relay.rs has three more hand-rolled inserts on backend_readiness. All three are past #[cfg(test)] mod relay_test, and all three pair interest.insert with event.insert to model "the event loop observed a WRITABLE edge on the backend" before calling back_writable — their own comments say so.

The helper is reachable there but would state the wrong thing: a fixture recording an observed epoll edge is not a session signalling buffered bytes, and arm_writable's doc scopes it to production paths writing into sozu-owned buffers. Production RelayProxyProtocol hand-rolls none. Left unchanged, and no comments added — churn outside the issue.

Repo-wide, git grep -n 'event.insert(Ready::WRITABLE)' -- lib/ now returns only the definition inside signal_pending_write itself, the comment heading the two new tests (which names both spellings deliberately), and those three fixtures.

On red-then-green

No conventional red test is available and none was invented: the two spellings are the same state transition, so nothing can discriminate them. What is offered instead is the inertness measurement above, the two tests that make the assertion reachable, and each seen red under its own mutation.

An honest limitation, stated because it matters: the inverted-assert probe fires at the first signal_pending_write call in each test — enqueue-time arm_writable — not at the drain site. The panic-at-site probe is what locates it, and it confirms the helper returned there, so both its debug_assert!s executed and held.

What a future regression looks like: someone re-spells a re-arm by hand (no test can catch that), or a new drain stage sets event while clearing interest — which signal_pending_write's asserts catch in every debug build, and which now actually run because these two tests walk the paths.

Verification

cargo +nightly fmt --all -- --check                                    exit 0
cargo build  --locked --no-default-features --features crypto-ring,…   exit 0
cargo clippy --all-targets --locked … -- -D warnings                   exit 0, 0 diagnostics
cargo test -p sozu-lib --locked …    ok. 1102 passed; 0 failed         (1100 + the 2 added)
cargo test -p sozu     --locked …    ok. 113 passed; 0 failed
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked exit 0
check_doc_citations.py --root .      211 citations, 521 test names, 16 pinned blocks
check_doc_citations.py --base 9472cd96   327 lines unchanged, 0 exempt spans

No test deleted or newly ignored, no assertion loosened, no lint escape, no #[allow] added. doc/ needs no change: doc/lifetime_of_a_session.md and LIFECYCLE.md state the invariant generically, with no site enumeration and no call-site count.

CHANGELOG.md entry sits at the top of ### 🔄 Changed; the insertion is pure — zero deleted lines against main's CHANGELOG.

…ing_write

`ConnectionH2::flush_pending_control_frames`'s WINDOW_UPDATE-drain and
RST_STREAM-drain stages each ended their stalled branch with
`self.readiness.event.insert(Ready::WRITABLE)`. No behaviour changes:
`Ready::insert` cannot clear a neighbouring bit, so the two spellings are the
same state transition. This is a missing guard, not a live defect (issue #1462).

Both sites mean "pending write" in the helper's own sense, on three independent
readings. They sit under `if self.socket.socket_wants_write()`, which is
`Readiness::signal_pending_write`'s documented precondition verbatim — buffered
data that will not generate a new epoll WRITABLE event. They run immediately
after `flush_zero_to_socket` returned stalled, which means
`update_readiness_after_write` has just REMOVED the WRITABLE event bit, so the
insert is a re-signal after a stall cleared it rather than an epoll edge being
recorded. And `ConnectionH2::ensure_tls_flushed` — called by the
zero-buffer-resume stage a few lines above in the same function — is the
identical gate and body, already spelled with the helper.

What the hand-rolled spelling opted out of is the only mechanical statement of
the `event`/`interest` rule: `signal_pending_write`'s two `debug_assert!`s,
which prove it set WRITABLE and disturbed no other bit. It also opted out
invisibly — `grep signal_pending_write` did not find these two, so an audit of
"every place we signal a pending write" missed them. Production call sites go
13 to 15.

Those assertions were inert here, measured rather than assumed: replacing each
re-arm with a `panic!` and running the whole `sozu-lib` suite against the
pre-image reports 1100 passed, 0 failed, neither panic observed. No test reached
either stalled branch, so neither `debug_assert!` could ever execute on these
paths. `a_stalled_window_update_drain_re_signals_the_writable_event` and
`a_stalled_rst_stream_drain_re_signals_the_writable_event` now walk them.
Neither test can see WHICH spelling a stage uses — by construction the two are
indistinguishable from outside — so neither is offered as a regression test for
the swap; what each detects is the re-arm going missing, which strands the
serialized frame in `expect_write` with no further epoll edge coming, and each
fails on its own assertion when its block is deleted. Reaching the stalled
branch needs a non-empty `socket_write` answering `(0, WouldBlock)`, which a
loopback socket with default buffers never does for 13 bytes of WINDOW_UPDATE,
so `BackpressuredTlsSocket` gains a `write_script` mirroring its existing
`vectored_script`; an empty script — the default — still delegates to the real
socket, leaving every earlier test byte-for-byte unaffected.

The three remaining hand-rolled `backend_readiness.event.insert(Ready::WRITABLE)`
sites in `proxy_protocol::relay` are deliberately left alone: all three are
inside `relay_test`, pairing an interest insert with an event insert to model
"the event loop observed a WRITABLE edge on the backend" before calling
`back_writable`. That is a fixture recording an epoll edge, not a session
signalling buffered bytes, so the helper would state the wrong thing there.
Production `RelayProxyProtocol` hand-rolls none.
`git grep -n 'event.insert(Ready::WRITABLE)' -- lib/` finds two further hits and
no more: the insert inside `signal_pending_write` itself, and the comment
heading the two new tests, which names both spellings on purpose.

Signed-off-by: Florentin Dubois <[email protected]>
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.

flush_pending_control_frames hand-rolls the WRITABLE event bit, bypassing signal_pending_write's assertions

1 participant