Skip to content

tcp: the send clock runs from a flush's arm, so a tick no longer times out a flush being started by the last one's time - #654

Merged
MDA2AV merged 2 commits into
mainfrom
fix/send-clock-arm
Nov 23, 2026
Merged

MDA2AV merged 2 commits into
mainfrom
fix/send-clock-arm

Conversation

@MDA2AV

@MDA2AV MDA2AV commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

SendTimeoutMs (60 s by default) could close a healthy connection that was only just starting to send, and its client got no response.

A flush is marked in progress (_flushInProgress) as soon as it is called. The time its deadline runs from (FlushArmedMs) was written only later, once it had armed. On the reactor thread nothing can come between the two. Off it, a tick can: the sweep then finds a flush in progress with the last flush's time, or 0 on a connection's first flush, which is as old as the machine's uptime. When that is more than SendTimeoutMs ago, TcpSweepClose shuts the connection down.

It needs a handler that flushes off the reactor thread with a send timeout set, which is the default, and either a connection's first flush or one after an idle stretch longer than the timeout. The gap is usually well under a microsecond, 170-310 µs on the first flush of a process (the JIT), and longer when the flushing thread is preempted in it. Found in the review of #653 and reproduced on main:

  • With a 400 ms delay in the gap and default options: a first flush got its client 0 of 40 bytes, then EOF. After 62 s idle it got 40 of 80.
  • With nothing injected: 400 connections with a 600 ms timeout, flushed by four threads that an application ticker signals, so next to the sweep's own tick. Main closed 21 healthy connections in 20 s and 25 in 60 s; this branch closed none. With flushes unrelated to the tick it did not show in 3M flushes.

What changes

  • The time is written right before the arm, not after it, so whoever sees the flush armed sees its own time. The read clock already does it in that order (ReadParkedMs: "before the claim, so the sweep sees both").
  • The sweep times only an armed flush (FlushArmed). One that has been called and not armed yet is left for a later tick.

It is not written before _flushInProgress: that flag is taken by every call, so a call with nothing to send, or one refused because another flush is running, would restart the clock of a flush that is stuck, or the read clock, which reads the same time as the last one the server sent.

On the flush path this is the same store, moved ahead of the compare-and-swap; ArmFlush JITs to the same size. The sweep reads one more field, only for connections with a flush in progress. A stalled flush is still cut at the same deadline.

#384 (epoxide) has the same bug. The Kotlin port (#393) does not: it posts an off-reactor flush to the reactor.

Tests

  • SendClockTests (Unit, new) calls the sweep on a flush in each state (called and not armed, queued, with the kernel, inside its timeout), and pins when the time is written: before the arm, and not by a call with nothing to send or refused behind another flush. No sockets and no waiting. Nine mutants, each caught by at least one of them: the time written after the arm, when the flush is called, before the nothing-staged return, or never; FlushArmed always false, only when queued, or only when with the kernel; the sweep timing any flush that was called (main), or none.
  • TcpTimeoutTests: "a flush a tick catches before it arms is not timed out by the last one's time" holds the in-progress flag for two send timeouts, once with no flush before it and once behind a flush that is older than the timeout, and the client must get both responses. On main's code it fails ("closed before response 1, on a flush that had not started").

Before this, nothing tested the sweep's send clock. TCP_USER_TIMEOUT ends the same stalls as every other test makes, so with the clock switched off they all still passed. #653 adds a slow-reader test over a socket, which TCP_USER_TIMEOUT does not see.

Suite Result
E2E 800 passed, 0 failed, 1 skipped (a Kestrel kTLS test)
Unit 403 passed, 0 failed, 0 skipped
Http 113 passed, 0 failed, 0 skipped
File 14 passed, 0 failed, 0 skipped
Tls 217 passed, 0 failed, 41 skipped (every skip is a kTLS test: no tls module on this box)
Chaos 105 passed, 0 failed, 0 skipped
Pg, Redis not run, not reached by this change

All but Unit were run on the first commit; the second changes comments and tests only.

Review

