Make the Windows handler reentrancy-safe (fix "already borrowed" panic on resize)#263
Open
aidan729 wants to merge 1 commit into
Open
Make the Windows handler reentrancy-safe (fix "already borrowed" panic on resize)#263aidan729 wants to merge 1 commit into
aidan729 wants to merge 1 commit into
Conversation
handle_on_frame and handle_event hold self.handler.borrow_mut() across the entire user callback. If that callback synchronously triggers another window message — e.g. a SetWindowPos or host-driven resize that sends WM_SIZE straight back into the window procedure — the wndproc re-enters handle_event, borrows the handler a second time, and panics with a BorrowMutError. In an audio-plugin context that unwinds across the FFI boundary and crashes the host. Use ry_borrow_mut() and skip the re-entrant call instead of panicking. The handler isn't reentrant, so dropping the nested invocation is correct: window state stays consistent and the next genuine frame/event picks up the latest size. This makes it safe to call host resize APIs (which synchronously re-enter the window proc on Windows) from inside a handler callback.
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
On Windows,
WindowState::handle_on_frameandhandle_eventholdself.handler.borrow_mut()for the full duration of the user'son_frame/on_eventcallback. If the callback causes Windows to synchronously dispatchanother message into the window procedure, the wndproc re-enters
handle_event,calls
borrow_mut()again, and panics withalready borrowed: BorrowMutError.The most common trigger is resizing: calling a host's resize request (or a
SetWindowPos) from inside a handler callback synchronously sendsWM_SIZEbackinto the window proc. In an audio-plugin host this panic unwinds across the FFI
boundary and crashes the DAW.
This is reproducible whenever a baseview-hosted GUI tries to resize its own
window in response to user interaction (e.g. a draggable resize affordance that
asks the host to grow the editor).
Fix
Use
try_borrow_mut()in bothhandle_on_frameandhandle_eventand skip theevent if the handler is already borrowed, instead of panicking. The handler is
not reentrant, so dropping the nested call is the correct behavior — the window
state remains consistent and the next genuine frame/event observes the latest
size.
This brings the Windows backend in line with how non-reentrant event loops are
expected to behave and makes it safe to invoke host resize APIs from within a
handler callback.
Scope
src/win/window.rs), two call sites.re-entrant callback is now skipped instead of panicking.