Skip to content

[logger] Never block the caller on writes to stderr - #1481

Merged
manugarg merged 2 commits into
mainfrom
logger-defer-debug
Sep 11, 2026
Merged

manugarg merged 2 commits into
mainfrom
logger-defer-debug

Conversation

@manugarg

Copy link
Copy Markdown
Contributor

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_total stepped down to zero over about 9 minutes while the node's logs went dark.

Change

  • Loggers created without WithWriter now write to stderr through a new asyncWriter. A single goroutine writes the queued entries in order. Write copies its input and never blocks.
  • If the queue fills up (4 MiB), new entries are dropped and counted. A WARN entry with dropped_entries=N goes just before the next accepted entry, so the gap is marked in the log stream.
  • Critical logs, and main's exit paths, flush the queue before exiting. The flush has a timeout, so a stalled stderr can't block the exit either.
  • Writers passed with WithWriter stay 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

  • Unit tests for a blocked writer (writes don't block, drops are counted, order is preserved, the notice lands at the gap, Flush times out), for the input copy, and for concurrent writes. They pass 30 times in a row under -race, and the full test suite passes.
  • Manual run: an HTTP probe with 20 failing targets at a 100ms interval, with stderr going to a FIFO whose reader never reads.
    • Before: probing froze within seconds, with no exports at all over 35s.
    • After: probing ran at full rate, with 100 → 200 → 300 per target across three 10s exports. Only the drain goroutine was blocked in pipe_write.

Follow-ups

  • Export the drop count as a sysvar metric. Metrics kept flowing during the incident while logs were dark, so this metric would have made the stall visible.
  • Route the raw_stderr_output relays in the external and command probes through the same writer.

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.
Comment thread logger/asyncwriter.go Resolved
Comment thread logger/asyncwriter.go Resolved
Comment thread logger/asyncwriter.go Resolved
Comment thread logger/asyncwriter.go Resolved
@manugarg
manugarg merged commit c60862f into main Sep 11, 2026
10 of 11 checks passed
@manugarg
manugarg deleted the logger-defer-debug branch September 11, 2026 23:17
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant