Repository navigation
topics: read particle.retainPath under the particle lock in scanMessages (fixes a data race with RetainMessage) - #539
Open
ChristophNetsch wants to merge 1 commit into
Conversation
…Messages RetainMessage writes particle.retainPath under the root and particle locks; scanMessages (a subscriber's retained-message scan, via Messages) read it with no lock at all. A retained publish racing a wildcard SUBSCRIBE is a data race the race detector reports as: Write at ... by (*TopicsIndex).RetainMessage topics.go:461 Previous read at ... by (*TopicsIndex).scanMessages topics.go:554 This is mochi-mqtt#200, closed as not reproducible; the added test reproduces it deterministically under -race and passes with the two reads locked. (cherry picked from commit 8b986f0)
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.
What races
TopicsIndex.RetainMessagewritesparticle.retainPathwhile holding the root lock and the particle's own lock (topics.go:461, and the clearing write at:471).TopicsIndex.scanMessages— the retained-message scan a SUBSCRIBE triggers throughMessages()/publishRetainedToClient— readsadjacent.retainPathandparticle.retainPathwith no lock at all (topics.go:554,:573). A retained publish that coincides with a wildcard subscribe on an overlapping filter is therefore a data race. Under-race:This is the race #200 describes; it was closed as not reproducible. The test added here reproduces it deterministically: one goroutine retains and clears messages under
a/b/+while the test thread scansa/#,a/b/+and an exact filter. On the currentmainit fails under-racewith the report above on every run; with the fix it passes.The fix
Read
retainPathunder the particle's lock at both sites inscanMessages, copying the string out before use. The scan already takes no root lock, and it takes only one particle lock at a time and briefly, so there is no lock-order interaction withRetainMessage(root → particle) and no new contention beyond the two short critical sections per visited particle. Behaviour is unchanged: the scan sees either the previous or the newretainPath, which was already the intended semantics; it just no longer sees a torn read.Cost
Two uncontended mutex acquisitions per scanned particle. No allocation, no API change.
Test
TestRetainMessageWhileScanningDoesNotRaceintopics_test.go. Run withgo test -race -run TestRetainMessageWhileScanningDoesNotRace ./— fails onmain, passes with this change.