A way to clean up multi-threaded Wasm resources when a worklet is garbage collected or otherwise finished/closed.
One way to currently do this with dedicated workers:
Parent Thread: Wait for a pre-determined memory location to be notified with Atomics.waitAsync().
Child Thread: When shutdown is desirable clean up Wasm thread resources.
Child Thread: Notify the pre-determined memory location with Atomics.notify().
Child Thread: Indefinitely block with Atomics.wait().
Parent Thread: Woken up by Atomics.notify(), call Worker.terminate() to clean up the worker.
Unfortunately this can't be done in worklets because they can't block, therefor there is no way to ensure that Wasm resources aren't being accessed.
What solutions exist today?
The best alternative is to invalidate the Wasm module and making any call to it throw, which doesn't solve the problem but at least prevents unsoundness.
How would you solve it?
A way to terminate worklets, like Worker.terminate() and an event that is triggered when the worklet closes. Alternatively only an event if worklets can be properly garbage collected even with a Wasm module.
I'm unsure how FinalizationRegistry could be used here instead of the event.
What problem are you trying to solve?
A way to clean up multi-threaded Wasm resources when a worklet is garbage collected or otherwise finished/closed.
One way to currently do this with dedicated workers:
Atomics.waitAsync()
.Atomics.notify()
.Atomics.wait()
.Atomics.notify()
, callWorker.terminate()
to clean up the worker.Unfortunately this can't be done in worklets because they can't block, therefor there is no way to ensure that Wasm resources aren't being accessed.
What solutions exist today?
The best alternative is to invalidate the Wasm module and making any call to it throw, which doesn't solve the problem but at least prevents unsoundness.
How would you solve it?
A way to terminate worklets, like
Worker.terminate()
and an event that is triggered when the worklet closes. Alternatively only an event if worklets can be properly garbage collected even with a Wasm module.I'm unsure how
FinalizationRegistry
could be used here instead of the event.Anything else?
This is coming from https://github.com/WebAudio/web-audio-api/issues/2568.