Shaders: dxc::cached_only leaks TLS on panic (and console_mesh bypasses it), and xcl shaders --direct leaks detached threads / mislabels panics as timeouts #1896

Open
opened 2026-11-26 05:08:37 +00:00 by github-bot · 3 comments
Owner

@⁠microwave805 on GitHub · 2026-11-24 22:37 UTC · original

Static audit on main @ 689f2db found three reliability issues in shader compilation probes and the xcl shaders --direct CLI harness:

1. dxc::cached_only() leaks CACHED_ONLY = true thread-local state on panic

  • Where: crates/xcl/src/gpu/recomp/dxc.rs:876-882
  • Root cause:
    pub fn cached_only<R>(f: impl FnOnce() -> R) -> (R, bool) {
        CACHED_ONLY.with(|c| c.set(true));
        MISSED.with(|m| m.set(false));
        let r = f();
        CACHED_ONLY.with(|c| c.set(false));
        (r, MISSED.with(|m| m.replace(false)))
    }
    
    If f() panics and is caught by an outer catch_unwind (or test runner), CACHED_ONLY stays stuck at true on that thread forever, causing every subsequent dxc::compile_cached / dxc::defer() call on that thread to silently fail as a cache miss instead of compiling.
  • Fix direction: Restore CACHED_ONLY (to its previous value, supporting nesting too) in an RAII Drop guard.

