Skip to content

http2: after a client's GOAWAY, the connection is half-closed once the last answers are sent, not aborted - #730

Merged
MDA2AV merged 47 commits into
mainfrom
fix/goaway-close
Dec 6, 2026
Merged

MDA2AV merged 47 commits into
mainfrom
fix/goaway-close

Conversation

@MDA2AV

@MDA2AV MDA2AV commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #738, its base, which stacks on #735 and #731: main ← #731 ← #735 ← #738 ← this ← #739. Merge them first, in that order. The base is fix/pipe-complete-ends-writes, so the diff here is this PR's own.

After a client's GOAWAY(NO_ERROR), #704 closes the connection once the client's last stream is answered. When that last answer is written outside a read pass, as the answer of a handler that waited on a database is, the read loop waits on a read the client has no reason to end, and #704 ended it with CancelPendingRead(). Since #725 a cancel on ioxide's TCP and TLS pipes aborts the connection, and the abort cut what was still being sent:

  • Two answers finished together. The second answer and the server's GOAWAY were queued behind the first answer's flush. That flush then reported the connection closed, and the queue was dropped.
  • A large answer. The abort's shutdown(SHUT_RDWR) landed while the send still waited for room in the socket. A client got about 2.7 MB of an 8 MiB answer, with no END_STREAM and no GOAWAY.
  • An answer the kernel had already taken. After SHUT_RDWR, Linux answers anything the client sends with a reset and drops what it still had to send. An h2 client sends WINDOW_UPDATEs as it reads.

The review of the 0.32 release found it (finding 1). 0.32.726 has it.

What changes

The stack talks to its transport only through its pipe: read, write, flush and complete.

  • The close after a client's GOAWAY completes the output. Once the write pump has sent the GOAWAY, with every answer queued before it, the stack completes its output pipe, for the close inside a read pass and outside one, unless the client has gone first. Over ioxide's pipes that ends the writing (tcp, tls: completing an ioxide pipe's writer ends the writing: what was written, then the FIN, and over TLS a close_notify before it #738): a FIN after what was sent, and over TLS a close_notify before it. The stack no longer cancels the read, and calls nothing on the connection.
    • The read side stays open. Until the output is completed, the read loop reads on, dropping what the client sends. After that, the loop ends at the client's next frame, or at its close. Over ioxide's pipes the handler then returns, and the reactor drops what the client sends and closes the connection ReadTimeoutMs after the FIN, unless the client closes first (tcp: what a peer sends after its handler let go is dropped, not queued, and the sweep's close cancels the recv #731).
    • That matters for credit: an h2 client sends a WINDOW_UPDATE for what it reads, Go's every 4 KiB. Left unread, they filled the connection's receive queue (RecvQueueEntries), and its overflow closed the connection with the rest of the answer still to be sent. The review of this PR found that (its finding 1).
  • Over ioxide's own pipes that close is guaranteed: the answers, the GOAWAY, then the FIN, over TLS behind a close_notify. If the client closes while the GOAWAY's flush is still out, the stack ends without completing its output, and ioxide's pipes end the connection anyway. A pipe of another kind gets its output completed once the GOAWAY is out while the client is still there; a wrapper passes Complete through to ioxide's writer for the same end. The docs say no more than that: the Rebalance remarks of TcpOptions, the remarks of Http2Connection(IDuplexPipe, IRingHost, Http2Options?) and the architecture page.
  • The cancel when the write side has died stays. Over a pipe of another kind nothing else ends the read waiting there: a default Pipe's input stays open when its output's reader has gone. The Unit suite's faulted flushes depend on it. Over ioxide's pipes the read ends anyway, as the reactor closes a connection whose send failed. It is now guarded by !_outputCompleted, which changes no behaviour: once the output is completed nothing reaches it (the review's mutant M2, without the guard, passes every test). The guard states the rule.
  • _tcp is left for threading alone: Host and OffReactor, the reactor serving the connection, found by the pipe's type. A follow-up PR removes that reach-through.
  • No public API changes.

Who notices

  • A client that sends GOAWAY and stays connected gets every answer, then the server's GOAWAY, then the end of the writing. Over TLS a close_notify comes before the FIN.
    • That holds whatever HTTP/2 frames the client sends meanwhile, as long as it has read the answers by ReadTimeoutMs after the FIN. A client still reading the answer then is reset if it sends anything, such as credit, and loses what the kernel had left to send. A silent client, or ReadTimeoutMs 0, gets it all. With the default 60 s it takes a slow client to meet that.
    • Over TLS, bytes that are not a TLS record end the read with a fault: the stack ends at once, and what is still queued behind the flush in flight is dropped. Under kTLS receive the reactor closes the connection and the client gets a reset. Only a client that breaks TLS meets that.
  • After either close the handler returns once the output is completed, at the client's next frame or its close. A client that never closes is let go about ReadTimeoutMs (60 s by default) after the FIN, silent or not: a silent one by the read timeout of the read left waiting, one that keeps sending by the reactor, which drops what it sends after the release (tcp: what a peer sends after its handler let go is dropped, not queued, and the sweep's close cancels the recv #731).
  • Over a pipe of another kind, such as a default Pipe, the stack no longer ends itself after the close. It completes its output once its GOAWAY is out, unless the client has gone first, and ends at the client's next frame or close.
  • tcp: what a peer sends after its handler let go is dropped, not queued, and the sweep's close cancels the recv #731 makes the reactor drop what a client sends after its handler let go, where it queued it until the receive queue overflowed and cut the end of the answer (the review's release probe). This PR relies on it: once the output is completed the stack lets the connection go, and the client's credit for the rest of the answer reaches the reactor.

Tests

Http2PeerGoAwayWireTests (E2E) gets the reviews' cases. Each one checks that every answer arrives whole, then the server's GOAWAY, then the end of the connection:

  • two answers finished together, one timer wait waking both handlers: 1 MiB and a small one, to a client that reads nothing until 500 ms after they are written, so the first answer's flush still waits when the connection closes;
  • an 8 MiB answer to a client that reads in silence;
  • an 8 MiB answer to a client that credits what it reads, with a WINDOW_UPDATE every 16 KiB, and every 512 KiB;
  • the 16 KiB one with the handler answering at once, so the close comes inside the read pass that reads the GOAWAY;
  • a client that keeps sending after the server's FIN, a PING every 250 ms, and never closes, with ReadTimeoutMs 2000, after the close outside a read pass and inside one: the connection has to end about ReadTimeoutMs after the FIN, its handler ended.
  • over TLS and kTLS: the server's GOAWAY is followed by its close_notify, then the FIN, also after an 8 MiB answer to a client that reads late. The client for these is RawTlsStream (tcp, tls: completing an ioxide pipe's writer ends the writing: what was written, then the FIN, and over TLS a close_notify before it #738), which tells a close_notify from a bare end.

Http2PeerGoAwayTests (Unit) runs over StrictClient, whose input is a default Pipe and whose output StrictWriter, which refuses writes after Complete as ioxide's writers do. It checks that the stack ends its writing, its output completed, where the last stream ends, and only then: two answers finished in one turn, the second and the GOAWAY queued behind the first's held flush, all go out before the output is completed. The review's mutant M4, which completes at once, fails that case in 2 of 2 runs, where it passed the whole class before. The client's close then ends the run.

The class runs over TCP, TLS, and kTLS both ways where the kernel has its tls module, as other TLS tests do. Its ends must be a FIN, over TLS after a close_notify: a reset fails it.

On main:

  • the 8 MiB tests, silent and every 512 KiB, failed in 63 of 64 runs, with the answer cut and no END_STREAM or GOAWAY;
  • the two-answers test failed in 21 of 21 runs over TCP and 21 of 21 over TLS. A typical failure: stream 1: 114688 bytes, no END_STREAM; no GOAWAY from the server; connection ended;
  • the 16 KiB test failed in 4 of 4 runs over TCP and 4 of 4 over TLS, and so did the one inside a read pass.

On d25b914, this PR's first head, which half-closed but stopped reading at the client's next frame, the 16 KiB test failed in 8 of 8 runs over TCP and 8 of 8 over TLS, the answer cut after 4.6 to 5.1 MB. The one inside a read pass failed in 4 of 4 over each.

On 2c5cac1, the head before the stack let the connection go at its FIN, the chatty tests failed in 2 of 2 runs over TCP and TLS, both closes: the connection was still open 6 s after the FIN, the handler still running. Without #731 they pass, but the 16 KiB tests fail, both closes: what the client sends after the release fills the receive queue.

Under the second review's mutant M1, _readToClose = !_passFlushPending; (reading on only after a close outside a pass), the test inside a read pass fails in 4 of 4 runs over TCP and 4 of 4 over TLS, and the rest of the class passes.

With this PR all of them pass every run:

  • 40 runs of the class on the host, 14 tests each, and 32 more with the test inside a read pass, 16 each (the 8 kTLS tests skip there);
  • 3 runs of the class in a VM with the kernel tls module loaded, and 3 more with its kTLS round, 24 tests each, 8 of them over kTLS both ways;
  • the review's probes on the host: fine, an 8 MiB answer to a client crediting every 16, 64 and 512 KiB, after the close inside a read pass and outside one, and at a 4 KiB step that credits once per 16 KiB DATA frame, so as often as at 16 KiB: 24 of 24; cases 6 of 6;
  • in that VM, cases and fine with kTLS both ways among the transports: 33 of 33, 11 of them over kTLS.
  • with tcp: what a peer sends after its handler let go is dropped, not queued, and the sweep's close cancels the recv #731 merged and the chatty tests: at tcp: what a peer sends after its handler let go is dropped, not queued, and the sweep's close cancels the recv #731's fd1f85b, 32 runs of the class on the host, 20 tests each (its 10 kTLS tests skip there), and 3 in the VM, 30 each, 10 of them over kTLS both ways; at 169d06f, the sweep close that cancels the recv, 3 more on the host and 3 more in the VM. The chatty client was refused 2,501 to 2,510 ms after the FIN, its handler ended, in every run.

#704's three tests still pass.

Two mutants were checked on an earlier head, which shut the write side itself:

  • Never shut the write side: 12 of the class's 14 tests fail, as the client never sees the end in time.
  • Shut it at once instead of after the pump's turn: the close inside a read pass then sends its FIN before the GOAWAY, which that pass flushes only afterwards. The test with nothing in flight fails over TCP and TLS, 3 of 3 runs.

Suites

On #738, which is on #735 at f6f7d19. x64 Linux 6.14, each process pinned to eight cores. The host has no kernel tls module, so the kTLS tests skip there. They ran in a VM with the module loaded.

Suite Host VM with the tls module
The GOAWAY class 22 of 22 in 3 runs, 12 kTLS skipped 34 of 34 in 3 runs, 12 over kTLS
E2E 1,252 passed, 0 failed, 31 skipped, 5 pending
Unit 438 passed, 0 failed, 2 pending
Http 113 passed, 0 failed
Tls 256 passed, 0 failed, 100 skipped 367 passed, 0 failed, 0 skipped

The Kestrel let-go failure the VM had earlier was a test flake, the test's first TLS handshake outlasting its 300 ms read clock. #731 fixed it in e28c7d0, which this branch has now.

At e22a6ed, with #738's review fixes merged in: the Unit suite 438 passed, 0 failed, 2 pending, and the GOAWAY class with its new case 24 of 24 in 3 runs; the wire class with AdapterEndTests 24 of 24 on the host in 2 runs (12 kTLS skipped) and 36 of 36 in the VM in 2 runs.

…e last answers are sent, not aborted

After a client's GOAWAY(NO_ERROR), a close outside a read pass ended the read loop with CancelPendingRead(). Since #725 that aborts the connection on ioxide's TCP and TLS pipes, and the abort cut what was still being sent: an answer queued behind a flush, a large answer still waiting for room in the socket, and, once the client sent credit for what it read, the rest of the send queue.

Over ioxide's pipes the stack now half-closes the connection once the write pump has sent the GOAWAY (TcpConnection.ShutdownWrite()). The read side stays open for the client's close, and nothing is written after the FIN. Over other pipes the cancel stays.

Http2PeerGoAwayWireTests gets the three cases of the 0.32 review, over TCP and TLS: two answers finished together, and an 8 MiB answer to a client that reads in silence or credits what it reads.
…ent that reads late, so it catches the abort every run
… the close inside a read pass too, so the client's credit can't fill the receive queue
…d, so it can't cut the end of the answer

After the handler's release the reactor keeps the read side open to the peer's FIN, but it still queued what the peer sent for a reader that no longer exists. A peer that kept talking while it read the end of the answer filled the queue in RecvQueueEntries deliveries, and the overflow tore the connection down with the end of the answer still in the kernel's send queue.

Both receive completions now hand a released connection's buffer straight back instead of queueing it. The FIN, ReadTimeoutMs (from the write shutdown, which the peer's bytes don't move) or the stop still end the connection.

Tests: a client that sends a byte per 16 KiB it reads after the let-go gets all of an 8 MiB answer, and one that keeps sending after the FIN is still let go on the read clock, each with a shared ring and a ring per connection. LetGoBufferTests' let-go connections that hold the ring take its buffers before the let-go now, and ReactorLoopTests' pending loop test keeps its connection.
… a read pass, and a kTLS round where the kernel has its tls module
…nd the 8 MiB test can fail with a ring per connection

The let-go tests from #525 made a let-go connection hold buffers by sending after the let-go, which this PR drops, so they no longer reached ReturnReleasedBuffers. LetGoDriver now holds its handlers at a gate once they're done with their connection: what their clients send then is queued, and held at the let-go, and what they send after it is dropped. That covers LetGoHandlerStyleTests, KestrelLetGoTests (gated through the reactor's TcpHandle), TlsLetGoBufferTests and LetGoChaosTests. IncrementalDryRingTests' client fills the ring before the let-go, and LetGoBufferTests' incremental upload sends before and after it.

With ReturnReleasedBuffers made a no-op they fail as on main again: 39 E2E, 9 Tls and 6 Chaos tests. A drop that keeps its buffer fails 23, 6 and 4 (shared ring) or 13, 3 and 2 (ring per connection).

The 8 MiB test's ring per connection has 128 buffers and its client sends 4 KiB per 16 KiB read: with the default 16 the queue never got past 16 of its 64 entries, so it could not fail on main. It now does in both modes.

The parked-read test's name no longer claims what it would take later: that is dropped now.
…close at a client's GOAWAY, and cuts what is still going out
… lets the connection go, so ReadTimeoutMs bounds a client that keeps sending
@MDA2AV
MDA2AV changed the base branch from main to fix/released-recv-drop December 5, 2026 16:03
…ing it at the deadline can't keep it open

The read and send clocks and Abort() close a connection with shutdown(SHUT_RDWR), and left its teardown to the recv's next completion. A peer still sending then gets a reset, and the kernel can leave a multishot recv that was busy at that moment uncompleted for good (seen on 6.14 under a flood): the connection, its descriptor and its slot stayed until the reactor stopped. Now that what a released connection's peer sends is dropped, a flooding peer no longer ends at the queue's overflow, so the read clock's close has to end it. Abort() on a live handler under a flood had the same gap already.

TcpSweepClose now also cancels the recv, as the overflow's close does. Its -ECANCELED runs CloseFromRecv, the same teardown, refcount and sends in flight included. A recv that ends on its own after the close is torn down instead of armed again, and a move whose recv the close cancels ends here. A recv parked on -ENOBUFS has nothing to cancel; CloseCondemnedStarved ends it, as before. A reactor without a ring, refused by the kernel or never run, has nothing to cancel either.

Tests: TcpSweepTests floods four connections at a time, half in 64 KiB writes and half in 1-byte writes, for 6 rounds, in both ring modes. In one test the connections are let go and closed by a 500 ms read clock; in the other their handlers drain the flood and the connections are aborted. Without the cancel, all four tests failed in every run (60 of 60, over 15 runs), in the first round. With it, they passed 116 of 116 (29 runs).
…, whose first handshake outlasted a 300 ms read clock

The process's first TLS handshake takes ~360-400 ms in the kTLS VM: the first case over TLS, "shared ring, they leave", has a 300 ms read clock, which ended the connection while the server waited on the client's Finished, and the client's write of its request then hit a broken pipe (later handshakes take 2-3 ms). One handshake with an SslStream of this process first takes that cost out. In the VM the Kestrel let-go cases now pass 6 of 6 runs; without it, the first TLS case failed in 5 of 6.
MDA2AV added a commit that referenced this pull request Dec 5, 2026
…ed again by the next case

A Lazy kept the warm-up's exception, so one failed warm-up failed every TLS case of the process, though it only takes a first-use cost out. A failed one now leaves the cases to run cold, and the next case tries again.
…s its socket gracefully

Abort() closed the connection as an expired clock does: shutdown(SHUT_RDWR) and the recv cancelled. What was flushed was lost when the peer hadn't read it all and sent anything meanwhile, a zero-copy send was reset, and the peer's last bytes, still unread in the socket, turned the close into a reset that dropped the rest of the answer.

For the handler nothing changes: its pending and later reads and flushes end with the connection closed, at once, a flush waiting on a send the kernel has included; that send goes on. The socket is then finished as a let-go connection's is: the FIN after the sends still out, zero-copy ones included, what the peer sends dropped, and the peer's close ending it. ReadTimeoutMs from the FIN, and SendTimeoutMs for a stuck send, cut it with the sweep's hard close, as before. With both at 0 it stays until the peer closes. Writes while an aborted connection's last send is out are dropped, rather than put in the slab the kernel still sends from. The zero-copy reset in TcpAbort is gone.

Tests: GracefulAbortTests covers an abort with a flush still going out to a slow reader, plain and zero-copy, and an abort with the client's last bytes unread in the socket, in both ring modes. All four fail on #731's head (3.5 MB of 8 MiB, 1.2 MB then a reset, 127 KB of 512 KiB then a reset). TcpSweepTests' flood abort, ConnectionAbortTests' -ENOBUFS abort and MoveHttp2OverPipesTests' cancel are adapted to the graceful finish.
…od is dropped and its read clock runs from the FIN, and writes during the aborted send are dropped

Every abort test had its handler return as soon as the abort let it go, so the let-go covered the drop and the clock, and no test wrote after an abort: three of the changes could be undone without a failure. GracefulAbortTests now aborts a connection whose handler waits, reading nothing, while its client floods it (both ring modes), and writes and flushes every way after an abort let the handler's flush go, with the aborted send still out. Without the abort's drop both flood tests fail (shared ring: overflow; ring per connection: the flood stalls); without its read clock both stay open 3 s; with writes throwing again the write test fails.

Tighter: the MoveHttp2 cancel now expects "push", the -ENOBUFS abort test the FIN and not a reset, and the slow-reader tests skip when the socket took the whole body before the abort, rather than fail.
…comments no longer describe the old abort

ReadTimeoutMs runs from the FIN, which waits for the sends still out: with SendTimeoutMs at 0, a peer that stops reading holds an aborted connection whatever ReadTimeoutMs is. The summary says what is written after the abort is dropped; the remarks say the clocks' close resets only a peer that sends after it or whose bytes were unread, and that the send clock counts from the flush's handover.
…bort() ends a TLS connection with a bare FIN

Abort() is a TCP-level end. Over BoringSSL, a write after the abort was encrypted and its records dropped, spending sequence numbers the client never saw; the close_notify a pipe disposed once the aborted send was over then went out (the FIN waits for the tick) and didn't decrypt, and the client lost the last record with a decryption error. Now WriteEncrypted encrypts nothing on a closed connection, and TlsSession.Dispose sends no close_notify for an aborted one, under BoringSSL and kTLS alike: the client gets what was flushed, then a bare FIN, and can tell the connection was cut. A connection that isn't aborted still ends with its close_notify.

Tests (TlsAbortTests): Abort() mid-flush with a write flushed after it and the pipe disposed before the FIN gives every byte, then a bare FIN with no close_notify and no decryption error, over BoringSSL and over kTLS. On the previous head BoringSSL got 8,372,224 of 8,388,608 bytes, then a decryption error, and kTLS a close_notify. A clean close still sends close_notify.
…bort is dropped, TcpAbort no longer submits the ring, and the scratch doesn't outlive its connection

A flush from another thread whose closed check ran before the abort, armed after the abort's MarkClosed, which found nothing armed to let go, could reach the reactor's flush queue before its own closed re-check: it was then sent after the abort, and its caller waited for the send, with a stalled peer until SendTimeoutMs or for good. DrainFlushQ now lets such a flush go unsent, its caller as closed and its bytes dropped, as MarkClosed lets go a flush the kernel doesn't have. TcpAbort drains the queue before it sets Aborted, so what was queued before the abort still goes out; a flush on the reactor thread can't race it. The window is a few instructions wide, so ArmFlush calls a test-only hook, null otherwise, to line it up.

TcpAbort's submit loop put the send in the kernel ahead of the old SHUT_RDWR; the FIN now waits for the send (FlushOutstanding), so it's gone. The scratch that writes during an aborted send go to is cleared with the connection, not kept in the pool.

Tests (GracefulAbortTests): a flush racing the abort is not sent, its caller returns at once and the client gets only the FIN (without the queue's check: the reactor sent it, and its caller still waited 1 s later); the scratch is cleared when the connection is recycled (without the reset it outlived its connection).
…as written, then the FIN, and over TLS a close_notify before it
… the run over a default Pipe, and over TLS a close_notify comes before the FIN
@MDA2AV
MDA2AV changed the base branch from fix/released-recv-drop to fix/pipe-complete-ends-writes December 5, 2026 21:59
…t after its walk fails it

It set the flag by hand before the walk, so setting it after CloseAll in Teardown passed. Teardown now runs on the never-run reactor, ring disposal included: with the flag set after the walk, 9 of 16 closed, then the throw; with no flag check at all, the same.
… scratch's reset and the TLS abort test's preconditions are pinned

- TlsAbortTests: a connection closed without an abort (MarkClosed), a write flushed after it, then the pipe's dispose: the close_notify still decrypts. Without WriteEncrypted's closed check the client gets 49,152 bytes, then a decryption error; no test caught that before, as an abort sends no close_notify after the dropped record.
- AbortedFlushDrainTests: an entry the drain finds after the abort for a flush the kernel already has leaves that send alone, its guard held and its bytes kept (fails if the drop takes any state, not only an armed flush); and a drain that submits the armed flush now says so.
- GracefulAbortTests waits for the scratch's reset itself: Clear() moves the generation on first, and the read right after it failed about 2% of the time.
- TlsAbortTests' abort mid-flush skips, rather than fails, when the socket took the whole body before the abort, checks that its dispose came before the FIN, and that the session ran the backend asked for.
…ends still gets its close_notify

A handler that can return should just return; Abort() is for ending a connection from code other than its handler, while the handler is parked in a read or a flush, and it ends the same graceful way. Abort() itself sends no close_notify, but a handler that disposes its TLS pipe before the posted abort runs closes TLS normally first: close_notify, then the FIN. Doc only.
…e with no room for the close_notify, a faulted one, and one before an abort or a TLS cancel; a move waits for the kTLS alert

PipeCompleteTests, over TCP, BoringSSL, kTLS send and kTLS both ways:
- Complete from another thread, 5 bytes unflushed: the bytes, then (close_notify,) the FIN.
- Complete while a flush of 8 MiB is out, on the reactor and from another thread, then the flush awaited: whole, then
  (close_notify,) the FIN.
- Complete when the socket has no room for more: 512 KiB flushed into a socket that takes it at once and keeps most of
  it unsent, then its send buffer cut below what it holds. The kTLS alert waits on the ring for room; the answer comes
  whole, then the close_notify, then the FIN.
- A faulted Complete: what was flushed, not what wasn't, then the FIN alone, no close_notify. Also with a flush of 8 MiB
  out, over TCP and BoringSSL: under kTLS transmit the pipe's dispose right after the flush sends TlsSession.Dispose's
  close_notify for it.
- TLS: Complete while a flush of 8 MiB is out, then TcpConnection.Abort(): whole, then a bare FIN; or then the pipe's
  cancel: whole, then one close_notify, then the FIN.

TlsMoveKtlsTests, kTLS send and both ways: a move begun while the completion's alert waits on the ring for room waits
for it, and the handler's read goes on on the other reactor.

Harness: RawTlsStream.Connect takes a receive buffer size; TcpSockets reads and sets a socket's SO_SNDBUF.
…ds since its cancel went to the session; a move test's comment says a faulted completion ends with the FIN alone
…t it

- A TLS pipe's cancel on a connection already closed, or once its session is disposed, still makes the TCP abort,
  which lets go of a flush the kernel holds; only TLS's end is skipped there (MED-1). Test: the pipe's cancel after the
  client closed, with a flush of 8 MiB the kernel holds, lets go of that flush at once.
- The close_notify's send on the ring starts the read clock that a FIN starts, so a peer that keeps the socket out of
  room can't hold a released or aborted connection past ReadTimeoutMs (MED-2). Test: the alert waiting for room, the
  handler returned, ReadTimeoutMs 1 s: the connection goes.
- A pending alert is dropped after TcpConnection.Abort() only, not whenever the connection is marked closed: a pipe's
  dispose that ends a parked read marks it closed too (LOW-1). The connection's other ends suppress the write shutdown,
  which EndWriteSide already checks. Tests: a read parked, Complete while a flush is out, or with 64 KiB unflushed, then
  the pipe disposed: the close_notify, then the FIN.
- A faulted Complete under kTLS settles the close_notify as ruled out, so TlsSession.Dispose sends none behind a flush
  still out (LOW-2). Test: the faulted completion while a flush of 8 MiB is out, now under kTLS too.
- A recv-side teardown waits for a close_notify's send on the ring, queued or in flight, as it waits for a send in the
  kernel: the descriptor stays the connection's until the kernel has the op, and the op's completion lets go
  (Reactor.CloseNotifyOver) (LOW-3). Test: the alert waiting for room, the handler returned, the client's FIN: the
  connection keeps its life until the alert is out.
- TlsSession.Dispose settles the close_notify it sends, and EndWrites queues the alert only when none is settled, so a
  Complete from another thread that a dispose overtakes sends no second one (NIT-1). The flag that says so is renamed
  for what it now means (CloseNotifyQueued -> CloseNotifySettled). Test: Complete from another thread, then the dispose
  on the reactor's thread first: one close_notify, then the FIN.
- TlsEncryptingPipeWriter.FlushAsync after Complete returns a completed result, as the kTLS half does, instead of
  throwing; a Pipe's writer doesn't throw there either (NIT-2). Test: the small answer's handler flushes after Complete.

And two cases pinned as they are: TcpConnection.Abort(), then the pipe disposed at once on the reactor's thread, ahead
of the posted abort, gives the flushed data, a close_notify, then the FIN; and Abort() with SendTimeoutMs 0 releases a
handler whose flush waits on a client that reads nothing, while the send goes on until the client reads it.
… two answers finished in one turn, and the wire tests take only a FIN as the end

- StrictWriter throws on GetSpan, GetMemory and Advance once completed, as ioxide's writers do.
- Http2PeerGoAwayTests: two answers finished in one turn after the client's GOAWAY, the second and the GOAWAY queued
  behind the first's held flush. The output must not be completed until both answers and the GOAWAY are out: the
  review's mutant M4 (completing in CloseIfDrained, at once) fails it, where it passed the whole class before.
- Http2PeerGoAwayWireTests: the wire client records a read that ended by a reset, and the class's ends must be a FIN
  (over TLS after a close_notify), not a reset.
…nce its own GOAWAY is out, unless the client has gone first; the graceful close is what ioxide's pipes give
MDA2AV added a commit that referenced this pull request Dec 6, 2026
MDA2AV added a commit that referenced this pull request Dec 6, 2026
MDA2AV added a commit that referenced this pull request Dec 6, 2026
…d, and the sweep's close cancels the recv (#731)

When a handler's task ends, the reactor sends the peer a FIN and keeps the read side open until the peer closes, bounded by `ReadTimeoutMs`. Until now, what the peer sent in that time was still queued in the connection's receive queue, for a reader that no longer exists. A peer that keeps talking while it reads the end of the answer, such as an HTTP/2 client crediting what it reads or an upload a server answered early, filled the queue in `RecvQueueEntries` (64) deliveries. The reactor then took that for an overflow and tore the connection down, with the end of the answer still in the kernel's send queue: the peer got a cut answer.

Found by the review of #730. ioxide.http2's close after a client's GOAWAY lost answers this way after the handler had returned (#730 now reads on to the client's close, so it no longer returns early). A raw handler shows it without HTTP/2: it flushes 8 MiB and returns, and a client that sends 4 KiB per 16 KiB it reads got 7,072,164 of its 8,388,608 bytes, then the end.

With that dropped, a peer that floods a let-go connection no longer meets the overflow, so its read clock has to end it. The review found that the clock's close could leave such a connection open for good, which `Abort()` on a live handler under a flood could already do on main. The close also cancels the recv now.

### What changes

- **The two receive completions** (`Reactor.Tcp.cs`, shared ring and ring per connection) don't queue a delivery for a connection whose handler let go. Its buffer goes straight back: to the shared ring, or, with a ring per connection, to its own ring once the kernel is done with it and nothing queued before the let-go still holds it.
  - The receive stays armed, so the peer's FIN still ends the connection, and `ReadTimeoutMs` still ends one that never closes. Its clock for a released connection starts at the write shutdown, and nothing the peer sends moves it.
  - A read a handler left parked when it returned is handed nothing more. Nobody may use the connection past its handler's end (#701).
- **`ReleaseHandlerRef`'s doc** says what the peer sends after the release is dropped.
- **`TcpSweepClose`**, the close of the read and send clocks, of `Abort()` and of the stop, also cancels the connection's recv, as the overflow's close does.
  - Before, it shut the socket down and left the teardown to the recv's next completion. A peer still sending when the shutdown lands gets a reset, and the kernel can then leave a multishot recv that was busy at that moment uncompleted for good (seen on 6.14, under a flood). The connection, its descriptor and its slot then stayed until the reactor stopped.
  - The cancel's -ECANCELED runs `CloseFromRecv`, the teardown the recv's own end runs, refcount and sends in flight included. A recv that ends on its own after the close is torn down instead of armed again.
  - A move whose recv the close cancels, or finds parked on -ENOBUFS, ends here instead of carrying the connection on.
  - A recv parked on -ENOBUFS has nothing to cancel, and `CloseCondemnedStarved` ends it, as before. Neither does a reactor without a ring, refused by the kernel or never run.
  - Nor is there a recv to cancel once the reactor's loop is over. The teardown's walk comes before the ring's dispose, which ends every recv anyway. A loop that faulted with its submission queue full would have the cancel's SQE throw "io_uring SQ still full after repeated flush" there, skipping the rest of the walk and of the teardown. The teardown marks the loop over before its walk; the stop's own walk runs inside the loop and keeps the cancel.

The reactor already did this for a released connection on a recv starved of buffers (`ReturnReleasedBuffers`, on -ENOBUFS). It now does it on every delivery.

### Who notices

- A peer that sends while it reads the end of an answer its handler already let go of gets the whole answer, then the FIN, instead of the answer cut and a reset, as long as it has read it by FIN + `ReadTimeoutMs`. That bound is the read clock's, from the FIN, and it covers reading the rest of the answer too: the FIN goes out once the kernel has taken the last bytes, which can be megabytes before the peer has them. A peer still reading then that sends anything is reset, and loses what the kernel had left to send. A silent peer, or `ReadTimeoutMs` at 0, gets it all. With the default 60 s it takes a slow peer, e.g. 2 MB still unsent at the FIN read at under 35 KB/s.
- A peer that never closes is ended as before: by its FIN, `ReadTimeoutMs` or the stop. With `ReadTimeoutMs` at 0 it stays until it closes, as before (the default is 60 s).
- A peer flooding a connection when its read clock or an abort closes it no longer keeps it open: it could stay until the reactor stopped.

### Tests

New in `TcpSweepTests` (E2E), each with a shared ring and with a ring per connection:
- **"a client that sends 4 KiB per 16 KiB it reads after the handler let go still gets the whole answer, then the FIN".** A raw handler writes 8 MiB, its flush completes, and it returns. The client reads slowly, silent until the let-go, then sends 4 KiB (one buffer's worth) per 16 KiB it reads.
  - On main it fails in every run, in both modes: the client gets 7,072,164 of 8,388,608 bytes.
  - The ring per connection has 128 buffers here. With the default 16 it could never fail: a queued delivery holds a buffer no other one holds, so its queue never got past 16 of its 64 entries.
- **"a client that keeps sending after the handler let go is still let go on the read clock".** With `ReadTimeoutMs` at 500, the client sends a byte every 20 ms after the FIN and never closes. The server still closes the connection 0.3-3 s after the FIN. It passes on main as well: it guards the bound, so that dropping what the peer sends can never keep a connection open past its read clock.

- **"clients that flood after their handlers let go are let go on the read clock, 6 x 4"** and **"handlers draining a flood, aborted, let their connections go, 6 x 4"**. Each round floods four connections at once, two in 64 KiB writes and two in 1-byte writes, as any one gets stuck only sometimes. The connections are let go and closed by a 500 ms read clock, or drained by their handlers and aborted. Each must be let go 0.3 to 2.5 s after the client has read the four FINs, or within 2 s of the abort. The lower bound keeps the drop covered: on main the overflow closed the connection in 5 ms.
  - With the cancel reverted, all four failed in every run, 60 of 60 over 15 runs, almost always in the first round: the 1-byte flooders' connections were still open 3 s on.
  - With it they passed 116 of 116, over 29 runs.

- **`ConnectionSlotTests` (Unit), "the teardown's walk over a reactor whose ring takes no more SQEs closes every connection, and throws nothing".** A ring made on another thread stands in for one that is full and won't take more: its enters are refused on this thread (SINGLE_ISSUER), so the walk can take only its 8 entries. All 16 connections are closed, and nothing is thrown. Without the check, 9 of 16 closed, then the throw.

Adapted: the let-go tests from #525 built their "a let-go connection holds buffers" cases by sending after the let-go, which now holds nothing. They now take those buffers before the let-go, and send after it as well, so they test both the give-back and the drop:
- **The let-go driver** (`LetGoDriver`), used by `LetGoHandlerStyleTests`, `KestrelLetGoTests`, `TlsLetGoBufferTests` and `LetGoChaosTests`.
  - Its handlers now wait at a gate once they're done with their connection, reading nothing. What their clients send then is queued, and held when they let go.
  - After the let-go the clients send as much again, which is dropped.
  - Kestrel's adapter is gated through its reactor's `TcpHandle`.
  - `KestrelLetGoTests` also warms the TLS client up before its TLS cases, best effort: a failed warm-up is tried again by the next case. A process's first handshake took ~400 ms in the kTLS VM, past the 300 ms read clock of "they leave". The server then ended the connection mid-handshake and the client's request hit a broken pipe, on main too in 2 of 3 VM runs.
- **`LetGoBufferTests`.**
  - Its let-go connections that hold every buffer of the ring take them before the let-go, through a new `'H'` handler that reads nothing until the test releases it. This applies to `DryTheRing`, `ThenLeave`, the round with a ring per connection, and the incremental upload, which now uploads before and after the let-go.
  - "a read left parked when its handler let go strands nothing it takes later" is now "reads left parked when their handlers let go keep no buffer from the clients after them": what they would take later is dropped.
- **`IncrementalDryRingTests`.** "a connection whose ring ran dry after its handler let go" is now "a connection whose client filled its ring before the handler let go, unread": the handler reads nothing until the client has sent.
- **`ReactorLoopTests`, the pending "a continuation hung by hand on a connection's ReadAsync that throws...".** Its handler now keeps the connection until the test is done, since a read left behind past the let-go is handed nothing. It still pends for its own defect.
  - This touches the comment #729 rewords. Whichever of the two merges second takes this one's version.

Each mutant was applied alone and run against the let-go tests:
- E2E: `LetGoBufferTests`, `IncrementalDryRingTests`, `LetGoHandlerStyleTests`, `KestrelLetGoTests`, `TcpSweepTests`, `StarvedRecvTests`, `StarvedRecvEdgeTests` and `RecvBufferReclaimTests`, 134 tests;
- Tls on the host: `TlsLetGoBufferTests`;
- Chaos: `LetGoChaosTests`.

| Mutant | E2E | Tls | Chaos |
|---|---|---|---|
| `ReturnReleasedBuffers` does nothing | 39 fail | 9 fail | 6 fail |
| the shared ring's drop keeps the buffer | 23 fail | 6 fail | 4 fail |
| the ring per connection's drop keeps the buffer | 13 fail | 3 fail | 2 fail |
| only the ring per connection's half of the fix reverted | 1 fails (the 8 MiB test) | 0 | 0 |

The review of this PR measured the first mutant on main, where the tests built on the post-let-go queue, at 39, 9 and 6. Without the adaptations, the PR left 7, 0 and 0 of them.

On main with the same tests, all of them pass but the two 8 MiB tests, which fail in every run (3 of 3).

With the fix, the four new tests passed 40 of 40 times over 10 runs of `TcpSweepTests`, and the 134 let-go tests passed three runs in a row.

Suites on this branch, x64 Linux 6.14, pinned to 8 cores. The host has no kernel `tls` module, so the kTLS tests skip there, as on main. They ran in a VM with the module loaded.

| Suite | Result |
|---|---|
| E2E | 1,228 passed, 0 failed, 19 skipped, 6 pending (main: 1,220; the 8 new tests are in `TcpSweepTests`) |
| Unit | 437 passed, 0 failed, 2 pending (the teardown test is new) |
| Http | 113 passed, 0 failed |
| Chaos | 65 passed, 0 failed |
| File | 15 passed, 0 failed |
| Tls | 242 passed, 0 failed, 82 skipped |
| Tls in a VM with the `tls` module | 335 passed, 0 failed, 0 skipped |
MDA2AV added a commit that referenced this pull request Dec 6, 2026
…s its socket gracefully, with a bare FIN over TLS (#735)

Stacked on #731: its base is `fix/released-recv-drop`, and it uses #731's drop of what a let-go connection's peer sends.

`TcpConnection.Abort()`, and with it the pipes' cancels, closed the connection the way an expired clock does: `shutdown(SHUT_RDWR)` and, since #731, a cancel of the recv. That lost data:
- what was flushed and still going out, when the peer sent anything before it had read it all;
- a zero-copy send, which the abort reset;
- the end of the answer, when the peer's last bytes were still unread in the socket. Their close turned into a reset that dropped what the kernel had left to send. The review of #730 and #731 measured 127,070 of 524,288 bytes.

The decision: wait for the timeout; if the data still isn't out by then, force the abort.

### What changes

- **For the handler, nothing.** Its pending and later reads and flushes end with the connection closed, at once. That includes a flush waiting on a send the kernel already has: the caller is let go, and the send goes on.
- **The socket is finished as a let-go connection's is:**
  1. the sends still out complete, zero-copy ones included;
  2. then the FIN, through the deferred `ShutdownWrite`;
  3. what the peer sends from the abort on is dropped (#731's drop, extended to aborted connections);
  4. the peer's close ends the connection.
- **The clocks bound it as for a let-go connection.**
  - `ReadTimeoutMs` runs from the FIN: `WaitingOnPeer` uses `_writeShutdownMs` for aborted connections too.
  - `SendTimeoutMs` cuts a send that is stuck.
  - Either one cuts with the sweep's hard close: `SHUT_RDWR` and the recv cancel. A peer still reading the end of the answer then, and sending meanwhile, is reset.
  - With both clocks at 0 the connection stays until the peer closes, like any finished one.
  - The read clock waits for the FIN, and the FIN waits for the sends still out. So with `SendTimeoutMs` at 0, a peer that stops reading holds an aborted connection whatever `ReadTimeoutMs` is. With `ReadTimeoutMs` at 0, a peer that reads everything but never closes holds it.
- **Writes while an aborted connection's last send is out are dropped.** Before, a write during a flush threw "Cannot write while flush is in progress". Now that the flush's caller is let go before the send is over, what it writes after that goes to a scratch buffer, not into the slab the kernel still sends from. A connection that isn't closed behaves as before.
- **TLS.** `Abort()` is a TCP-level end and knows nothing of TLS.
  - Nothing written after it is encrypted: `WriteEncrypted` drops what is written on a closed connection before `SSL_write`. Before, a record encrypted and then dropped spent a sequence number the client never saw, and whatever followed it, a close_notify included, didn't decrypt.
  - It ends with a bare FIN, no close_notify, under BoringSSL and kTLS alike: `TlsSession.Dispose` sends none for an aborted connection, including while its FIN still waits behind the last send. The client can tell the connection was cut.
  - To end a TLS connection with its close_notify, cancel through its TLS pipe. That becomes true with the core Complete PR, which merges in the same round.
- **A flush from another thread that reaches the reactor after the abort is dropped.** One whose closed check ran before the abort but whose arm came after the abort's `MarkClosed` could be claimed by the flush queue before its own closed re-check. It was then sent after the abort, and its caller waited for the send. `DrainFlushQ` now lets it go unsent: its caller is let go as closed, and its bytes are dropped.
  - `TcpAbort` drains the queue before it sets `Aborted`, so what was queued before the abort still goes out.
  - A flush on the reactor thread can't race the abort, and `SubmitFlush`'s other callers, the reactor-thread flush and `Adopt`, can't see an aborted connection.
- **The zero-copy reset in `TcpAbort` is gone**: the FIN waits for the send instead. So are its `CloseCondemnedStarved` call, since the abort no longer shuts the read side, and its `SubmitAndWait` loop, which put the send in the kernel ahead of the old `SHUT_RDWR`.
- **The scratch that writes during an aborted send go to** is cleared when the connection is recycled, so it doesn't stay in the pool.
- **Not changed:** the hard close of the clocks and the stop, with the recv cancel; Kestrel's own `Abort`, which shuts its socket down itself.
- **Docs:** `Abort()`'s summary and remarks, and the `ReadTimeoutMs` and `SendTimeoutMs` remarks, which said `Abort()` ends a connection "as this does". The remarks now say:
  - what is written after the abort is dropped;
  - the clocks' close resets only a peer that sends after it, or whose bytes were still unread;
  - the send clock counts from the flush's handover;
  - with `SendTimeoutMs` at 0, nothing but the peer ends an aborted connection whose peer stopped reading.

  No docs page describes `Abort()`.

### Who notices

- A handler that aborts, or cancels a pipe, after flushing: its peer gets what was flushed, then the FIN, where it could get a reset and lose the end.
- A peer that keeps a connection open after an abort holds it until `ReadTimeoutMs` after the FIN, as after a handler's return. Before, it was closed at once.
- #732 changes `Abort()`'s doc to warn about the reset this PR removes. Whichever of the two merges second takes this one's doc.

### Tests

New, in `GracefulAbortTests` (E2E). Each fails on #731's head (`426f4906`) and passes here:

| Test | On #731's head |
|---|---|
| a flush still going out to a slow reader (8 MiB, client reading 64 KiB every 5 ms), then `Abort()` once it has read 1 MiB: the handler's flush ends at once (under 500 ms), and the client gets every byte, then the FIN | 3,556,685 of 8,388,608 bytes |
| the same under `ZeroCopySend` | 1,175,470 bytes, then a reset |
| the client's last bytes still unread in the socket when the abort lands (the review's probe: 512 KiB flushed, 4 bytes sent while the reactor is busy), shared ring | 127,070 of 524,288 bytes, then a reset |
| the same, ring per connection | 127,070 of 524,288 bytes, then a reset |

Also new: an aborted connection whose handler still runs. In every test above the handler returns as soon as the abort lets it go, so the let-go would cover the drop and the clock. These keep the handler running:

| Test | Fails without |
|---|---|
| a connection aborted while its handler waits, reading nothing, while its client floods it (shared ring) | the abort's drop: the flood overflows the queue; the abort's read clock: still open 3 s after the FIN |
| the same, ring per connection | the abort's drop: the flood stalls; the abort's read clock: still open 3 s after the FIN |
| what a handler writes and flushes after the abort let its flush go, with the aborted send still out (`Write`, `GetSpan` + `Advance`, `GetMemory` + `Advance`, then a flush) is dropped: no throw, and the client gets exactly the 8 MiB flushed before, then the FIN | dropped writes: "Cannot write while flush is in progress" |

Each mutant fails only its own tests among the 7 of `GracefulAbortTests`. The slow-reader tests skip, rather than fail, when the socket takes the whole 8 MiB before the abort, as on a host whose send buffer allows that.

TLS, in `TlsAbortTests`:

| Test | On the previous head (`efcbe84b`) |
|---|---|
| `Abort()` mid-flush, a write flushed after it, the pipe disposed once the send is over and before the FIN: every byte, then a bare FIN, no close_notify, no decryption error (BoringSSL) | 8,372,224 of 8,388,608 bytes, then a decryption error |
| the same over kTLS (VM) | every byte, then a close_notify |
| a pipe disposed once its flush has gone out, not aborted, still ends with close_notify (BoringSSL, and kTLS in the VM) | passes, as here |

The client reads the server's records as they come, so it can tell a close_notify from a bare FIN.

Also in `GracefulAbortTests`:
- **The scratch is cleared when the connection is recycled.** Without the reset, it outlived its connection.

And in the Unit suite, `AbortedFlushDrainTests`:
- **A flush racing the abort.** The race is a few instructions wide, so the state it leaves is set up directly on a reactor that never runs: an aborted, closed connection with a flush armed and queued. Then the queue is drained by hand.
- **Result:** the flush's caller is released, its bytes dropped, and nothing submitted. Without the drain's check the flush is submitted, which throws on that reactor's missing ring.

Changed to the new behaviour:
- **`TcpSweepTests`, "handlers draining a flood, aborted, let their connections go"**, both ring modes, now "... on the read clock". The flooding clients' connections are let go 0.3 to 2.5 s after the abort, by a 500 ms read clock from the FIN, where they used to close at once. On #731's head they close in 5 ms and fail the lower bound.
- **`ConnectionAbortTests`, "Abort of a connection whose recv is parked on -ENOBUFS"**, both ring modes. Its read still ends at once, and the client gets the FIN at once (a FIN, not a reset). The connection is then let go by its read clock (500 ms), where it used to be let go in the pass that took the abort: with its recv parked, nothing can see the client's FIN.
- **`MoveHttp2OverPipesTests`, "a flush asked for from another thread, then cancelled..."**, with and without a move. The client gets "push", then the FIN: the abort always finds the flush claimed, by the flush queue it drains first or by the arrival that took it up in transit. The client then closes, and the connection is let go then; before, the abort closed it from the server's side.
- **`ConnectionAbortTests`' zero-copy comment.** It said ending the flush takes a reset; the test itself is unchanged and passes.

Unchanged and passing: the rest of `ConnectionAbortTests`, `TlsAbortTests`, `TlsPipeTests`, `PrologueReaderTests` and `OffReactorFlushTests`. A waiting read or flush still ends at once, and the client still sees the end.

Suites on this branch, x64 Linux 6.14, pinned to 8 cores. The host has no kernel `tls` module, so the kTLS tests skip there; they ran in a VM with the module loaded.

| Suite | Result |
|---|---|
| E2E | 1,230 passed, 2 failed, 19 skipped, 6 pending in the full run at the first commit. The 2 failures were `MoveHttp2OverPipesTests`' two cancel cases, adapted above. At the head, the abort, graceful-abort, sweep and MoveHttp2 classes pass 70 of 70 (2 pending). |
| Unit | 438 passed, 0 failed, 2 pending |
| Http | 113 passed, 0 failed |
| Chaos | 65 passed, 0 failed |
| File | 15 passed, 0 failed |
| Tls | 244 passed, 0 failed, 84 skipped |
| Tls in a VM with the `tls` module | 339 passed, 0 failed, 0 skipped |
MDA2AV added a commit that referenced this pull request Dec 6, 2026
…as written, then the FIN, and over TLS a close_notify before it (#738)

**Stacked on #735**, its base, which stacks on #731: main ← #731 ← #735 ← this ← #730. Supersedes #734, which put the close_notify in the HTTP/2 stack: here it lives in the TLS pipe's writer. Fixes #676.

A protocol stack talks to its transport through the pipe alone: read, write, flush and complete. To end its writing it completes its output, and this PR makes ioxide's pipes turn that into the transport's ending, as completing a `Pipe`'s writer ends what its reader gets. Until now `Complete()` on ioxide's writers did nothing on the wire.

### What changes

- **`TcpConnectionPipeWriter.Complete()`** (and `CompleteAsync`) ends the writing on the socket: what was written goes out, then the FIN, behind any flush still in flight, through the existing deferred `ShutdownWrite`. The read side stays open. From any thread: off the connection's reactor the end is posted there. A writer kept past its connection's life ends nothing.
- **The TLS writers' `Complete()`** sends what was written, then a close_notify, then the FIN (RFC 8446 6.1):
  - **BoringSSL** (`TlsEncryptingPipeWriter`): what is staged is encrypted, `SSL_shutdown` makes the record, and both go into the connection's slab. The connection's own send carries them out, partial sends included, and the FIN waits for that send. A completion while one of its flushes is still in flight is carried out when that flush is done, before the flush's caller resumes.
  - **kTLS transmit** (the connection's own writer, made by `TlsConnectionDualPipe` with a close_notify): the kernel makes the records, and an alert needs its record type in a cmsg, which the send path doesn't carry. So once the flush is idle, the alert is a SENDMSG on the ring, and the FIN follows its completion.
    - It is submitted at once, so the kernel takes the socket while the fd is still the connection's. An SQE left in the queue could meet the fd closed by the connection's recycle and taken by the next accept: the first version sent the alert into another client's handshake in the TLS move tests.
    - While it is in flight the connection doesn't move, and a teardown keeps the connection's descriptor until it is over, as for a send in the kernel. The read clock (`ReadTimeoutMs`) runs from its send, as it runs from a FIN, so a peer that keeps the socket out of room doesn't hold a connection whose handler let go. A completion for a connection recycled since does nothing. A stopping reactor submits nothing, and the FIN goes out alone.
    - Deferred behind a flush, it goes the way `DeferWriteShutdown` does: the tick sends it, and a move takes the deferral along.
- **A connection already closed or aborted** gets nothing from a writer's `Complete`: no close_notify, no FIN, and over BoringSSL nothing encrypted, which would spend a sequence number the client then misses (the #735 review's finding 1). The transport's own ending is the end.
- **`TlsSession.Dispose`** sends no close_notify once the close_notify is settled: a writer's completion queued one, a faulted completion under kTLS ruled one out, or a cancel queued one. One it sends settles it too, so a completion posted from another thread that the dispose overtook sends no second one.
- **No new public API.** #734's `TlsConnectionDualPipe.ShutdownWrite()` is dropped: completing `Output` does it.

### A TLS pipe's cancel

Each layer ends its own part.
- **`TcpConnection.Abort()`** stays TCP's: over TLS it ends with a bare FIN, and nothing written after it is encrypted (#735).
- **A TLS pipe's cancel** ends the TLS layer first: `CancelPendingRead` or `CancelPendingFlush` on `TlsDecryptingPipeReader`, `TlsEncryptingPipeWriter`, and the kTLS halves `TlsConnectionDualPipe` makes. It runs on the connection's reactor (`TlsSession.CancelEnd`, internal), in this order:
  1. A close_notify after what was flushed. Over BoringSSL it is the record `SSL_shutdown` makes, sent on the ring as it is, since the abort closes the slab. Under kTLS it is the alert, the SENDMSG with its cmsg.
  2. Nothing more is encrypted: the abort closes the connection, and `WriteEncrypted` encrypts nothing on a closed connection.
  3. Then the TCP abort (#735's): reads end, the send it lets go on goes on, and the FIN follows the close_notify, deferred behind that send.

  The order on the wire is the flushed data, the close_notify, then the FIN. On a connection already closed, as by the client's FIN with a send still in the kernel, or once the session is disposed, only the TCP abort runs, which still lets go of a flush waiting on that send.

The rules where the two meet, from the merge of #735:
- `Complete` or a TLS cancel sends a close_notify.
- After a TCP-level abort nothing is encrypted and no close_notify is sent. That includes one a completion left pending: it goes out only for a TLS cancel. A pipe's dispose that marks the connection closed to end a parked read is no abort: a pending close_notify still goes.
- A closed connection's `Complete` sends nothing.
- `TlsSession.Dispose` sends none after `Abort()`, nor once a completion or a cancel queued one.

### Bytes written but not flushed when `Complete` is called

They go out, ahead of the FIN (and the close_notify). That is what `Pipe` does: its `Complete` commits what was written, and the reader gets it. Checked against .NET 11's `Pipe`:
- after `Complete`, `GetSpan` and `GetMemory` throw `InvalidOperationException`, while `FlushAsync` and a second `Complete` don't;
- a faulted `Complete(exception)` hands the reader the exception and no data.

So here:
- writing after `Complete` throws `InvalidOperationException` (`GetSpan`, `GetMemory`, `Advance`). `FlushAsync` sends nothing and returns completed, under BoringSSL and kTLS alike: `TlsEncryptingPipeWriter.FlushAsync` threw there before. A `Pipe`'s writer doesn't throw there either; its result's `IsCompleted` says whether its reader completed, and is false then.
- a faulted completion drops what was staged and sends no close_notify, only the FIN, so a failure isn't dressed up as a whole answer. The BoringSSL writer already dropped its staged plaintext then.

### The review's fixes

The review of this PR (MED-1, MED-2, LOW-1 to LOW-3, NIT-1, NIT-2), fixed as it suggested, each with a test that fails without it:
- **MED-1.** A TLS pipe's cancel on a connection already closed, or once its session is disposed, skipped the TCP abort too, so a flush the kernel held waited on (a regression from #735). Now only TLS's part is skipped.
- **MED-2.** The close_notify's send on the ring was on no clock: a peer that kept the socket out of room held a released connection. It now starts the read clock, as a FIN does.
- **LOW-1.** A pending alert was dropped whenever the connection was marked closed, which a pipe's dispose does to end a parked read (kTLS send). It is dropped after `TcpConnection.Abort()` only; the connection's other ends suppress the write shutdown, which was already checked.
- **LOW-2.** Under kTLS, a faulted `Complete` with a flush out still ended with `TlsSession.Dispose`'s close_notify. A faulted completion now settles the close_notify as ruled out.
- **LOW-3.** An `io_uring_enter` that submitted nothing left the alert in the submission queue, where the connection's recycle could close its fd and an accept take the number. A recv-side teardown now keeps the descriptor while a close_notify's send is queued or in flight, as it does for a send in the kernel, and the send's completion lets go (`Reactor.CloseNotifyOver`).
- **NIT-1.** A `Complete` from another thread that the pipe's dispose overtook sent a second close_notify under kTLS. `TlsSession.Dispose` settles the one it sends, and `EndWrites` queues the alert only when none is settled. The flag is renamed for what it now means (`CloseNotifyQueued` → `CloseNotifySettled`).
- **NIT-2.** `FlushAsync` after `Complete` threw over BoringSSL and returned completed under kTLS. Both return completed now.

### Tests

`PipeCompleteTests` (Tls) runs over TCP, BoringSSL, kTLS send and kTLS both ways. The client for TLS is `RawTlsStream` (harness), which tells a close_notify from a bare end, as `SslStream` doesn't.
- A small answer, unflushed, then `Complete`: the answer, then (close_notify,) the FIN. Then the client sends, and the handler reads it: the read side is open. Writing after `Complete` is refused, and a flush after it returns completed.
- 8 MiB to a client that starts reading 500 ms late and reads slowly, the last 64 KiB unflushed: whole, then (close_notify,) the FIN.
- 8 MiB flushed, then completed at once while the client still reads, so the send buffer is full: whole, then (close_notify,) the FIN.
- The pattern of an application such as GenHTTP: complete the input, complete the output, then dispose the TLS pipe, 20 ms later. It sends one close_notify, then the FIN, with nothing between them.
- `Complete` from another thread, 5 bytes unflushed: the bytes, then (close_notify,) the FIN.
- `Complete` while a flush of 8 MiB is out, on the reactor and from another thread, then the flush awaited: whole, then (close_notify,) the FIN.
- `Complete` when the socket has no room for more: 512 KiB flushed into a socket that takes it at once and keeps most of it unsent, as the client reads nothing yet, then the socket's send buffer cut below what it holds (`SO_SNDBUF`). Under kTLS the alert waits on the ring for room; the client then gets the answer whole, the close_notify, then the FIN.
- A faulted `Complete`: what was flushed, not what wasn't, then the FIN alone, no close_notify; also with a flush of 8 MiB out, the pipe disposed once it is done.
- TLS: `Complete` while a flush of 8 MiB is out, then `TcpConnection.Abort()`: whole, then a bare FIN; or then the pipe's cancel: whole, then one close_notify, then the FIN.
- TLS: a read parked, then `Complete` while a flush of 8 MiB is out, or with 64 KiB unflushed, then the pipe disposed: the close_notify, then the FIN.
- TLS: `Complete` from another thread, overtaken by the pipe's dispose on the reactor's thread: one close_notify, then the FIN.
- TLS: the writer of a connection already aborted, completed, sends no close_notify, and nothing the client can't decrypt.
- TLS: a pipe's cancel gives what was flushed, then a close_notify, then the FIN: the reader's cancel after a small answer, and the writer's cancel while a flush of 8 MiB to a slow reader is out, which goes on whole.
- TLS: `TcpConnection.Abort()` gives what was flushed, then a bare FIN: no close_notify, nothing the client can't decrypt, and no reset.
- TLS: `TcpConnection.Abort()`, then the pipe disposed at once on the reactor's thread, ahead of the posted abort: what was flushed, then a close_notify, then the FIN.
- The pipe's cancel after the client closed, with a flush of 8 MiB the kernel holds, lets go of that flush at once.
- `TcpConnection.Abort()` with `SendTimeoutMs` 0 and a flush of 8 MiB to a client that reads nothing yet releases the handler at once; the connection stays, and the client then gets it whole, then the FIN alone.
- kTLS: the alert waiting for room, the handler returned, `ReadTimeoutMs` 1 s: the connection goes.
- kTLS: the alert waiting for room, the handler returned, then the client's FIN: the connection keeps its life, and its descriptor, until the alert is out.

`TlsMoveKtlsTests`, kTLS send and both ways: a move begun while the completion's alert waits on the ring for room waits for it to be out, and the handler's read goes on on the other reactor.

Harness: `RawTlsStream.Connect` takes a receive buffer size, and `TcpSockets` reads and sets a socket's `SO_SNDBUF`.

### Mutants

Each one change to this PR's source, built and run against the tests above, twice, in a VM with the kernel `tls` module. Every one fails a test in both runs.

| Mutant | Caught by |
|---|---|
| `Complete` ending nothing, as before | the TCP tests ("expected [5], got [0]") |
| `TlsSession.Dispose` sending its close_notify regardless | the application pattern, under kTLS: a second record before the FIN |
| kTLS sending a raw `MSG_DONTWAIT` alert instead of the ring's SENDMSG | `Complete` with no room in the socket, and the move test, under kTLS send and both ways: a bare end |
| a pending close_notify sent after `Abort()` too | `Complete` with a flush out, then `Abort()`, under kTLS: a close_notify after the abort |
| BoringSSL's `Complete` ignoring the flush in flight | `Complete` with a flush out (both threads), faulted with a flush out, and the cancel after a deferred `Complete`, over BoringSSL |
| a move ignoring the alert in flight | the move test, under kTLS: it moved with the alert out. On the wire this mutant looks the same; the test checks the rule that a connection stays where its alert is out |
| a faulted kTLS `Complete` sending the alert | the faulted `Complete`, under kTLS: a close_notify |
| a TLS cancel dropping a completion's pending alert | the cancel after a deferred `Complete`, under kTLS: a bare end |

And each of the review's fixes reverted alone: MED-1 fails the cancel after the client closed (BoringSSL, kTLS send, kTLS both ways: the flush still waited 5 s); MED-2 the read clock on the alert (kTLS: still held 5 s after the handler returned); LOW-1 both parked-read tests (kTLS send: a bare end); LOW-2 the faulted completion with a flush out (kTLS: a close_notify); LOW-3 the descriptor test (kTLS: recycled with the alert out); NIT-1 the overtaken completion (kTLS: a second close_notify); NIT-2 the small answer's flush after `Complete` (BoringSSL: it threw). 2 of 2 runs each, nothing else failing.

### Results

At 8c810d1, on #735. CPUs 16-31. The host has no kernel `tls` module, so the kTLS tests skip there; they ran in a VM with the module loaded.

| Run | Host | VM with the `tls` module |
|---|---|---|
| `PipeCompleteTests` and the kTLS move test | 34 passed, 50 kTLS skipped | 84 of 84 in 3 runs |
| Tls | 278 passed, 0 failed, 134 skipped | 423 passed, 0 failed, 0 skipped |
| E2E, the teardown classes (aborts, sweep, let-go, moves, slots, adapter end, timeouts) | 296 passed, 0 failed | |

An earlier version of the TLS move class failed 3 times in 4 runs. In that version the kTLS alert's SENDMSG waited in the submission queue, and a connection recycled meanwhile gave its fd to the next accept, so the alert went into another client's handshake. It is submitted at once, and since LOW-3 a teardown also keeps the fd while it is queued.

Before the restack, on #731, one full E2E run passed: 1,228 passed, 0 failed, 19 skipped, 5 pending. In it, #676's pending test passed, so it is a test now (`AdapterEndTests`): unflushed bytes reach the peer when the pipe is completed.
Base automatically changed from fix/pipe-complete-ends-writes to main December 6, 2026 00:46
@MDA2AV
MDA2AV merged commit dde6b99 into main Dec 6, 2026
2 checks passed
@MDA2AV
MDA2AV deleted the fix/goaway-close branch December 6, 2026 00:46
MDA2AV added a commit that referenced this pull request Dec 6, 2026
…no longer looks for the connection under it (#739)

**Stacked on #730**, its base: main ← #731 ← #735 ← #738 ← #730 ← this. Merge them first, in that order.

A protocol stack never touches `TcpConnection` or concrete transport pipes. After #730, the HTTP/2 stack's last reach-through was `_tcp`, the connection under `TcpConnectionDualPipe` or `TlsConnectionDualPipe`, found by the pipe's type. It used that for threading alone: `Host`, the reactor serving the connection, which follows it from reactor to reactor, and `OffReactor`.

### What changes

- **New public API: `ioxide.IRingHostedPipe`.** It is an `IDuplexPipe` that says which reactor serves it, through `IRingHost RingHost { get; }`, the host that follows its connection (`TcpOptions.Rebalance`).
  - `TcpConnectionDualPipe` and `TlsConnectionDualPipe` implement it, so each gains a public `RingHost` property: the connection's `RingHost`.
  - A pipe wrapped around one of them can pass it through and be served the same way.
- **The HTTP/2 stack takes its host from the pipe:** `(pipe as IRingHostedPipe)?.RingHost`. `_tcp` is gone, and so is every mention of ioxide's pipe types in the stack's logic.
  - `Host` and `OffReactor` read that host. The connection's host gives `OnReactorThread` from the reactor serving the connection, which is what the stack computed through `_tcp`, so nothing changes over ioxide's pipes.
  - A pipe that says its host is served there whatever host it is given, as ioxide's pipes were.
- **The public constructors are unchanged** and work as before, the `Http2Connection(TcpConnection, ...)` convenience included.
- **Docs:** the `Http2Connection(IDuplexPipe, IRingHost, Http2Options?)` remarks, the Rebalance remarks of `TcpOptions` and the architecture page say that a wrapper that passes `IRingHostedPipe` through is followed as ioxide's pipes are.

### Tests

`MoveWrappedHttp2Tests` (E2E) gets a third pipe: a wrapper around ours that passes its host through. Under the move shuffle, 16 callers × 120 requests each, it counts the requests dispatched on a reactor other than the connection's:

| Pipe | Answered | Dispatched off the connection's reactor (2 runs) |
|---|---|---|
| ioxide's own | 1,920 of 1,920 | 0, 0 |
| a wrapper that passes nothing through | 1,920 of 1,920 | 1,329, 1,518: it stays on the reactor it began on |
| a wrapper that passes its host through | 1,920 of 1,920 | 0, 0 (asserted) |

The move and off-reactor HTTP/2 classes all stay green: `MoveHttp2Tests`, `MoveWrappedHttp2Tests`, `MoveWrappedTlsHttp2Tests`, `MoveHttp2OverPipesTests`, `MoveHttp2GivenHostTests`, `OffReactorStreamTests`, `OffReactorH2FixTests`, `OffReactorH2EarlyFlushTests`, and `Http2PeerGoAwayWireTests`.
- Host: 110 passed, 0 failed, 30 skipped (kTLS), 2 pending, in 2 of 2 runs.
- VM with the kernel `tls` module: 140 passed, 0 failed.

Unit: 438 passed, 0 failed, 2 pending. CPUs 16-23.



### Review follow-up

- `MoveWrappedHttp2Tests` also gives ioxide's pipe, and a wrapper that passes its host through, another reactor as their host on purpose, while the connection moves: a pipe that says its host is served there. The review's mutant M6 (`_host = host`) fails both (1,423 and 1,547 requests dispatched off the connection's reactor); this PR passes.
- The class summary, the explicit-host constructor's summary and `LearnHost`'s warning name a pipe that says its host (`IRingHostedPipe`) rather than ioxide's two pipes alone; the warning also says a wrapper can pass ioxide's pipe's host through. `OffReactorH2EarlyFlushTests` pins the warning's new opening words.
- The unused `using ioxide.tls;` goes, and the long line in `TcpOptions` is wrapped.

At 2907330, with #730 and #738's review fixes merged in, one run on the host of the HTTP/2 move and off-reactor classes (`MoveWrappedHttp2Tests`, `MoveHttp2Tests`, `MoveHttp2GivenHostTests`, `MoveHttp2OverPipesTests`, `OffReactorStreamTests`, `OffReactorH2FixTests`, `OffReactorH2EarlyFlushTests`): 89 passed, 0 failed, 16 skipped, 2 pending (as before).
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