Repository navigation
[logger] Never block the caller on writes to stderr - #1481
Merged
Merged
Conversation
If whatever reads the other end of stderr stalls (e.g. the container runtime's log writer on a node with a stuck disk), a write to stderr blocks indefinitely. Every goroutine that logs then blocks behind it, including probe goroutines, so probing gradually stops. Loggers that don't use WithWriter now write to stderr through an asyncWriter: entries are queued and written, in order, by a single goroutine. If the queue (4 MiB) fills up, new entries are dropped and counted, and a notice with the drop count precedes the next accepted entry. Critical logs and main's exit paths flush the queue with a timeout, so a stalled stderr can't block exiting either.
- Rename Flush to Wait (and the package-level Flush to WaitForStderr): it only waits for the drain goroutine and doesn't write anything itself. - Check the deadline in the loop condition, via a pendingBytes() helper, so it's clear that Wait never sleeps once the deadline has passed. Test Wait(0). - Comment the non-blocking wake-up of the drain goroutine, the ignored write error, and what 4 << 20 is.
manugarg
commented
Sep 11, 2026
manugarg
added a commit
that referenced
this pull request
Sep 12, 2026
…async writer (#1482) Follow-up to #1481. logger.DroppedEntries() exposes the async writer's drop counter, and sysvars exports it as the cumulative metric dropped_log_entries: metrics kept flowing during the incident while logs were dark, so this is the signal that makes a stderr stall visible. The external and command probes' raw_stderr_output relays now write to logger.Stderr() instead of os.Stderr, so a stalled stderr can't block the relay goroutine or, through it, the probe's child process. DroppedEntries() and WaitForStderr() only observe the stderr writer and never start it, so polling them can't create a drain goroutine in processes that never log to stderr.
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.
If whatever reads the other end of stderr stalls (for example, the container runtime's log writer on a node with a stuck disk), a write to stderr blocks indefinitely. Every goroutine that logs then blocks behind that one write, including probe goroutines, so probing gradually stops. We saw this in production:
cloudprober_totalstepped down to zero over about 9 minutes while the node's logs went dark.Change
WithWriternow write to stderr through a newasyncWriter. A single goroutine writes the queued entries in order.Writecopies its input and never blocks.dropped_entries=Ngoes just before the next accepted entry, so the gap is marked in the log stream.main's exit paths, flush the queue before exiting. The flush has a timeout, so a stalled stderr can't block the exit either.WithWriterstay synchronous.This follows zerolog's
diode.Writer(the input copy, drops reported in the stream) and Docker's non-blocking log mode (a byte budget, dropping the newest entries). It's a buffered queue with no new dependencies.Testing
Flushtimes out), for the input copy, and for concurrent writes. They pass 30 times in a row under-race, and the full test suite passes.pipe_write.Follow-ups
raw_stderr_outputrelays in the external and command probes through the same writer.