Skip to content

Flaky test: export_falls_back_to_the_cpu_when_gpu_work_is_lost reads process-wide GPU fallback state other tests overwrite #675

Closed

Description

@cajax

Summary

crates/engine/src/tests_export.rs::export_falls_back_to_the_cpu_when_gpu_work_is_lost fails intermittently when
the whole engine library test binary runs, on main too (1 of 2 full cargo test --workspace --lib runs at 5436951,
and 4 of 6 on a branch based on it). It passes every time on its own. The fallback works. The test reads GPU state
that every other test in the process shares.

thread 'tests_export::export_falls_back_to_the_cpu_when_gpu_work_is_lost' panicked at crates/engine/src/tests_export.rs:198:9:
device failure during the render: stopped after a GPU failure: a GPU render panicked          (main)
GPU render: out of device memory: Out of Memory                                                 (branch)

Why

crates/gpu/src/lib.rs keeps some of the fallback state per thread and some for the whole process:

  • FAULT (from inject_fault) is thread-local, so the injected DropWork only hits this test's own next render.
  • LAST_FALLBACK ("the latest render that fell back from the GPU to the CPU and why"), BROKEN and BROKEN_REASON
    are process-wide statics, and reset_failures() clears all three.

The test injects the fault, exports (falls back), then asserts last_fallback().contains("incomplete") and that the
GPU is now off for the process. Meanwhile the other engine tests in the same binary are rendering on the GPU in
parallel. When one of their renders falls back for its own reason, it overwrites LAST_FALLBACK between this test's
export and its check. That reason was out of device memory under the parallel load, or once "a GPU render panicked".
The test also turns the GPU off for every test running at that moment, and reset_failures() at its end can clear
another test's genuine broken state.

Fix

Either:

  • Ask about this render, not the last one in the process. For example, have the export or render report its own
    fallback reason (the render path knows it when it falls back; today only the latest one in the process is kept, which is what ui.inspect → gpuFallback reports),
    and assert on that. That makes the API more useful too, since callers can attribute a fallback to their own render.
  • Give the test its own process. Move it to an integration test (crates/engine/tests/gpu_fallback.rs), as
    crates/gpu/tests/fallback.rs already is. It then can't see or disturb other tests' GPU state.
  • At minimum, serialize every engine test that touches the process-wide GPU state (fault injection,
    reset_failures, last_fallback, available) behind one test-only mutex.

Worth a look on its own

When this test fails, the reason it read is another test's fallback. One run showed "a GPU render panicked": a
panic inside the GPU render path, caught by its catch_unwind (crates/gpu/src/lib.rs render) and turned into a
CPU fallback. The safety net worked. But under the project's never-crash rule a panic there is a bug in itself, even
when caught. It's suspected to appear only under heavy parallel GPU use (many test renders at once). Which test's
render panicked wasn't identified; running the engine lib tests with RUST_BACKTRACE=1 and looking for the panic
message should find it.

Reproduce

for i in 1 2 3 4; do cargo test -q --workspace --lib 2>&1 | grep -E "export_falls_back_to_the_cpu.*FAILED|panicked at crates/engine/src/tests_export"; done
cargo test -q --workspace --lib -- export_falls_back_to_the_cpu   # alone: passes

Needs a working GPU (Vulkan/Metal/DX12). Seen on Linux with an NVIDIA RTX 500 Ada, driver 595.91.07.

Found while working on #345 (PR #601), which doesn't touch GPU or export code.

🤖 Generated with Claude Code

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions