Skip to content

[eventloop] Fix main loop keepalive cleanup when cancelling or pausing - #27468

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:fix-mainloop-shutdown-27456
Aug 5, 2026
Merged

[eventloop] Fix main loop keepalive cleanup when cancelling or pausing#27468
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:fix-mainloop-shutdown-27456

Conversation

@sbc100

@sbc100 sbc100 commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Pop runtimeKeepaliveCounter immediately in MainLoop.pause() when a main loop is paused or cancelled, rather than deferring the pop until checkIsRunning() runs at the end of the next frame tick.

This fixes cases where exit() or emscripten_force_exit() is called immediately after emscripten_cancel_main_loop() from within the main loop callback.

Additionally, remove the redundant MainLoop.running property, using MainLoop.scheduler directly to determine if a main loop is active.

Fixes: #27456

Comment thread src/lib/libeventloop.js
@sbc100
sbc100 requested a review from kripken August 3, 2026 18:52
Comment thread src/lib/libeventloop.js Outdated
#if ASSERTIONS
assert(!MainLoop.func, 'emscripten_set_main_loop: there can only be one main loop function at once')
#endif
MainLoop.pause();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does "set main loop" pause the main loop? Especially since there is no current func to pause?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I just noticed this is redundant myself. The above assertion ensures this should never be needed.

@sbc100
sbc100 force-pushed the fix-mainloop-shutdown-27456 branch 2 times, most recently from 2afb68e to c0a37e2 Compare August 5, 2026 06:05
Comment thread src/parseTools.mjs Outdated
@sbc100
sbc100 force-pushed the fix-mainloop-shutdown-27456 branch from c0a37e2 to 02ff65c Compare August 5, 2026 15:59
@sbc100
sbc100 enabled auto-merge (squash) August 5, 2026 15:59
Pop runtimeKeepaliveCounter immediately in MainLoop.pause() when a main
loop is paused or cancelled, rather than deferring the pop until
checkIsRunning() runs at the end of the next frame tick.

This fixes cases where exit() or emscripten_force_exit() is called
immediately after emscripten_cancel_main_loop() from within the main
loop callback.

Additionally, remove the redundant MainLoop.running property, using
MainLoop.scheduler directly to determine if a main loop is active.

Fixes: emscripten-core#27456
@sbc100
sbc100 force-pushed the fix-mainloop-shutdown-27456 branch from 02ff65c to 5c31998 Compare August 5, 2026 17:55
@sbc100
sbc100 merged commit 739800b into emscripten-core:main Aug 5, 2026
41 checks passed
@sbc100
sbc100 deleted the fix-mainloop-shutdown-27456 branch August 5, 2026 18:51
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.

Can't exit the runtime immediately after emscripten_cancel_main_loop without errors when using assertions

2 participants