mac_unified_logging: fix infinite json.Decoder error loop (sustained ~113% CPU, silent collection stall)#305
Open
amrik-lc wants to merge 1 commit into
Open
Conversation
The gatherer dropped the first line of `log stream` output via a throwaway bufio.Reader, which could buffer and discard up to 64KB beyond that line. The json.Decoder then started mid-object and hit a SyntaxError, which json.Decoder never advances past: the loop spun forever printing the same error (sustained ~113% CPU) while the stdout pipe filled and `log stream` blocked permanently, so no logs were collected at all until restart. Parse the ndjson output line by line with a bufio.Scanner instead and skip lines that fail to parse (including the non-JSON header line, so the drop-first-line hack is no longer needed). A bad line can now never wedge the stream. Also: - Close logs.Channel when the stream ends so the adapter shuts down instead of hanging silently if `log stream` dies. - Make StopGathering close-based (idempotent, non-blocking) and kill the subprocess so a reader blocked in Scan() unblocks. - Report the actual stderr line instead of panicking with a stale nil err from the enclosing scope. - Buffer the signal.Notify channel (pre-existing go vet finding). Co-Authored-By: Claude Fable 5 <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.
Fixes a bug in the
mac_unified_loggingadapter observed in the field as sustained ~113% CPU on macOS — with the worse side effect that the adapter had silently stopped collecting logs entirely. Tracked in refractionPOINT/tracking#4458.Root cause
StartGatheringdropped the first line oflog stream --style=ndjsonoutput using a throwawaybufio.Reader, which buffers (and then discards) up to 64KB beyond that first line. Depending on startup timing, thejson.Decodercreated afterwards starts reading mid-JSON-object and returns aSyntaxError. Go'sjson.Decodernever advances its input past a syntax error, so the read loop degenerated into a permanent hot loop:Decode+ twofmt.Printlnper iteration, forever → ~1 core pegged (plus GC churn)log streamblocks forever inwrite()→ zero events ingested until the process is restartedDiagnosed from a process sample (all on-CPU time under
StartGathering.func1→fmt.Fprintln/encoding/json.(*SyntaxError).Error) and a spindump showing thelogchild's output thread blocked inwrite()since the moment it was forked, days earlier.Fix
bufio.Scanner(4MB max line) andjson.Unmarshaleach line, skipping lines that fail to parse. The non-JSON"Filtering the log data..."header is skipped naturally, so the racy drop-first-line hack is gone. A bad line can now never wedge the stream.logs.Channelwhen the stream ends, so the adapter terminates (and can be restarted by its supervisor) instead of hanging silently iflog streamdies.StopGatheringclose-based (sync.Once+close(exit)): idempotent and non-blocking, where the old unbuffered send could block forever if the gatherer wasn't at theselect. A watcher goroutine kills the subprocess on stop so a reader blocked inScan()unblocks.cmd.Start()'s error and return it fromStartGathering(was previously ignored inside the goroutine).panic(err)on a stalenilerror from the enclosing scope (which produced a meaninglesspanic(nil)).client.go: buffer thesignal.Notifychannel (pre-existinggo vetfinding; unbuffered signal channels can drop the signal).Verification
gofmt -lclean;go buildandgo vetpass forGOOS=darwin GOARCH=arm64andGOOS=linuxon the package.🤖 Generated with Claude Code