fix: resolve concurrency bugs in eventbus and webtransport#3518
Conversation
There was a problem hiding this comment.
Thanks for this. I'd been debugging the same bug myself, so I'm glad you beat me to it :)
I pushed two small test commits on top:
- Moved the eventbus slow-consumer test over to synctest. It runs on a fake clock now, so it's fast and won't flake on CI the way the sleep-based version could.
- Added two tests for
SerializedCertHashes: one checks the getter returns a copy, the other runs it against cert rotation under-race.
@MarcoPolo fwiw lgtm, and Kubo could benefit from this, but your call
459c98c to
267126b
Compare
Replace the wall-clock TestWildcardSlowConsumerDeadlock with a synctest version that fires the warning timeout on a fake clock, so it runs instantly and cannot flake on a loaded CI runner. Add a close-during- stall test covering the safety valve where closing a subscription releases an emit parked under the wildcard node read lock.
Add a test that the getter returns an independent copy, and a -race test that hammers it during background certificate rotation. Both fail on the unsynchronized getter and pass with the read lock and copy.
267126b to
efc1305
Compare
48af017 to
d557644
Compare
50d34c0 to
38167d7
Compare
use slices.DeleteFunc ensure draining goroutine returns don't use metrics tracer to test behavior Assisted-By: Claude Fable 5
38167d7 to
8c6fee9
Compare
Document what SerializedCertHashes promises, and make the tests check it. The concurrent test only ever guarded the read lock: with the copy removed but the lock kept, it passed every run under -race. Its clear() probe lowers to runtime.memclrHasPointers, which carries no race instrumentation, so the detector never saw the write. An indexed store is instrumented, and now kills both mutants. - godoc: the caller owns the outer slice, the hash bytes are shared with the manager and read-only - TestSerializedCertHashesReturnsOuterCopy fails without the copy on any run, no race detector needed - yield in the reader loop, dropping the concurrent test from 8.0s to 1.1s at GOMAXPROCS=1 under -race
Make these tests fail loudly instead of hanging. A require failure inside a synctest bubble unwinds the root goroutine, and the deferred emitter close then parks on the node lock a stalled emit still holds. Closing the subscription before asserting turns a timeout hang into an immediate failure that names the subscription that broke. - TestEmitLogsErrorOnStall: close, wait, then assert; derive the sleep from slowConsumerWarningTimeout rather than a bare 3s - TestWildcardCloseUnblocksStalledEmit: a goroutine blocked on an RWMutex is not durably blocked, so synctest cannot call it a deadlock; say so, and send the warning to io.Discard
lidel
left a comment
There was a problem hiding this comment.
SGTM, good to ship from my side.
@sukunrt fyi pushed two commits on top of your removeSink cleanup that make tests more tight:
-
dffa0f3 test(webtransport): assert cert hash ownership
The concurrent test only guarded the read lock:clear()on a[][]bytecompiles toruntime.memclrHasPointers, which has no race instrumentation, so-racenever saw the write. An indexed store does, and now catches a missing clone too. Also brought back an ownership test, scoped to the outer slice thatslices.Cloneactually copies. -
4f6c46c test(eventbus): tighten slow-consumer assertions
A failedrequirein a synctest bubble unwinds into the deferred emitter close, which parks on the node lock a stalled emit still holds, so failures hung until-timeoutrather than failing. Closing the sub first fixes it. Also yielded in the rotation reader loop: 8s atGOMAXPROCS=1and 2.2s on a 4 vCPU runner, now 1.1s.
Issue
observed two concurrency issues:
slowConsumerTimerpointer. Concurrent emitters raced onReset(), causing the timer to fire only once. One emitter consumed the tick and blocked on the channel, leaving other emitters permanently blocked on<-timer.Ceven after the queue was drained. This hang occurred under a read-lock (RLock), blocking any subscription closure (sub.Close()) or node updates from acquiring a write-lock.SerializedCertHashes()getter read the sharedm.serializedCertHashesslice without acquiring a lock. This caused a read/write data race with the background cert manager thread which updates the slice during certificate rotation. Returning the raw slice by reference also caused slice aliasing, exposing the caller to backing memory being concurrently mutated/resized.Fix
slowConsumerTimerpointer field fromnodeandwildcardNode. The slow consumer warning logic was refactored to use a localtime.Timerallocated on the stack withinemitAndLogError, eliminating shared timer state across concurrent emitters.m.mx.RLock()toSerializedCertHashes()and returned a deep copy of the slice rather than a direct reference to avoid data races and slice aliasing.Tests
TestWildcardSlowConsumerDeadlockinp2p/host/eventbus/basic_test.gowhich simulates a full queue under concurrent emissions to verify that no deadlocks occur and the emitters safely proceed when the queue is drained.