Wait for v86 emulator-loaded before restoring state#367
Merged
Conversation
The 500ms setTimeout raced against v86's async wasm/BIOS/hda load. When loading exceeded 500ms (cold cache, Gatekeeper scan on packaged apps), restore_state ran on an uninitialized CPU, threw, was silently caught, and the VM cold-booted from default-state.bin instead of resuming the user's session.
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
Sessions intermittently fail to resume from saved state in packaged builds — the app cold-boots from
default-state.bininstead of the user'sstate-v5.bin.Cause
startEmulator()used a hardcodedsetTimeout(500)before callingrestore_state(). This races against v86's async wasm/BIOS/hda load. In dev the files are warm in cache (~170ms measured), so 500ms is plenty. In a packaged app launched cold — Gatekeeper scan, cold filesystem cache, slower disk — loading can exceed 500ms,restore_stateruns on an uninitialized CPU, throws, gets silently caught, andemulator.run()proceeds from a cold boot.Fix
Replace the timer with v86's
emulator-loadedevent, which fires exactly once after wasm + BIOS + hda are fully initialized. The listener is registered synchronously afternew V86()so it can't miss the event (file loads are always async).Testing
Instrumented build, launched Electron with an existing 86MB
state-v5.bin:emulator-loadedfired at 171msrestore_statesucceeded