Skip to content

xcl run: echo log lines on a thread of their own, never blocking a caller - #3430

Draft
vakaobr wants to merge 1 commit into
xcl-dev:mainfrom
vakaobr:xcl-run/log-echo-never-blocks
Draft

vakaobr wants to merge 1 commit into
xcl-dev:mainfrom
vakaobr:xcl-run/log-echo-never-blocks

Conversation

@vakaobr

@vakaobr vakaobr commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

log::write copies every Info line to stderr on the thread that logged it, after the file line. When nothing reads stderr, that write blocks: a pipe whose reader stopped, or a Windows console with text selected in it. Each thread that logs then stops right after its file line, including the title's own threads inside a console call, and the title freezes with nothing in the log to say why. Part of #258.

What is in it

  • crates/xcl/src/log.rs: the stderr copy goes through a bounded queue (4096 lines) to an xcl-stderr thread, so a caller never waits. With the queue full a line is dropped and counted, and the next line copied says how many were left out. The file write is unchanged and still comes first. log::flush gives the copy up to 200 ms to catch up.
  • New test an_unread_stderr_does_not_stop_the_thread_that_logs: runs the test binary again as a child whose stderr is a pipe the test never reads; the child logs 4000 lines and must exit within 30 s. It fails without the change ("logging stopped on a stderr nobody reads").

Tested

  • M5 Pro, macOS 27.2.1, CrossOver 26.3 (Wine) with D3DMetal, Rust 1.100, x86_64-pc-windows-gnu; head 4a99fce on main 9dfbe3e.
  • log::tests (debug, --test-threads=4): 2 passed, 0 failed. With the old blocking echo put back, an_unread_stderr_does_not_stop_the_thread_that_logs fails after 30 s.
  • The test title presenting on the 60 Hz frame clock with stderr to a pipe nobody reads: before, frames stopped about 2 s in and xcl.log ended at 69 lines; after, 240 s at 60 fps to the end, 402 log lines, exit code 0 (measured on my Mac branch with the same change).
  • Windows: andrewDoing's native harness check above (Ryzen 7 5700X3D, MSVC).

Regression run

Not tested

  • Windows (any GPU), the whole suite, a retail title.

AI use

  • Claude Code with Claude Opus 5.5: reproduced the freeze with the test title, wrote the change and the test, ran them on my Mac, and drafted this description. I checked it before opening.

@andrewDoing

Copy link
Copy Markdown
Contributor

Native Windows check on Ryzen 7 5700X3D, Rust 1.100.0 MSVC. I compiled the log.rs modules unchanged at bc2cbb72 and 218bc95b in a small native harness that imports the same Win32 calls. Both stock logger tests passed on the PR, serially.

The separate overflow control used four producers, 8,192 numbered Info lines and a parent process that deliberately left the child's stderr pipe unread until the producers and flush() had returned. The base reached 50 file lines and was still blocked at the 3 s watchdog. On the PR, producers finished in 60–67 ms across the runs and the bounded flush returned in 201–205 ms. Every file held all 8,192 unique IDs. After resuming the reader, stderr held 4,143 unique messages plus a summary of 4,049 dropped echoes. The totals reconciled on every run.

One scope detail for the test suite: a direct Rust eprintln! in the same process still waited through a 150 ms observation and completed once the reader resumed. The stock unread-stderr test also restored the process-wide stderr handle while queued lines remained, and some lines printed after the test reported completion. An isolated child would keep that handle change and its pending writes away from parallel tests. This concerns direct stderr calls. The log::write producers completed before the reader resumed.

This exercises the logger and native Windows pipe behavior without a GPU or retail smoke run. AI disclosure: OpenAI Codex (GPT-6.2 Sol) implemented and ran the process harness and drafted this report.

…ller

log::write echoed every Info line to stderr synchronously, on whichever
thread logged, after the file line. A stderr that nobody reads blocks
that write: a pipe whose reader stopped, or a Windows console with text
selected. Each thread that logged then stopped right after its file
line, including the title's own threads inside a console call, and the
title froze with nothing in the log to say why.

