Skip to content

optimize(logging): make logging asynchronous to prevent slow output from blocking requests - #5248

Open
caikun-pjlab wants to merge 1 commit into
InternLM:mainfrom
caikun-pjlab:optimize/async_logging
Open

caikun-pjlab wants to merge 1 commit into
InternLM:mainfrom
caikun-pjlab:optimize/async_logging

Conversation

@caikun-pjlab

Copy link
Copy Markdown
Collaborator

Motivation

Synchronous logging can stall request processing when stdout is redirected to slow or overloaded storage, such as GPFS. Uvicorn emits access logs before sending response headers, so a blocked log write can significantly delay /health, even when the model is healthy.

This also affects the dedicated monitoring server: its separate event loop still uses the same logging infrastructure.

Changes

  • Introduce asynchronous logging with a bounded queue of 8192 records per handler and a background writer.
  • Move output I/O off request threads. Writers process queued records immediately; there is no batching interval.
  • Drop records when the queue is full or output fails, without a synchronous stderr fallback.
  • Format records before enqueueing to preserve the values of mutable arguments and other record fields at logging time.
  • Use private output descriptors and daemon workers so blocked logging does not hold stdout’s buffered-stream lock or require draining the queue during shutdown.
  • Apply asynchronous handlers to application and Uvicorn logs, including the dedicated monitoring server.
  • Replace diagnostic print calls with logger calls while preserving interactive, report, and example output.

Existing logger formatting is preserved.

Benefits

  • Health checks and inference requests remain responsive when log output stalls.
  • Bounded queues prevent unlimited accumulation of pending records.
  • Logging resumes after the output destination recovers.
  • Log delivery is best effort: availability takes priority over retaining every log record.

Performance and validation

Validated with a deployed GLM-5.3-Flash service using DP=8 and EP=8, plus its router.

To simulate blocked stdout writes, the test paused consumption of a 4096-byte stdout pipe. Kernel pipe_write waits confirmed that logging was actually blocked. This simulates output backpressure; it is not a GPFS benchmark.

For the dedicated monitoring endpoint, :9100/health, each phase used 9600 requests with 24 concurrent clients:

Output state Successful requests P50 P99 Maximum
Normal 9600/9600 5.79 ms 6.40 ms 50.62 ms
Blocked 9600/9600 5.25 ms 5.79 ms 10.34 ms
Recovered 9600/9600 5.82 ms 6.52 ms 11.06 ms

During approximately 17.6 seconds of blocked output:

  • Queue saturation dropped 1363 access-log records without failing requests.
  • Health checks succeeded across all eight monitoring ports, eight API ports, and the router.
  • Direct and router-mediated inference requests both completed successfully.
  • After recovery, pending logs drained and new access logs appeared.

A separate test of the regular API /health endpoint also completed 9600/9600 requests during blocked output, with 8.08 ms P99 latency.

Trade-offs

Queued records may be lost on overflow, output failure, or process exit, including error-level records. Formatting still runs on the calling thread; the asynchronous boundary covers output I/O. Native and third-party logging that bypasses these handlers is outside this change’s scope.

Use bounded 8192-entry queues and daemon writers for LMDeploy and Uvicorn logs. Snapshot formatted text before enqueueing, silently drop records on saturation or output failure, and avoid waiting for blocked output during shutdown.

Replace diagnostic print calls with logger calls while preserving interactive and report output. Cover mutable arguments, lock contention, fork, blocked output, shutdown, and API/monitoring health responses.

Validation: 55 focused tests passed; pre-commit checks passed.

This branch has not been deployed

No deployments
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