Skip to content

Wait for v86 emulator-loaded before restoring state#367

Merged
felixrieseberg merged 1 commit into
mainfrom
claude/friendly-hertz
Apr 13, 2026
Merged

Wait for v86 emulator-loaded before restoring state#367
felixrieseberg merged 1 commit into
mainfrom
claude/friendly-hertz

Conversation

@felixrieseberg
Copy link
Copy Markdown
Owner

Problem

Sessions intermittently fail to resume from saved state in packaged builds — the app cold-boots from default-state.bin instead of the user's state-v5.bin.

Cause

startEmulator() used a hardcoded setTimeout(500) before calling restore_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_state runs on an uninitialized CPU, throws, gets silently caught, and emulator.run() proceeds from a cold boot.

Fix

Replace the timer with v86's emulator-loaded event, which fires exactly once after wasm + BIOS + hda are fully initialized. The listener is registered synchronously after new 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-loaded fired at 171ms
  • restore_state succeeded
  • CPU resumed at 563M instructions (mid-session, not POST)
  • save-on-quit still writes state correctly

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.
@felixrieseberg felixrieseberg merged commit 731a9b2 into main Apr 13, 2026
1 check passed
@felixrieseberg felixrieseberg deleted the claude/friendly-hertz branch April 13, 2026 17:18
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