run GL teardown on the refresh thread that owns the context (fixes segfault on power-off / window close)#44
Merged
Conversation
…gfault on power-off and window close
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a macOS crash during guest power-off (halt) and window close by ensuring OpenGL teardown (e.g., glDeleteTextures via compositor destruction) runs on the REX3-Refresh thread that owns the GL context, rather than on whichever thread calls Rex3::stop().
Changes:
- Move
renderer.stop()intoRex3::refresh_loop()so GL resource destruction happens on the context-owning refresh thread. - Remove the foreign-thread
renderer.stop()call fromRex3::stop(). - Add a
rules/gui/document codifying the “all GL calls belong to the refresh thread” invariant.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/rex3.rs | Relocates renderer teardown to the refresh thread after the loop exits; removes teardown from stop() to avoid foreign-thread GL calls. |
| rules/gui/gl-teardown-must-run-on-the-refresh-thread.md | Documents the thread-affinity rule for GL calls and explains the original crash mechanism and fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Owner
|
yup makes sense. gl context is bound to thread. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
irissegfaults on macOS when the guest powers off (IRIXhalt), and on the same code path when the window is closed. The crashing thread ismachine-events, inglDeleteTextures:EXC_BAD_ACCESS (SIGSEGV),KERN_INVALID_ADDRESS at 0x1e0— i.e. a dereference of a null current-context plus a small field offset, not a freed/garbage pointer.Root cause
The GL context is created and made current lazily by
GlRenderer::init_gl, which only ever runs frompresent()— andpresent()is only called fromRex3::refresh_loop(the REX3-Refresh thread). That thread is the sole owner of the context; theunsafe impl Send for GlRenderereven documents this ("sent to the refresh thread where it owns and uses the GL context. No other thread touches these fields").Rex3::stop()joined (terminated) the refresh thread and then calledrenderer.stop()→Compositor::destroy(&state.gl)→glDeleteTexturesfrom whatever thread invokedstop()—machine-eventson guest power-off, the main thread on window close (main.rsafter the winit loop returns). On macOS, GL dispatches through the calling thread's current context; with none current, the call faults.This was a broad latent bug, not specific to power-off. Headless/CI paths escaped only because
state == Nonemakes the teardown a silent no-op.Fix
Tear the renderer down at the end of
refresh_loop— on the thread that owns the context, while it is still current — and drop therenderer.stop()call fromRex3::stop(). Ordering is preserved:Rex3::stop()still joins the refresh thread, so teardown completes beforestop()returns. BecauseMachine::stopstops the CPU first and the processor thread is joined before the refresh thread, the GFIFO is fully drained, the fence-wait spin falls through, the loop observesrunning == false, exits, and runs teardown. Afterreset,restart_peripherals()→rex3.start()respawns the refresh thread andpresent()re-inits GL lazily — no black screen.Known related issue (not addressed here)
disp compositor gl|sw→GlRenderer::switch_compositordestroys GL resources directly on the monitor thread (ui.rs), same foreign-thread fault, rarely triggered. A proper fix routes the compositor swap through the refresh thread; left out of scope to keep this change focused.Notes / testing
--features lightning,rex-jit,tlbvmap(only pre-existing warnings).halt(the soft power-off that crashed), and separately close the window — both hitrex3.stop()and both are fixed.rules/gui/note documenting the "all GL calls belong to the refresh thread" invariant, per the repo'srules/convention.