Files
vpinball-wasm/patches/vpinball/0008-emscripten-controller-stop-no-busywait.patch
valknarandClaude Sonnet 5 43216a408e Fix PinMAME busy-wait freeze on Controller.Stop() (dispose() hang)
Controller::Stop() (real desktop vpinball code, unmodified until now)
busy-waits for PinmameIsRunning() to clear after calling PinmameStop(),
in a sleep loop meant for a real OS thread to notice the quit flag and
finish stopping on its own. Under Emscripten there is no such thread -
PinmameStop() there only sets the quit flag - so the loop spun forever
on the single available thread, with nothing left to ever clear it.

This fires from Player::~Player()'s GameEvents_Exit script event
(controller.vbs's default Exit handler calls Controller.Stop), which
runs synchronously inside dispose() - hanging any consumer's teardown
for a ROM-based table, most visibly React StrictMode/unmount cleanup
calling stop() then dispose() shortly after.

Fixed by driving one more (now-instant, since the quit flag is already
set) PinmameEmscriptenStep() call directly under __EMSCRIPTEN__ instead
of busy-waiting - it runs cpu_post_run() and OnStateChange(0)
synchronously right there.

Also adds a "Stop & Dispose" button to the basic example, exercising
the same stop()-then-wait-two-frames-then-dispose() sequence consumers
use, to make this kind of regression visible without a separate app.

Confirmed fixed by hands-on testing: dispose() on a running ROM-based
table now returns immediately instead of hanging the tab.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-24 10:12:25 +02:00

26 lines
1.1 KiB
Diff

diff --git a/plugins/pinmame/Controller.cpp b/plugins/pinmame/Controller.cpp
index 29c2405..a498fee 100644
--- a/plugins/pinmame/Controller.cpp
+++ b/plugins/pinmame/Controller.cpp
@@ -265,8 +265,20 @@ void Controller::Stop()
if (PinmameIsRunning())
{
PinmameStop();
+#ifdef __EMSCRIPTEN__
+ // PinmameStop() only sets a "please quit" flag here (see its own
+ // Emscripten branch) - there's no separate OS thread that will ever
+ // notice it and finish stopping on its own, so busy-waiting for
+ // PinmameIsRunning() to clear on this single thread would spin
+ // forever. Drive one more step directly instead: with the quit flag
+ // already set, it's a bounded, instant call that completes the
+ // pending teardown (calls cpu_post_run() and OnStateChange(0))
+ // synchronously right here.
+ PinmameEmscriptenStep();
+#else
while (PinmameIsRunning() != 0) // Wait until the machine is stopped
std::this_thread::sleep_for(std::chrono::milliseconds(75));
+#endif
if (m_onGameEndHandler)
m_onGameEndHandler(this);
}