Skip to content

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

Open
sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:fix-mainloop-shutdown-27456
Open

[eventloop] Fix main loop keepalive cleanup when cancelling or pausing#27468
sbc100 wants to merge 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

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 139020e to c02b9c2 Compare July 31, 2026 19:59
@sbc100
sbc100 requested a review from brendandahl July 31, 2026 19:59
Comment thread src/lib/libeventloop.js
// This will be non-null whenever a loop function is registered.
func: null,
// This will be non-null whenever a loop function is both registered and
// currently running.

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.

What is the difference between "registered" and "registered and currently running"?

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.

Registered meas somebody called emscripten_set_main_loop.

Running can be false if somebody calls emscripten_pause_main_loop, but even while pauses the callback is still "registered", because one can call emscripten_resume_main_loop to re-use the same callback.

@sbc100
sbc100 requested a review from kripken August 3, 2026 18:52
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