fix: add mutex to Cloudability emitter for concurrent Emit() safety#154
Open
fix: add mutex to Cloudability emitter for concurrent Emit() safety#154
Conversation
Adds sync.Mutex to the Cloudability Emitter struct to protect mutable state (sampleCt, currentSamplePath, nextSamplePath, lastEmission) from concurrent access when Emit() is called from overlapping goroutines in the export loop. Also protects ClearOldScratchSamples() which calls Uploader.RemoveSample() and races with AddSample() in Emit(). Follow-up to #151. Minimal change: one import, one struct field, three lines in two methods. Init() and ID() are not locked — Init() runs sequentially before the loop, ID() returns a constant. Includes concurrency test that validates under -race.
Split ClearOldScratchSamples into public (locked) and private (unlocked) methods to prevent deadlock when called from within Emit()'s lock scope. The call chain Emit() -> writeStatsData() -> writeStatsFile() -> ClearOldScratchSamples() would previously deadlock because sync.Mutex is not reentrant in Go. Now writeStatsFile() calls clearOldScratchSamplesLocked() which assumes the caller already holds the lock, while external callers use the public ClearOldScratchSamples() which acquires the lock.
daniel-spray
approved these changes
Feb 24, 2026
Collaborator
daniel-spray
left a comment
There was a problem hiding this comment.
LGTM, thank you! Let's run it on uw2p-akp-j1 for 24-48hrs just to ensure we don't find anything unexpected :)
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.
Adds sync.Mutex to the Cloudability Emitter struct to protect mutable state (sampleCt, currentSamplePath, nextSamplePath, lastEmission) from concurrent access when Emit() is called from overlapping goroutines in the export loop.
Also protects ClearOldScratchSamples() which calls Uploader.RemoveSample() and races with AddSample() in Emit().
Follow-up to #151. Minimal change: one import, one struct field, three lines in two methods.
Init() and ID() are not locked — Init() runs sequentially before the loop, ID() returns a constant.
Includes concurrency test that validates under -race.