Skip to content

TD-5452: Server: one slow reader never holds up the next connection - #313

Open
captcrock wants to merge 1 commit into
mainfrom
td/5452-fan-out
Open

captcrock wants to merge 1 commit into
mainfrom
td/5452-fan-out

Conversation

@captcrock

@captcrock captcrock commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Four paths sent to one connection after another, so each connection waited out the drain of the one before it. A viewer whose phone stopped reading held the next viewer's session list up by its whole drain (1.005 s behind a 1.0 s drain). The four paths are session and machine list updates (hub.py), cu_permissions (daemon.py) and a device's own frames (ws.py send_to_device). Each now reaches every connection at once. So does the app-level ping, which the review found doing the same.

What changes

  • core/tstd/ws.py:
    • New deliver(frames): each connection gets its own frame. Every frame is built first, then every place in a connection's send line is taken in one pass before any send, as a broadcast's are (TD-5446), so a later frame never overtakes it. Each connection is then sent to in its own task. A connection that closed meanwhile is skipped.
    • broadcast and deliver share that sending step (_send_all).
    • One connection is sent to in place, as a push is: no task, and no change in timing from main.
    • The app-level ping (TD-1716) pinged one client after another, so a reader that stopped reading held every other client's ping up by its drain, long enough for those clients to call the silence a zombie. Each client is now pinged in its own task. A client still draining its last ping is skipped, so at most one ping waits behind it.
  • send_to_device, Hub._on_rows, Hub._on_machines and Daemon._emit_cu_permissions use deliver.
  • Tests in core/tests/test_reply_funnel_off_loop.py, next to TD-5446's:
    • On each of the four paths, a reader that drains for 1 s never holds up the next connection.
    • A delivery keeps its place behind older frames and ahead of newer ones.
    • A cancelled delivery leaves no place behind.
    • send_to_device skips a closed connection.
    • A delivery waits behind a reply that is still being scrubbed.
    • A frame that can't be built leaves no place behind.
    • A delivery cancelled before it sends leaves no place, on one connection and on two, and before its tasks start.
    • A stuck reader never holds up another client's pings.
  • docs/tst-desk-backlog.md: TD-5452 entry.

Before / after

e2e/td5452/test_td5452_fanout_probe.py calls each real path with two connections, the first draining for 1.0 s (results/td5452-fanout-probe.out):

path main cb20b685: when B gets its frame this branch
hub._on_rows (session list) 1.005 s 0.000 s
hub._on_machines (machine list) 1.005 s 0.000 s
daemon._emit_cu_permissions 1.006 s 0.000 s
ws.send_to_device 1.005 s 0.000 s

App-level pings (e2e/td5452/ping_probe.py, results/td5452-ping-probe.log), with a 0.05 s interval and one reader draining 1.0 s: in 0.4 s the other client gets 1 ping on main and 7 here. The stuck reader gets 1 on both, so nothing piles up behind it.

Question

The ticket's last point is left as it is: a broadcast still returns only once its slowest connection has its frame, so a producer that sends two in a row waits out a slow reader between them. That covers two broadcasts, and also two session-list updates, now that those are deliveries, so the second reaches the other connections that much later. Within one broadcast or delivery, connections are sent to at once.

Returning sooner would remove the only thing that slows a producer down to its slowest reader. A reader that is alive but slow would then collect frames with no bound; the ping loop closes only dead ones.

  • (A) Keep it as it is.
  • (B) Return once every place is taken, send in tracked tasks, and close a connection whose queue passes a limit. The client reconnects and re-reads its state. I'd measure the limit (frames and bytes queued per connection) before setting it.

I recommend (B), with the limit set from a measurement. Your call.

Independent review

A separate reviewer, given the diff, the ticket and main's probes, wrote its own probes. Verdict: FIX FIRST, all fixed:

  1. deliver took each place while it read its frames. A frame_for that raised for a later connection (send_to_device) left the earlier connections a ready place that never sent, so their later pushes hung until they disconnected. Not seen in production: frame_for there is defensive. Every frame is now built first, and a test covers it.
  2. The app-level ping loop was a sequential fan-out the ticket missed. The reviewer measured another client's pings at 0.05 / 1.11 / 2.16 s behind a reader draining 1.0 s. Fixed as above, with a test.
  3. Tests weaker than claimed. The order behind a reply being scrubbed, and cancelling before any send (including the one-connection path), were untested. Tests added.

Checked by the reviewer, nothing found:

  • Order: pushes during a delivery, a reply scrubbed off the loop followed by a delivery, two deliveries, and a broadcast plus a delivery. Taking every place in one pass is stricter than main, where the second connection's place was taken only after the first had drained.
  • Cancellation and closed connections.
  • The one-connection inline path matches the task path.
  • Errors: the hub still reaches the other connections. cu_permissions now goes on past a failing connection, where it used to stop.
  • The four fan-out tests fail on main.

Re-review of the fixes: the code fixes are correct. Both new tests fail on the code before the fixes and pass after.

  • Pings can't reorder frames from the send line. In websockets 17.0.1 a text frame is written when its send starts, before any yield, as on main.
  • One gap left, fixed after it: cancelling a delivery before its tasks start was still untested. The test now cancels right after the places are taken. It fails with the cleanup removed and passes with it (results/td5452-cancel-test-guard.log).
  • A question, taken: an unexpected exception from one ping ended the whole loop on main. Now it's logged, and the other clients' pings go on.

Tests

  • tests/test_reply_funnel_off_loop.py: 36 passed. The two tests for the review's findings fail on the commit before the fixes .
  • The 78 test files that touch the server, hub, broadcasts, cu_permissions or pings: 1,990 passed, 1 skipped on the final commit (results/td5452-related-tests-r4.log).
    • On the first run, with every connection always sent to from a task, test_two_daemons.py::test_self_pairing_shows_every_row_twice_and_the_remote_copy_survives failed 3 of 3 times. Its ask(list_machines) was taking a pushed machine_list frame and discarding the session_list it later waited for, because the single-connection push now came one loop turn later. Sending one connection in place, as main does, fixed it (3 of 3 passed alone, and it passed in both related runs since). The test itself is unchanged.

Checks

  • ruff, ruff format and mypy: clean.
  • release_scan --git: 0 hits.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SPdoY5jgXgTw6gUHBMBngR

Session and machine list updates, cu_permissions, send_to_device and the
app-level ping reach every connection at once: deliver() builds every frame,
takes every place in one pass, and sends to each connection in its own task.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SPdoY5jgXgTw6gUHBMBngR
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