fix: auto-detect schedule_reinit and tighten termination guard#5083
Merged
Conversation
--abort-reinit flag for automatic module reinitialization7498784 to
ecfa5e4
Compare
b6d6d15 to
0b1e7fc
Compare
Auto-detect the __wbindgen_reinit intrinsic so schedule_reinit() emits private reinit machinery without --experimental-reset-state-function. Keep the experimental flag only for the public __wbg_reset_state() export and instance-id tracking. Tighten termination handling to distinguish hard termination from scheduled reinit, preserve the sentinel through __wbg_handle_catch, and invoke the abort hook for host-initiated termination before re-checking the flag. Rename reinit() to schedule_reinit() and update coverage and guide docs accordingly.
0b1e7fc to
b340bf1
Compare
…to boolean Decouples reinit from the abort path — schedule_reinit() no longer triggers a Wasm trap, so the current call completes normally and the reset happens on the next export call. The abort-then-reinit path still works: the abort hook fires, calls schedule_reinit(), and the guard resets instead of throwing. The __instance_terminated address is now a simple boolean (0/1). Reinit state is tracked via a private __wbg_reinit_scheduled JS variable. Also removes the imports_without_catch_wrapper mechanism since the reinit intrinsic no longer writes to the terminated flag.
b58b38f to
62c86ea
Compare
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.
This disables the auto-reinit functionality of aborts with
--experimental-reset-state-functionto instead require an explicit call to theschedule_reinit()function. In addition, any use ofschedule_reinit()will automatically bring in the reinit machinery by treating it as an intrinsic.schedule_reinit()sets a private JS-side flag instead of writing to a__instance_terminatedsentinel value. This means:schedule_reinit()can be called during normal execution without triggering a trap — the current call completes normally and the reset happens on the next export callschedule_reinit(), and the guard resets instead of throwing__instance_terminatedis a clean boolean (0= live,1= terminated) with no internal sentinel values leaked into the public specUsage for abort on reinit:
A new guide section is added "Handling Aborts" covering these behaviours as a separate guide page to "Catching Panics".
What changed:
reinit()toschedule_reinit()to reflect that it schedules reinitialization for the next export call__wbindgen_reinitintrinsic now sets a JS variable (__wbg_reinit_scheduled) instead of writing-1to linear memory__wbg_handle_catchunconditionally writes1(no conditional to preserve a sentinel)__wbg_reset_stateonly references__wbg_called_abort/__wbg_reinit_scheduledwhen the catch handler machinery is active, fixing aReferenceErroronpanic=abortbuilds with--experimental-reset-state-functionschedule_reinit()usage emits private reinit machinery without--experimental-reset-state-function__instance_terminateddocumented as boolean,schedule_reinitdocs note it works outside abort contexton_reinitfor now, now that we support multiple start functions insteadTest coverage:
termination,termination_abort_handler,termination_reset_state,termination_reinit, andtermination_reinit_auto_detectall pass.