The echo now goes through a bounded queue to an "xcl-stderr" thread. A
caller never waits: with the queue full (4096 lines) the line is
dropped and counted, and the next line echoed says how many were left
out. The file write is unchanged and still comes first. log::flush
gives the echo up to 200 ms to catch up, so lines from the end of a run
still reach the console.

Measured on an M5 Pro, macOS 27.2.1, CrossOver 26.3 + D3DMetal, with the
test title presenting 1920x1080 frames on the 60 Hz frame clock and
stderr connected to a pipe whose reader never reads:
- before: the title's frames stopped about 2 s in; xcl.log ended at 69
  lines, each logging thread right after one line;
- after: 240 s at 60 fps to the end, 402 log lines, exit code 0.
an_unread_stderr_does_not_stop_the_thread_that_logs (new) runs this
test binary again as a child whose stderr is a pipe the test never
reads; the child logs 4000 lines and must exit within 30 s. It fails
before this change ("logging stopped on a stderr nobody reads") and
passes after; the blocked pipe and the queued lines stay in the child.
@vakaobr
vakaobr force-pushed the xcl-run/log-echo-never-blocks branch from 218bc95 to 4a99fce Compare November 30, 2026 11:17
@vakaobr

vakaobr commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

@andrewDoing thanks for the native check, and for the point about the test. I changed it as you suggested (4a99fce): the logging now runs in a child process of the test binary whose stderr is a pipe the test never reads, so neither the blocked pipe nor the queued lines reach the other tests. On a Mac (CrossOver 26.3), log::tests passes running 4 at a time, and no child line shows up in the test output any more. With the old blocking echo put back, the test fails after its 30 s limit (logging stopped on a stderr nobody reads (child: None)). Direct eprintln! calls are outside this PR, as you say.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head 4a99fce, diff read, not a merge-check; draft, PR description red, no Windows run):

  • flush() can now cost 200 ms every time (log.rs ~134-138). It sleeps until the echo queue is empty or 200 ms pass. Some paths always pay the full 200 ms:

    • end_title (real.rs ~4897 on main) has already suspended the xcl-stderr thread, so what the drain logs is never echoed.
    • At DLL_PROCESS_DETACH (loader.rs ~434) ExitProcess has already ended the echo thread, and this sleep then runs under the loader lock.
    • With a blocked console (the case this PR is for), the stop-file path, which flushes 4-5 times, adds about a second.

    Skipping the wait when the echo thread can't run, or making it a single wait at exit, would avoid that. What was still queued at a crash or at exit doesn't reach the console. The file has it, but the description should say so.

  • The "N lines not echoed" note (~108-112) is printed before the next queued line, which is older than the dropped ones, so it shows up early in the console.

  • The queue is bounded at 4096 lines, not bytes. Some lines are several KB (the thread dump at eh.rs ~986), so a long-paused console can hold several MB.

  • The test logs 4000 lines, fewer than the queue holds, so the drop path and the note are never run. Nothing checks that the file still gets every line.

  • Still missing: a Windows suite run and the smoke pair (this is a runtime change). It will conflict textually with xcl run: report log write failures once #2634, xcl run: say on stderr when the log or report.json cannot be written (#2277) #2286 and Tests: pin the process logger's level filter and file line format #1589, which edit the same code.

Fine: the file write is unchanged and still comes first, so xcl.log keeps its order. try_send never blocks a caller, and the echo thread never logs, so it can't deadlock on itself. The thread is spawned lazily without holding a log lock, a failed spawn just skips the echo, and the pending count is kept right on a failed send. The test runs in a child process, so its blocked pipe doesn't affect other tests.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 4a99fce

No open or merged PR moves the stderr echo off the logging thread; #2286 and #2634 only add stderr messages when the log cannot be written, and #1831 adds a separate error writer. Part of #258.
The test exercises the change: ECHO defaults to true, and the child writes through std::io::stderr(), which test output capture does not intercept.
Minor: lines the echo thread writes can now reach stderr after direct eprintln! output written later (e.g. host start errors), and log::flush waits up to 200 ms whenever stderr is blocked; its callers are exit and watchdog paths, so that is bounded.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

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.

3 participants