fix(rpc): close WaitGroup Add/Wait race in Engine#1776
Merged
Conversation
Do checked the atomic closed flag and then called wg.Add(1) as a separate step, while Close stored the flag and called wg.Wait(). Between Do's check and its Add, Close could mark the engine closed and have Wait return on a zero counter, then Do would Add(1) — violating the WaitGroup contract that an Add raising the counter from zero must happen-before Wait. This is the data race the detector reported between Engine.Close and Engine.Do. Guard the close check and wg.Add together under the existing mux so they cannot interleave with Close. closed becomes a mux-guarded bool; Close sets it under the lock and waits outside it. Removes the now-unused isClosed helper. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1776 +/- ##
==========================================
+ Coverage 71.10% 71.23% +0.12%
==========================================
Files 501 503 +2
Lines 23501 23577 +76
==========================================
+ Hits 16711 16795 +84
+ Misses 5569 5550 -19
- Partials 1221 1232 +11 ☔ View full report in Codecov by Harness. 🚀 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.
Summary
Fixes a data race between
Engine.CloseandEngine.Doreported by the race detector (seen e.g. ingotd/botapi'sTestRunEndToEnd).Dochecked the atomicclosedflag and then callede.wg.Add(1)as a separate step, whileClosestored the flag and callede.wg.Wait(). BetweenDo's check and itsAdd,Closecould mark the engine closed and haveWaitreturn on a zero counter, after whichDowould callAdd(1)— violating thesync.WaitGroupcontract that anAddraising the counter from zero must happen-beforeWait.Fix
Guard the close-check and
wg.Add(1)together under the existingmuxso they cannot interleave withClose:closedbecomes amux-guardedbool(was an atomicuint32).Dotakesmux, returnsErrEngineClosedif closed, elsewg.Add(1), then unlocks.Closesetsclosed = trueundermux, thenwg.Wait()outside the lock (so pending requests can stillDone).isClosedhelper.Testing
go test -race -count=1 ./rpc/... ./mtproto/...passes.🤖 Generated with Claude Code