Skip to content

GPU: a failed allocation fails the frame, not the context (#1812) - #1919

Closed
ded-achtung wants to merge 1 commit into
storytold:mainfrom
ded-achtung:g5-gpu-failed-alloc
Closed

ded-achtung wants to merge 1 commit into
storytold:mainfrom
ded-achtung:g5-gpu-failed-alloc

Conversation

@ded-achtung

@ded-achtung ded-achtung commented Oct 20, 2026 •

Copy link
Copy Markdown

Found while testing #1812. One allocation failure retired the GPU for the rest of the session, and every later frame rendered on the CPU.

Problem

Gpu::within_memory catches OutOfMemory and Internal errors, and the preview then retries the frame on the CPU with fewer GPU frames kept. That is the intended path from #106 and #591.

A texture whose allocation failed is invalid, though. Its next use, a create_view or a copy in the same render, is a validation error. Nothing caught that error, so it reached the uncaptured-error handler and retired the context:

ERROR [ec-frame-7] effectcraft: uncaptured GPU error: Validation Error
  In Texture::create_view
    Texture with '' label is invalid
WARN  [ec-frame-14] effectcraft_ui_egui::frames: the GPU context is retired (...); the frame renders on the CPU

On the #1812 project this happened in most cold-start playbacks: 16 workers allocate at once while the caches are empty.

Change

within_memory also pushes a Validation error scope. Scopes are thread-local in wgpu 30, so this only catches errors from the guarded work. Out-of-memory is still reported first. The frame then fails the way an out-of-memory frame already does: the CPU renders it, uploads are cleared, and the GPU keeps working.

Result

Desktop playback of the #1812 project, 10 runs, together with #1917 and #1918:

  • the GPU context was never retired;
  • a few cold-start frames (0–6 per run) fell back to the CPU, as designed;
  • the first pass ran at 68–84 % of real time.

Test

within_memory_catches_the_use_of_a_failed_texture creates a texture over the size limit and makes a view of it inside the guard. It checks that the work returns an error, that check_health is still Ok, and that the next guarded work succeeds. On main the test fails, because the uncaptured error retires the context.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • GPU memory-guard operations now report validation failures in addition to memory and internal errors.
    • Failed texture allocations are handled without compromising the GPU context, allowing subsequent valid allocations to succeed.
  • Documentation

    • Clarified that allocation failures may surface as validation errors and can retire the context if left uncaught.

…ld#1812)

within_memory caught out-of-memory and internal errors, but the next use of the invalid texture
(a view, a copy) is a validation error; uncaught, it retired the GPU context for the session
and every later frame rendered on the CPU. Validation errors are now caught in the same scope:
the frame retries on the CPU and the GPU keeps going.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a93d62fb-9eea-4a36-9f7f-d214d61b83e0
📥 Commits

Reviewing files that changed from the base of the PR and between ef74d82 and 6d47d7a.

📒 Files selected for processing: 2
  • crates/gpu/src/lib.rs
  • crates/gpu/src/tests.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

📝 Walkthrough

within_memory now captures validation errors alongside out-of-memory and internal errors. A regression test checks that an oversized texture allocation returns an error and that the GPU context can still perform a valid allocation.

Changes: GPU memory guard

Layer / File(s) Summary
Capture and test allocation errors
crates/gpu/src/lib.rs, crates/gpu/src/tests.rs
within_memory resolves a validation error scope and checks errors in out-of-memory, internal, then validation order. Its documentation describes validation errors after failed texture allocations. The regression test checks that an oversized allocation returns an error and that the context remains usable for a later valid allocation.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: echelon

Merge Risk: ⚪ Minimal · up to 6d47d

No concrete PR-introduced merge-blocking risk remains.

Security architecture risk: ⚪ Minimal · up to 6d47d

A failed GPU operation now rejects the frame while preserving the existing CPU fallback and device-health checks. The reviewed change introduces no identified material security exposure.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed exposure is failure handling for existing native rendering work and its shared GPU caches. The inspected delta adds no caller, privilege or external entry point; it changes whether guarded validation errors reject a frame rather than reach context-level handling.

Trust Boundaries and Controls

  • observed — The selected native backend maintains error scopes per thread and requires reverse-order removal. The guard follows that order, so adding validation capture does not create a device-wide scope that consumes unrelated workers' errors.

Resilience and Maintainability Implications

  • observed — The selected dependency automatically pops unconsumed error-scope guards when they are dropped. Consequently, a panic unwinding through within_memory does not leave the newly added validation scope attached to a worker thread. The existing worker-level panic catcher can then attempt CPU rendering.
Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly summarizes the main change: failed GPU allocations fail the frame without retiring the GPU context.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

🔧 Clippy (1.98.1)

Clippy execution timed out

Comment @coderabbitai help to get the list of available commands.

@ded-achtung

Copy link
Copy Markdown
Author

Closing as a duplicate of #1326 (@tylerjenningsw, opened Oct 13 for #1319), which I missed when I opened this. It adds the same Validation scope to within_memory and goes further: it discards pooled textures from a failed frame, labels working textures, and keeps validation failures apart from out-of-memory. I'll post what the #1812 project showed over there.

🤖 Generated with Claude Code

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.

1 participant