Skip to content

Fix streaming callbacks cancelled after 10s when a downlink closes late - #4184

Open
T4rk1n wants to merge 4 commits into
devfrom
fix/stream-downlink-close-race
Open

T4rk1n wants to merge 4 commits into
devfrom
fix/stream-downlink-close-race

Conversation

@T4rk1n

@T4rk1n T4rk1n commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Under load, ASGI streaming callbacks could stop after about 10 seconds with only part of their frames: the client got done early.

A downlink's close reads the connection record (is it still mine?), then writes it closed. Those are two round trips. When the browser's next downlink opened between them, the stale close overwrote the live open record, and the pumps cancelled the live streams once DOWNLINK_GRACE passed. With 4 uvicorn workers on LocalSharedStorage at 4000 streaming browsers, this cut 1-2% of streams.

The newest downlink of a page now owns its record (records carry opened):

  • A close writes closed over its own record or an older downlink's.
  • An open async downlink reads the record every DOWNLINK_GRACE / 2 and writes it open again only when an older downlink closed it. It never writes over an open record or a newer downlink's, so a relay whose client vanished unnoticed can't keep a page alive.
  • A close waits for a repair in flight before writing closed.
  • A record without opened (written by an older Dash) is left alone.

Load test (4 uvicorn workers, LocalSharedStorage, 4000 browsers, 20 frames per stream): 28 to 74 incomplete streams per run before (6 runs), none after. The extra cost is one aget per open downlink every 5 seconds.

Tests in tests/streaming/test_stream_transport.py:

  • test_stale_close_does_not_end_a_live_downlink replays the interleaving. It fails without the fix.
  • test_relay_of_a_gone_client_does_not_keep_the_connection_alive checks that a stale relay can't mask a browser that left.
  • test_refresh_in_flight_does_not_reopen_a_closed_downlink checks that a repair landing late can't reopen a closed record.
  • test_closed_record_from_an_older_dash_is_left_alone covers records from an older Dash.

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Dash performance benchmarks

✅ all within thresholds

scenario metric p90 (ms) median growth baseline p90 note
✅ callback_chain chain_ms 325.7 318.3 0.94x 499.1
✅ callback_chain graph_ms 1.2 1.2 1.0x 2.4
✅ callback_fanout fanout_ms 55.7 52.3 0.91x 92.5
✅ deep_nesting render_ms 37.1 35.6 1.01x 56.8
✅ full_children_replace replace_ms 103.4 67.9 2.68x -
✅ initial_render_large render_ms 412.1 353.7 0.87x 694.4
✅ initial_render_small render_ms 79.8 65.9 0.89x 104.0
✅ patch_append_nested append_ms 78.1 57.1 2.32x 192.3
✅ patch_append_toplevel append_ms 74.7 55.2 2.11x 140.2
✅ patch_scalar_update_large update_ms 94.8 83.9 0.95x 202.9
✅ rerender_existing_subtree rerender_ms 85.4 68.4 0.83x -
✅ wildcard_all_resolve wildcard_ms 202.6 188.2 0.98x 313.6
✅ wildcard_all_resolve graph_ms 0.9 0.9 1.0x 1.3

growth = late-third / early-third per-op time; ~1 is flat, a large value means the per-op cost scales with accumulated state.

machine scale vs baseline: 0.69x - divided out of the baseline ratios so they compare like for like (the absolute warn/fail ceilings are left un-scaled); calibrated on initial_render_small.

@sonarqubecloud

Copy link
Copy Markdown

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.

2 participants