Skip to content

Fix async futex wait in WASM worker dose not keep runtime alive #20306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/library_wasm_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,18 +292,24 @@ Atomics.waitAsync = (i32a, index, value, maxWaitMilliseconds) => {
$liveAtomicWaitAsyncs: '{}',
$liveAtomicWaitAsyncCounter: '0',

emscripten_atomic_wait_async__deps: ['$atomicWaitStates', '$liveAtomicWaitAsyncs', '$liveAtomicWaitAsyncCounter', '$jstoi_q'],
emscripten_atomic_wait_async__deps: ['$atomicWaitStates', '$liveAtomicWaitAsyncs', '$liveAtomicWaitAsyncCounter', '$jstoi_q',
#if !MINIMAL_RUNTIME
'$runtimeKeepalivePush', '$runtimeKeepalivePop',
#endif
Comment on lines +296 to +298
Copy link
Member

Choose a reason for hiding this comment

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

I believe this is not needed. The compiler adds the runtime-keepalive stuff automatically.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, after testing, the compiler does not automatically add runtime-keepalive stuff, because library_wasm_worker.js is not in the core system libraries of modules.js, so need to add a reference here manually.

// Core system libraries (always linked against)

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, it's not in the core system libraries always linked against, but when you enable wasm workers then it is added to the list. And then it should be processed like any other library. That is, there should be no difference between a core library and a non-core library if both are linked in. Is that not so?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, it's not in the core system libraries always linked against, but when you enable wasm workers then it is added to the list. And then it should be processed like any other library. That is, there should be no difference between a core library and a non-core library if both are linked in. Is that not so?

According to the modules.js, when enable WASM worker support, it doesn't seem to be automatically added to the core library.
As a result, dependencies must be added manually.

Copy link
Member

Choose a reason for hiding this comment

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

Where do you see that? (I see no mention of wasm workers in modules.js)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think @kripken is right, we hopefully don't need these __deps here.. if you remove the __deps here what error do you see?

],
emscripten_atomic_wait_async: (addr, val, asyncWaitFinished, userData, maxWaitMilliseconds) => {
let wait = Atomics.waitAsync(HEAP32, {{{ getHeapOffset('addr', 'i32') }}}, val, maxWaitMilliseconds);
if (!wait.async) return atomicWaitStates.indexOf(wait.value);
// Increment waitAsync generation counter, account for wraparound in case
// application does huge amounts of waitAsyncs per second (not sure if
// possible?)
// Valid counterrange: 0...2^31-1
{{{ runtimeKeepalivePush() }}}
let counter = liveAtomicWaitAsyncCounter;
liveAtomicWaitAsyncCounter = Math.max(0, (liveAtomicWaitAsyncCounter+1)|0);
liveAtomicWaitAsyncs[counter] = addr;
wait.value.then((value) => {
{{{ runtimeKeepalivePop() }}}
if (liveAtomicWaitAsyncs[counter]) {
delete liveAtomicWaitAsyncs[counter];
{{{ makeDynCall('vpiip', 'asyncWaitFinished') }}}(addr, val, atomicWaitStates.indexOf(value), userData);
Expand Down