2. pso::console_mesh() bypasses dxc::cached_only() during cold Direct DXIL mesh builds

  • Where: crates/xcl/src/gpu/recomp/pso.rs:750-762
  • Root cause: When XCL_DIRECT_DXIL=1 is enabled and a mesh pipeline is probed under dxc::cached_only() (non-blocking PSO creation), console_mesh() immediately runs kernel::translate_mesh() on the caller's thread without checking dxc::defer(), blocking the render thread on cold mesh translation. (Note: open PR #1788 adds dxc::defer() for compute PSO cache probes, but does not cover console_mesh().)
  • Fix direction: Check dxc::defer() before running cold translate_mesh() when not already cached (matching #1788's compute path).

3. xcl shaders --direct leaks unbounded detached 256 MiB-stack threads on timeouts and mislabels panics as timeouts

  • Where: crates/xcl/src/shaders.rs:1925-1953
  • Root cause:
    • For every program, --direct (and --direct --lanes) spawns a detached std::thread::spawn and waits on rx.recv_timeout(timeout). Inside translate_mode / compute_with, translation itself spawns a 256 MiB-stack worker thread. When multiple programs hit timeouts in a whole-pack sweep, each timed-out thread (plus its 256 MiB stack) remains running and detached with no concurrency cap or cancellation, exhausting virtual/physical memory and CPU.
    • At shaders.rs:1931 and 1942:
      rx.recv_timeout(timeout).unwrap_or_else(|_| Err(format!("timeout: over {} s", timeout.as_secs())))
      
      unwrap_or_else collapses both RecvTimeoutError::Timeout AND RecvTimeoutError::Disconnected (which happens when the spawned thread panics before tx.send(...), e.g. in compute_with which has no outer catch_unwind on line 1940) into "timeout: over N s", masking real panics as timeouts!
  • Fix direction: Match RecvTimeoutError::Timeout vs RecvTimeoutError::Disconnected (reporting "panic" on disconnect, and wrapping compute_with in catch_unwind), and bound active timed-out worker slots (or share a bounded pool / cooperative cancellation flag inside Lw / opt).

AI use

  • Antigravity (Gemini 4 Neon): static code audit on main @ 689f2db and issue drafting.
**@⁠microwave805** on GitHub · 2026-11-24 22:37 UTC · [original](https://github.com/xcl-dev/xcl/issues/1896) Static audit on `main @ 689f2db` found three reliability issues in shader compilation probes and the `xcl shaders --direct` CLI harness: ### 1. `dxc::cached_only()` leaks `CACHED_ONLY = true` thread-local state on panic - **Where:** `crates/xcl/src/gpu/recomp/dxc.rs:876-882` - **Root cause:** ```rust pub fn cached_only<R>(f: impl FnOnce() -> R) -> (R, bool) { CACHED_ONLY.with(|c| c.set(true)); MISSED.with(|m| m.set(false)); let r = f(); CACHED_ONLY.with(|c| c.set(false)); (r, MISSED.with(|m| m.replace(false))) } ``` If `f()` panics and is caught by an outer `catch_unwind` (or test runner), `CACHED_ONLY` stays stuck at `true` on that thread forever, causing every subsequent `dxc::compile_cached` / `dxc::defer()` call on that thread to silently fail as a cache miss instead of compiling. - **Fix direction:** Restore `CACHED_ONLY` (to its previous value, supporting nesting too) in an RAII `Drop` guard. ### 2. `pso::console_mesh()` bypasses `dxc::cached_only()` during cold Direct DXIL mesh builds - **Where:** `crates/xcl/src/gpu/recomp/pso.rs:750-762` - **Root cause:** When `XCL_DIRECT_DXIL=1` is enabled and a mesh pipeline is probed under `dxc::cached_only()` (non-blocking PSO creation), `console_mesh()` immediately runs `kernel::translate_mesh()` on the caller's thread without checking `dxc::defer()`, blocking the render thread on cold mesh translation. *(Note: open PR `#1788` adds `dxc::defer()` for compute PSO cache probes, but does not cover `console_mesh()`.)* - **Fix direction:** Check `dxc::defer()` before running cold `translate_mesh()` when not already cached (matching `#1788`'s compute path). ### 3. `xcl shaders --direct` leaks unbounded detached 256 MiB-stack threads on timeouts and mislabels panics as timeouts - **Where:** `crates/xcl/src/shaders.rs:1925-1953` - **Root cause:** - For every program, `--direct` (and `--direct --lanes`) spawns a detached `std::thread::spawn` and waits on `rx.recv_timeout(timeout)`. Inside `translate_mode` / `compute_with`, translation itself spawns a 256 MiB-stack worker thread. When multiple programs hit timeouts in a whole-pack sweep, each timed-out thread (plus its 256 MiB stack) remains running and detached with no concurrency cap or cancellation, exhausting virtual/physical memory and CPU. - At `shaders.rs:1931` and `1942`: ```rust rx.recv_timeout(timeout).unwrap_or_else(|_| Err(format!("timeout: over {} s", timeout.as_secs()))) ``` `unwrap_or_else` collapses both `RecvTimeoutError::Timeout` AND `RecvTimeoutError::Disconnected` (which happens when the spawned thread panics before `tx.send(...)`, e.g. in `compute_with` which has no outer `catch_unwind` on line 1940) into `"timeout: over N s"`, masking real panics as timeouts! - **Fix direction:** Match `RecvTimeoutError::Timeout` vs `RecvTimeoutError::Disconnected` (reporting `"panic"` on disconnect, and wrapping `compute_with` in `catch_unwind`), and bound active timed-out worker slots (or share a bounded pool / cooperative cancellation flag inside `Lw` / `opt`). ### AI use - Antigravity (Gemini 4 Neon): static code audit on `main @ 689f2db` and issue drafting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:08:37 +00:00
Author
Owner

@⁠JJAutoCompare on GitHub · 2026-11-25 00:36 UTC · original

Local regression results for item 3. A patch on base commit 8c4a46a3c2d50498691cbda79cbbf4fadb45fbe8 makes both xcl shaders --direct and --direct --lanes report a worker that exits without replying as panic: shader worker disconnected. An expired wait still reports timeout: over N s.

cargo test --locked -p xcl --lib shaders::tests::direct_worker_panic_is_distinct_from_timeout -- --exact ran in debug mode on Windows 11 build 26200, Ryzen 7 3800X, Intel Arc A770 (driver 32.0.101.9175), and Rust 1.100.0 MSVC. The test requires no GPU. With the old mapping it failed because the panic was classified as Timeout. With the fix restored, 1 passed, 0 failed, 0 ignored, and 0 skipped. git diff --check passed.

The patch is local and uncommitted. Full workspace tests, shader-pack sweeps, and retail smoke runs remain unverified.

AI use: Codex in T3 Code (gpt-6.2-sol) wrote the patch and regression test, ran the checks, and drafted this report.

**@⁠JJAutoCompare** on GitHub · 2026-11-25 00:36 UTC · [original](https://github.com/xcl-dev/xcl/issues/1896#issuecomment-7148682164) Local regression results for item 3. A patch on base commit `8c4a46a3c2d50498691cbda79cbbf4fadb45fbe8` makes both `xcl shaders --direct` and `--direct --lanes` report a worker that exits without replying as `panic: shader worker disconnected`. An expired wait still reports `timeout: over N s`. `cargo test --locked -p xcl --lib shaders::tests::direct_worker_panic_is_distinct_from_timeout -- --exact` ran in debug mode on Windows 11 build 26200, Ryzen 7 3800X, Intel Arc A770 (driver 32.0.101.9175), and Rust 1.100.0 MSVC. The test requires no GPU. With the old mapping it failed because the panic was classified as `Timeout`. With the fix restored, 1 passed, 0 failed, 0 ignored, and 0 skipped. `git diff --check` passed. The patch is local and uncommitted. Full workspace tests, shader-pack sweeps, and retail smoke runs remain unverified. AI use: Codex in T3 Code (gpt-6.2-sol) wrote the patch and regression test, ran the checks, and drafted this report.
Author
Owner

@⁠JJAutoCompare on GitHub · 2026-11-25 00:32 UTC · original · edited 2026-11-25 00:41 UTC

Follow-up to the targeted diagnostic test report. #1997 covers the same panic/timeout distinction and adds a limit on concurrent timed-out translations, so the narrower local patch can be set aside. I left a review on #1997 about releasing a worker slot before publishing its result. The cached-only mesh probe is outside both changes.

**@⁠JJAutoCompare** on GitHub · 2026-11-25 00:32 UTC · [original](https://github.com/xcl-dev/xcl/issues/1896#issuecomment-7148608301) · edited 2026-11-25 00:41 UTC Follow-up to the [targeted diagnostic test report](https://git.gtahub.st/github/xcl-dev--xcl/issues/1896). #1997 covers the same panic/timeout distinction and adds a limit on concurrent timed-out translations, so the narrower local patch can be set aside. I left a [review on #1997](https://git.gtahub.st/github/xcl-dev--xcl/pulls/1997) about releasing a worker slot before publishing its result. The cached-only mesh probe is outside both changes.
Author
Owner

@⁠Decodere on GitHub · 2026-11-25 03:57 UTC · original

Taking the item 2 follow-up for console_geometry() (crates/xcl/src/gpu/recomp/pso.rs, Part of #1269 / related to #1896): add if dxc::defer() { return Err("DXIL not compiled yet".into()); } on the direct DXIL path before kernel::translate_geometry (matching compile_console_shader / compile_console_mesh), with a deterministic synthetic dxc::cached_only regression test.

AI use: Google Antigravity (Gemini 4 Neon).

**@⁠Decodere** on GitHub · 2026-11-25 03:57 UTC · [original](https://github.com/xcl-dev/xcl/issues/1896#issuecomment-7152391834) Taking the item 2 follow-up for `console_geometry()` (`crates/xcl/src/gpu/recomp/pso.rs`, Part of #1269 / related to #1896): add `if dxc::defer() { return Err("DXIL not compiled yet".into()); }` on the direct DXIL path before `kernel::translate_geometry` (matching `compile_console_shader` / `compile_console_mesh`), with a deterministic synthetic `dxc::cached_only` regression test. AI use: Google Antigravity (Gemini 4 Neon).
This discussion has been locked. Commenting is limited to contributors.
No description provided.