fix(updates): prevent deadlock when relaying channel difference - #1716
Merged
Conversation
The internalState worker is the only reader of internalQueue, yet it can block in applyCombined -> handleChannel -> channelState.Push while writing into a channel's bounded updates queue. Meanwhile the channelState worker blocks in getDifference writing diff.OtherUpdates into internalQueue, which is full precisely because the internalState worker is stuck. Both selects only release on ctx.Done(), so update processing hangs until shutdown. Add channelState.sendOut, which keeps draining the channel's own input queue while waiting for the worker to accept the hand-off, guaranteeing the worker can always complete its Push and return to drain internalQueue. This reuses the same drain-and-ignore pattern already used in the diffTimeout wait; dropped updates are recovered by a future getChannelDifference call. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1716 +/- ##
==========================================
- Coverage 69.74% 69.66% -0.08%
==========================================
Files 464 464
Lines 20216 20234 +18
==========================================
- Hits 14100 14097 -3
- Misses 5064 5084 +20
- Partials 1052 1053 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A circular wait can hang update processing between the two worker goroutines:
internalStateworker (mainRunloop) is the only reader ofinternalQueue. While processing a batch inapplyCombined→handleChannel→channelState.Push, it blocks writing into a channel's boundedupdatesqueue (cap 10) when full.channelStateworker, insidegetDifference, blocks writingdiff.OtherUpdatesintos.out(=internalQueue, cap 10) when full — and it's full precisely because the main worker is stuck above and not draining it.Both selects only release on
ctx.Done(), so updates stop flowing until shutdown.The same hazard is already mitigated in the
diffTimeoutwait branch ofgetDifference("Ignoring updates to prevent *internalState worker from blocking"), but the actuals.out <-hand-off had no such protection.Fix
Add
channelState.sendOut, which keeps draining the channel's own input queue while waiting for the worker to accept the hand-off. This guarantees the main worker can always complete itsPushand return to its select to draininternalQueue, so the cycle can no longer form.This reuses the existing drain-and-ignore pattern; dropped updates are recovered by a future
getChannelDifferencecall (or the idle timeout), so there is no permanent loss.Safety
getDifference/sendOutrun only on the singlechannelState.Rungoroutine, so there is no concurrent read ofs.updatesand no new data race (verified with-race).s.outnow has exactly one send site.diffTimeoutdrain and is recoverable via the gap mechanism.🤖 Generated with Claude Code