Repository navigation
Description
cargo test -p jcode-tui --lib intermittently fails 2 to 6 tests, always a shifting subset. --test-threads=1 is reliably green: 1974 passed / 0 failed. So these are isolation failures, not real regressions.
Cause
Rendering reaches process-global state: render snapshots, scroll metrics, flicker history, prompt positions, and the thread-locals cleared by clear_test_render_state_for_tests in crates/jcode-tui/src/tui/ui.rs. Any two tests that render concurrently can therefore observe each other's state.
I already fixed one layer of this (5b38b2d): viewport_snapshot_test_lock and scroll_render_test_lock each defined their own private mutex, so they serialized within each helper but not against each other, which is the same defect as #593. Both now delegate to a single ui::render_state_test_lock(). That took the parallel failure count from 6 down to 2 to 4.
The remaining problem is coverage rather than correctness of the lock. Most rendering tests never take it:
| file | tests | locked |
|---|---|---|
ui_tests/tools.rs |
47 | 0 |
ui_tests/rendering.rs |
25 | 0 |
ui_tests/input_layout.rs |
25 | 0 |
ui_tests/body_cache.rs |
24 | 0 |
ui_tests/prepare.rs |
23 | 0 |
ui_tests/basic/frame_flicker.rs |
17 | 10 |
ui_tests/image_regions.rs |
7 | 0 |
That is roughly 180 rendering tests contending on shared globals with essentially no mutual exclusion. test_changelog_overlay_repeated_renders_are_stable illustrates it: it does hold the shared lock and still fails in parallel, because unlocked renderers mutate the same globals underneath it.
Options
- Blanket the lock. Mechanical: take
render_state_test_lock()at the top of every rendering test. Correct but ~180 edits, and it silently serializes a large slice of the suite, so wall-clock cost goes up and the requirement is easy for future tests to forget. - Make the state injectable. Thread a render-context value through instead of reaching for globals, so tests are hermetic by construction and can run in parallel. Bigger change, removes the class permanently.
- Interim: run this crate single-threaded in CI while option 2 lands.
I deliberately did not do option 1 unilaterally: a change that large to test structure, with a real throughput cost, is a call for whoever owns this area. Option 2 is the same recommendation I made on #593, and the recurrence across two crates suggests the underlying pattern of tests reaching global state is worth addressing directly rather than serializing around each time.
Not affected
jcode-app-core is stable at 1024 passed / 0 failed across 14 consecutive parallel runs after #593.
Activity
1jehuang commentedon Jul 25, 2026
I tried option 1 (blanket the lock) so the tradeoff is measured rather than assumed. Two concrete findings.
The throughput cost is small. Measured on this machine:
--test-threads=1So full serialization costs about 14 seconds. That is cheap enough that "correctness beats throughput" is the right call, and it weakens the main argument I gave against option 1.
But the mechanical version of option 1 does not work. I inserted
render_state_test_lock()at the top of 110 rendering tests across 7 files. The suite then hung rather than failed:render_state_test_lockis a plain non-reentrantMutex, and many of these tests call shared helpers that already acquire it, so the second acquisition self-deadlocks. This is exactly the trap I hit while fixing #593, where 23selfdevtests each held two guards of the same mutex.I reverted those 110 insertions; the suite is back to 1974 passed / 0 failed single-threaded.
What this means for the options. Option 1 is not a mechanical sweep. Doing it properly requires auditing which helpers already lock and either (a) pushing the lock strictly into the leaf helpers and out of the tests, or (b) making the guard reentrant/token-based so nesting is safe. Both are real design work, which is why I am still not doing it unilaterally.
Given the measured 14-second cost, option 3 (run this crate single-threaded in CI) is a much better interim than I first suggested — it is one line, it is correct today, and it costs little. That would make CI trustworthy immediately while option 2 (injectable render context) is considered on its merits rather than under pressure from a red suite.
Happy to implement whichever you prefer, including option 1 done properly if you want the lock pushed into the leaves.
1jehuang commentedon Jul 25, 2026
One more finding that reframes the urgency, and it is worth knowing independently of this issue.
CI never runs these tests.
.github/workflows/ci.ymlonly compiles them:The only lib tests actually executed are a single targeted cohort (
-p jcode-app-core --lib retention_readiness), plus theprovider_matrixande2eintegration suites. So no CI job would have caught the 22 deterministicjcode-tuifailures I fixed in #594, nor the near-total parallel failure ofjcode-app-coreI fixed in #593. They compiled, so CI stayed green.That changes the priority ordering here:
I would suggest the real sequencing is: get the workspace lib tests running in CI, which then makes the parallel-isolation work in this issue genuinely necessary (and makes option 2, injectable render state, clearly worth the investment rather than optional polish).
Both suites are green today (
jcode-app-core1024/0 parallel,jcode-tui1974/0 single-threaded), so this is a good moment to turn them on before they drift again. I have not made that change: enabling ~3000 tests in CI affects runner time and failure policy for everyone, so it is your call. Happy to implement it, starting single-threaded forjcode-tuiso it is green from day one, if you want.