Two agents reviewed this: one the correctness of the change, one the bug's reproduction, the tests and the rest of the repo.

  • No bug introduced. Close times of genuinely stuck flushes match main in every shape (plain, without MSG_WAITALL, segmented, zero-copy, incremental, pipe writer, stream, userspace TLS, Kestrel, on and off the reactor), and the read clock's times do too. A randomized stress with delays, stalls and resets cut 236 healthy clients on main and none here. Every interleaving of an arm, a second caller, a close and a recycle was explored in a model: an armed flush is never judged by an older time, which main's is.
  • Fixed from review: the tests above, FlushOutstanding's comment, and "must not throw" on IFlushListener.OnParking. A listener that threw there would leave the guard taken with nothing armed, which neither clock governs; both listeners only reset a field.
  • The same on main, not changed here: FlushArmedMs and ReadParkedMs take the reactor's cached clock while the sweep reads a fresh one, so with a timeout below about a second a flush or read can be judged up to a tick, or a long pass, too old. And _finSentMs counts from the FIN rather than from the handler's release, so a connection that called ShutdownWrite and kept reading is gone sooner after its handler returns than ReadTimeoutMs promises.

Overlap

git merge-tree --write-tree against every open PR: this change adds no conflict. #653 touches the same file (Reactor.Tcp.Sweep.cs) and auto-merges; the two merged pass TcpTimeoutTests and #653's TcpSweepTests (32 of 32). #413 and #386 add lines to tests/Ioxide.Tests.Unit/Program.cs, as this does, and conflict in the place they already conflict with main. The conflicts #556, #553, #413, #386 and #307 show against this branch are the ones they already have against main.

🤖 Generated with Claude Code

…s out a flush being started by the last one's time

A flush is in progress from the moment it is called, and the time its deadline runs from was written only once it had armed. Off the reactor a tick can come in between: it finds a flush in progress with the last flush's time, or 0 on a connection's first, and when that is SendTimeoutMs ago it shuts a healthy connection down.

The time is written right before the arm now, as the read clock's is before its claim, and the sweep times only an armed flush. Not before the in-progress flag: a call with nothing to send, or one refused as already in progress, would restart the clock of a flush that is stuck, or the read clock.
… timeout cannot stand in for it

Review found no bug in the change and that nothing tested the sweep's send clock: every stall test in the suites is also ended by TCP_USER_TIMEOUT, so with the clock switched off they all still passed. SendClockTests calls the sweep on a flush in each state, and pins when the time is written: before the arm, and not by a call with nothing to send or refused behind another flush.

Also from review: FlushOutstanding's comment says when the send clock applies, IFlushListener.OnParking must not throw (neither clock governs a flush left between its guard and its arm), and a fault in the E2E test's handler is reported as one.
@MDA2AV
MDA2AV merged commit f3b69e8 into main Nov 23, 2026
2 checks passed
@MDA2AV
MDA2AV deleted the fix/send-clock-arm branch November 23, 2026 17:14
MDA2AV added a commit that referenced this pull request Nov 23, 2026
… the table walk, only a look at the recvs waiting (#655)

A recv parked on -ENOBUFS makes the sweep walk the table, to take back what connections their handler let go hold. From the shared ring any of them can end the wait. With a ring per connection only the one waiting can, and then only once its handler has let go, so the walk served nothing for as long as a handler was too busy to read an upload.

The sweep now looks at the recvs waiting alone in that mode, and gives back what one's handler let go of. It cannot leave that out too: a connection whose handler lets go of a dry ring would never see its client close, and keep its descriptor. Condemned ones are let go of as before, walk or no walk.

Suites, on main with #653 and #654: E2E 816 (1 kTLS skip), Unit 403, Http 113, File 14, Tls 217 (41 kTLS skips), Chaos 105, none failing.
MDA2AV added a commit that referenced this pull request Nov 23, 2026
…the closing listener drops no longer outlasts the stop (#657)

"stop: a datagram that reaches a socket the stop closed is dropped" failed on arm64 with "the stop ended with a handler still holding its connection", on #654 and again on #656, each time 1.26 s into the test. Its probe connects to the port until it is refused, while the stop closes the listener. A SYN that lands as the listener closes is dropped without an answer, connect waits a second for TCP to send it again, and the stop's one-second drain is over by the time the test looks for the reactor.

Looping that step on main: 34 of 4,244 stops were refused after a second (1,062 ms at worst). With the probe giving up on a connect that has no answer in 100 ms: none of 4,914, 101 ms at worst.
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