Skip to content

libretro: wait for scheduler-safe state save and restore - #583

Merged
sonninnos merged 1 commit into
libretro:masterfrom
BigBossWHD:fix/libretro-safe-state-scheduler
Oct 9, 2026
Merged

sonninnos merged 1 commit into
libretro:masterfrom
BigBossWHD:fix/libretro-safe-state-scheduler

Conversation

@BigBossWHD

Copy link
Copy Markdown

Problem

On a fresh content load, restoring a Neo Geo savestate can return success and briefly display the saved scene, then reboot the emulated machine. This is reproducible with in-game Metal Slug 3 and 4 states; a successful unserialize return alone does not establish that the state resumed correctly.

Cause and change

MAME's running_machine::handle_saveload() waits for scheduler.can_save() before both saving and loading, because pending anonymous timers can overwrite restored data. The libretro buffer entry points bypass this check.

In the failing cold-load case, pending CPU input queues still have nonzero queue counts when scheduler postload removes their temporary delivery callbacks. Subsequent interrupts are not delivered correctly and the Neo Geo watchdog resets the machine. A debugger trace reaches watchdog_timer_device::watchdog_expired() and schedule_soft_reset() after restoration.

Wait for the same scheduler-safe condition before write_buffer() and read_buffer(), advancing scheduler timeslices only while needed. Bound the wait to one emulated second and 100,000 iterations; return failure with a log message if it cannot become safe. The observed cold-load case needed one timeslice. This does not change the state format, disable the watchdog, or add an unconditional frame delay.

This is independent of the session slot-default reset in #582, which is already in the base branch.

Reproduction

  1. Start mslug3 or mslug4, enter a level and save a state.
  2. Completely unload/reopen the content, then restore immediately (RetroArch automatic state loading reproduces this).
  3. Let it run for at least 600 frames. Before the fix, the restored scene gives way to the boot sequence; with the fix, gameplay continues.

Validation and limits

  • Full arm64 macOS libretro core build on MAME 0.289 base 9069f39340f2d2b1795df8e71bb1d3d0fbc76598, with the libretro: reset machine options between content sessions #582 fix and this patch. This PR applies the same state change to current master e858fcb097c59836f68a246648c4e0c070a8f083; it does not duplicate libretro: reset machine options between content sessions #582.
  • Same Metal Slug 3 state: baseline CPU total cycles dropped from 1,097,078,511 to 120,028,510 after 600 frames (reset); patched total rose to 1,218,526,959. In-game screenshots confirmed level continuation for 3 and 4.
  • Restored and continued 600 frames with mslug, mslug2, mslug3, mslug4, mslug5, mslugx, mslug3b6, sf2, and kof97; repeated 4-to-3 loading also passed. Some samples were credited player-selection screens, not in-level gameplay. SF2 was additionally continued for 1,500 frames.
  • Real RetroArch 1.22.2: Metal Slug 3 automatic load, automatic save on exit, and automatic restore on the next launch retained the level and credits and continued the timer. Normal exit/unload passed.
  • Existing user states/configuration/remaps were preserved. Not a full-game or cross-platform validation; no DEBUG=1 build was performed.

Build: make -f Makefile.libretro platform=osx CROSS_COMPILE=1 LIBRETRO_APPLE_PLATFORM=arm64-apple-macos11 LIBRETRO_APPLE_ISYSROOT="$(xcrun --show-sdk-path)" -j10

AI assistance

Diagnosis, implementation, and this description were assisted by OpenAI Codex (GPT-6 family; an exact model snapshot/version identifier is not exposed in this session). Validation above was executed locally, with the stated scope and limitations.

@sonninnos
sonninnos merged commit 5f5a077 into libretro:master Oct 9, 2026
3 checks passed
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.

2 participants