fix: guard Debug.proc against concurrent CPU-percent reads#8
Open
aguung wants to merge 1 commit into
Open
Conversation
gopsutil's Process.Percent tracks the previous CPU-time reading on the *Process itself with no internal locking. /api/debug is polled every 2s by the TopBar widget in every open tab, so overlapping calls (e.g. more than one tab open, or a slow response overlapping the next tick) raced on that state and could report a corrupted, wildly inflated cpu_percent (reproduced up to ~135000% under concurrent load; -race confirms the data race). Serialize access to proc with a mutex.
3 tasks
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
/api/debug'scpu_percentcould report wildly inflated, nonsensical values (spotted in practice around 96.6%, peaking well over 100%) under normal usage.Debug.proc, a single shared*process.Process, caused by concurrent calls toPercent(0).procwith a mutex.Problem
server/handlers/api_debug.go'sDebug.Getcallsh.proc.Percent(0)(gopsutil) to compute CPU%.Percenttracks the previous CPU-time reading as unsynchronized fields on the*process.Processitself (seegopsutil/v4/process/process.go'sPercentWithContext) — there's no internal locking./api/debugis polled every 2s by the TopBar widget (web/src/os/SystemStats.tsx) in every open tab. With more than one tab open, or a response that overlaps the next 2s tick, two calls toGetcan run concurrently and race on that shared state.Debug.Getproduced acpu_percentup to 135758.9%, andgo test -raceflags a genuine data race at the exact read/write inPercentWithContext.Solution
mu sync.MutextoDebugand serialize theh.proc.Percent(0)/h.proc.MemoryInfo()calls insideGetwith it. Both read/mutate state on the same*process.Process, so both are covered.Changed files
server/handlers/api_debug.gomu sync.MutextoDebug; locks around theh.proccalls inGet.server/handlers/api_debug_test.goTestDebugConcurrentAccess— fires 20 goroutines × 20 concurrent calls toGet; meaningful undergo test -race(fails with "WARNING: DATA RACE" before this fix, clean after).Test plan
go test -race -run TestDebugConcurrentAccess ./server/handlers/...reliably reports a data race.go build ./...andgo test ./...pass with no regressions (41 packages).curlrequests to/api/debugwhile idle all returned sanecpu_percentvalues (0–1.2%), no crash, no corrupted output.