Skip to content

jcode-tui render tests share process-global state with almost no locking (parallel-only failures) #595

Open

Description

@1jehuang

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

  1. 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.
  2. 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.
  3. 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

  1. added
    bug
    priority: medium
    triage: reproducible
    tech-debt
    on Jul 25, 2026
  2. 1jehuang commented on Jul 25, 2026

    @1jehuang
    OwnerAuthor

    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:

    mode result time
    parallel 1970-1971 passed, 3-4 failed 11.7-12.3s
    --test-threads=1 1974 passed, 0 failed 25.7-26.2s

    So 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_lock is a plain non-reentrant Mutex, 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 23 selfdev tests 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.

  3. 1jehuang commented on Jul 25, 2026

    @1jehuang
    OwnerAuthor

    One more finding that reframes the urgency, and it is worth knowing independently of this issue.

    CI never runs these tests. .github/workflows/ci.yml only compiles them:

    - name: Compile library and binary tests
      run: cargo test --target ... --lib --bins --no-run

    The only lib tests actually executed are a single targeted cohort (-p jcode-app-core --lib retention_readiness), plus the provider_matrix and e2e integration suites. So no CI job would have caught the 22 deterministic jcode-tui failures I fixed in #594, nor the near-total parallel failure of jcode-app-core I fixed in #593. They compiled, so CI stayed green.

    That changes the priority ordering here:

    1. The parallel-isolation problem in this issue is not blocking CI, because CI does not run the suite. So option 3 is unnecessary as a CI fix.
    2. The larger gap is that ~3000 library tests across the workspace are compiled but never executed in CI. That is the reason two suites were able to rot to 22 and ~20 failures without anyone noticing.

    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-core 1024/0 parallel, jcode-tui 1974/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 for jcode-tui so it is green from day one, if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority: mediumtech-debttriage: reproducible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions