Fix race condition on global network rate state#3
Open
hobostay wants to merge 1 commit intosumant1122:mainfrom
Open
Fix race condition on global network rate state#3hobostay wants to merge 1 commit intosumant1122:mainfrom
hobostay wants to merge 1 commit intosumant1122:mainfrom
Conversation
The global variables `netPrevTotal` and `netPrevAt` are read and written
by `getNetRateKB()`, which is called from both `SampleMetrics()` and
`SampleSystem()`. In model.go, these are invoked as batched tea.Cmds:
tea.Batch(sampleMetricsCmd(), sampleSystemCmd())
Since batched commands run concurrently in separate goroutines, both
goroutines can call `getNetRateKB()` simultaneously, creating a data
race on the unprotected global state.
This can cause incorrect network rate calculations, sporadic panics, or
crashes detected by the Go race detector (`go test -race`).
Add a sync.Mutex to serialize access to the global network state.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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 on the global variables
netPrevTotalandnetPrevAtininternal/monitor/monitor.go.Bug
getNetRateKB()reads and writes two package-level variables without synchronization:This function is called from both
SampleMetrics()andgetNetSummary()→SampleSystem(). Inmodel.go, these are dispatched as batched tea.Cmds:Batched commands in Bubble Tea execute concurrently in separate goroutines, so both goroutines can call
getNetRateKB()at the same time, creating a data race on the unprotected globals.This can cause:
go test -raceFix
Add a
sync.Mutexto serialize access to the global network state ingetNetRateKB().How to verify
🤖 Generated with Claude Code