Repository navigation
feat: wake parked subscribers on context cancellation - #11
VojtechVitek wants to merge 4 commits into
Conversation
A Subscriber parked in Read() slept in sync.Cond.Wait() and only observed ctx cancellation via the non-blocking check before the wait. With no Write/Close calls, a canceled subscriber context never woke a parked reader -- it could sleep forever. The documented workaround was to keep calling Write() with zero items. Register context.AfterFunc(ctx, ...) in Subscribe() to broadcast on ctx cancellation. The parked reader wakes, loops, and returns ctx.Err() via the existing checkEnd path. The Write hot path is untouched (lock-free fill + writePos.Store + cond.Broadcast, unchanged) -- the cost is one AfterFunc registration per Subscribe call. Write-path benchmarks unchanged (ns/op within noise, 0 allocs/op). Co-Authored-By: DeepSeek V4.1 Flash <noreply@deepseek.com>
Per maintainer review: the wake costs one context.AfterFunc goroutine per subscriber, parked until ctx is done -- too much for a throughput library to impose by default. Move it behind SubscribeOpts.WakeOnCancel, default off. Guard ctx.Done() == nil (Background/Todo) even when opted in. Restore the Write() doc note for the default path: an idle writer should occasionally call Write() with zero items to drop canceled subscribers. Default-path cost: Subscribe 3 allocs/op, 176 B/op (~58 ns), Write untouched. Opted-in: 9 allocs/op, 696 B/op (~685 ns), one parked goroutine per subscriber. Benchmarks included. Co-Authored-By: DeepSeek V4.1 Flash <noreply@deepseek.com>
Full-suite benchmarks before/after: no regressions (benchstat all ~, p=1.000; geomean -0.89% = noise). Document the toggle cost in the README (new section) and the WakeOnCancel doc comment: - off (default): ~46 ns/op, 176 B/op, 3 allocs/op per Subscribe - on: ~566 ns/op, 696 B/op, 9 allocs/op, +1 parked goroutine per subscriber until ctx done -- Subscribe-time only, Write untouched Co-Authored-By: GLM 5.3 <noreply@z.ai>
Two changes from peer review (Opus 5.5 + Muse): 1. Write()/Close() now hold rb.mu around the writePos store / close + cond.Broadcast. Before, a broadcast landing between a reader check and its sync.Cond.Wait() park fired at zero waiters -- the reader parked forever (intermittent CI flake, ~1/15). The fill loop stays lock-free. Measured: single-item Write 3.23 -> 4.11 ns/op (+27%, 0 allocs); batch unchanged; fan-out throughput unchanged at 10k subs. 2. WakeOnCancel no longer registers context.AfterFunc per subscriber -- the dropped stop func leaked parent-ctx child-map entries for subscribers that outlived their registration (EOF, ErrTooSlow), and a mass cancel ran one broadcast per canceled subscriber: O(N^2) re-parks on the same rb.mu that Write() needs. Instead: one shared reaper goroutine per ring buffer, ticking every StreamReaperInterval (250ms) while at least one subscriber opted in, broadcasting under rb.mu. A subscriber releases on its terminal Read() error or via Close(); the reaper stops when the last opted-in subscriber is gone. Measured: Subscribe ~84ns/4 allocs on vs ~55ns/3 off (was 566ns/9). Co-Authored-By: DeepSeek V4.1 Flash <noreply@deepseek.com>
|
I don't like this. Closing. 🤖 The real finding — both flagged the eager AfterFunc as flawed
I will come up with a different fix myself. |
|
Closing as superseded — splitting into two smaller PRs off master:
The WakeOnCancel design in this PR didn't survive review: the eager context.AfterFunc leaks parent-ctx child-map entries (dropped stop()) and mass-cancels storm O(N^2) on rb.mu. Thanks for the review pressure that surfaced both. 🤖 This message was generated by pi using GLM 5.3 on behalf of Vojtech. |
Problem
Two bugs, one old and one mine:
Write()/Close()calledcond.Broadcast()without holdingrb.mu. A reader checkswritePos/closedunderrb.mu, then parks insync.Cond.Wait()— which adds it to the notify list only after the check. A broadcast landing in that window fires at zero waiters → the reader parks forever. Intermittent (~1/15 locally,TestSubscriberDrainsClosedStreamin CI), reproduced on base master.context.AfterFuncin the first iteration of this PR: droppedstop()leaked parent-ctx child-map entries for subscribers that outlive their registration (EOF,ErrTooSlow), and a mass cancel ran one broadcast per canceled subscriber — O(N²) re-parks serialized on the samerb.muthe fixedWrite()needs.Both found by peer review (two independent reviewers); both fixed here.
Fix 1: hold
rb.muaround store/close + BroadcastWrite()andClose()now broadcast underrb.mu(the fill loop stays lock-free). This restores thesync.Condinvariant: no broadcast can land between a reader's re-check and its park.Measured (full suite, M4 Pro):
Correctness beats nanoseconds: there is no way to keep
Writefully lock-free withsync.Cond— the predicate-mutation-under-muinvariant is structural. The mutex is forever uncontended (single writer).Fix 2:
WakeOnCancelis a shared reaper, not per-subscriberOne reaper goroutine per
RingBuffer, ticking everyStreamReaperInterval(default 250 ms) only while at least one subscriber opted in, broadcasting underrb.mu— so canceled parked readers exit via the existingcheckEndpath within one interval. Zero per-subscriber goroutines, zero parent-ctx map entries, no cancel storm. A subscriber releases on its terminalRead()error or via the new idempotentClose(); the reaper stops when the last opted-in subscriber is gone.Measured (
BenchmarkSubscribe):WakeOnCancelThe extra alloc is
cancelCtx.Done()'s lazy channel from thectx.Done() != nilguard, not the reaper.Tests
TestReaperStopsAfterLastOptedInSubscriber,TestWakeOnCancelNoReaperByDefault,TestCloseStopsReaperBeforeReadEnds(internal, no sleeps — asserts reaper exit deterministically).-race -count=2green;go vetclean.Caveats (honest)
Read()without a terminal error and never callsClose()— keeps the reaper alive. Documented onClose().StreamReaperInterval(250 ms default), not instantly — that is the design's bounded staleness.count=1runs are indicative; the single-itemWrite+27% was reproduced across three runs per tree.🤖 This message was generated by pi using GLM 5.3 on behalf of Vojtech.