Skip to content

fix(wgpu): reset gpu_ready on close + re-init (use-after-free guard) - #427

Merged
apotema merged 1 commit into
mainfrom
fix/wgpu-gpu-ready-reset
Jun 30, 2026
Merged

apotema merged 1 commit into
mainfrom
fix/wgpu-gpu-ready-reset

Conversation

@apotema

@apotema apotema commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Addresses the gemini High on #426. gpu_ready stayed true after closeWindow freed the GPU resources (sprite buffers/pipeline → null), so a stray endDrawing/ensureSurface after close — or a re-init whose initGpu fails — would pass the if (!gpu_ready) return guard and touch freed handles (panic / UB). Reset to false in closeWindow (authoritative: resources freed) and at the top of initWindow (initGpu sets it true on success, so a failed re-init stays not-ready). Completes the re-init state hygiene from #425. wgpu example builds (macOS Metal).

Summary by CodeRabbit

  • Bug Fixes
    • Improved window lifecycle handling to avoid rendering with partially initialized or already released GPU resources.
    • Ensured graphics state is reset when the window is initialized or closed, reducing the chance of crashes or invalid drawing after failed setup or shutdown.

gpu_ready stayed true after closeWindow released the GPU resources (sprite
buffers/pipeline → null), so a stray endDrawing/ensureSurface post-close — or a
re-init whose initGpu fails — would pass the `if (!gpu_ready)` guard and touch
freed handles. Reset it to false in closeWindow (resources freed) and at the top
of initWindow (initGpu sets it true on success). gemini High on #426; completes
the re-init state hygiene started in #425.
@coderabbitai

coderabbitai Bot commented Jun 30, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 6d9e6621-b986-425c-b457-3a6b9a1d0fa1

📥 Commits

Reviewing files that changed from the base of the PR and between e5c2815 and 8165238.

📒 Files selected for processing (1)
  • backends/wgpu/src/window.zig

📝 Walkthrough

Walkthrough

In backends/wgpu/src/window.zig, gpu_ready is now explicitly set to false at two points: during initWindow state reset (before initGpu() runs) and at the end of closeWindow after GPU resources are released and GLFW is terminated.

Changes

gpu_ready lifecycle management

Layer / File(s) Summary
gpu_ready guards in init and close
backends/wgpu/src/window.zig
Sets gpu_ready = false on initWindow state reset and after GPU teardown in closeWindow to prevent rendering against uninitialized or freed GPU handles.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A flag set false, a guard in place,
No freed GPU shall render's grace.
Init resets, close does too,
The rabbit hops and sees it through!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main fix: resetting gpu_ready on close and re-init to prevent use-after-free.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 PR with unit tests
  • Commit unit tests in branch fix/wgpu-gpu-ready-reset

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

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates backends/wgpu/src/window.zig to set gpu_ready = false during window initialization and closure to prevent drawing calls on freed handles. The reviewer noted that setting gpu_ready = false at the end of closeWindow() creates a vulnerability where re-entrant events or callbacks could access partially freed resources, and recommended moving this assignment to the beginning of the function.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +737 to +740
// The GPU resources above are released — mark not-ready so a stray
// `endDrawing`/`ensureSurface` after close (or before a re-init's `initGpu`)
// hits the `if (!gpu_ready) return` guard instead of touching freed handles.
gpu_ready = false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Setting gpu_ready = false at the very end of closeWindow() leaves a window of vulnerability. During the release of GPU resources and GLFW termination, callbacks or re-entrant events could be triggered. If these callbacks attempt to draw or update the surface, they will see gpu_ready as true while resources are partially freed, leading to undefined behavior or panics.

It is much safer to set gpu_ready = false at the very beginning of closeWindow() (before releasing any resources) to immediately gate any re-entrant calls. Please move this assignment and its comment to the top of the closeWindow() function.

@apotema
apotema merged commit a12cf5b into main Jun 30, 2026
4 checks passed
@apotema
apotema deleted the fix/wgpu-gpu-ready-reset branch June 30, 2026 16:19
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