Skip to content
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

js: Properly manage workers when useWorkers is called twice #626

Merged
merged 1 commit into from
Oct 31, 2023
Merged

Conversation

zeux
Copy link
Owner

@zeux zeux commented Oct 29, 2023

Before this change, calling useWorkers twice would allocate new workers but keep old workers around; this resulted in old workers leaking when the new count was larger; when the new count was smaller, we would still maintain the worker count but recreate some workers and leak older ones.

Instead we now only create more workers if more workers are requested, and if the requested count is smaller than current worker count, we ask some existing workers (nicely) to close by sending a dummy message.

Because messages are processed in order, and processing the worker response does not access global workers[], it should be safe to call useWorkers(0) at any point without disrupting already dispatched requests. This also provides a clean way to reclaim all resources used by the workers, including WebAssembly memory.

Fixes #522 (by allowing useWorkers(1) / useWorkers(0) to be used to clean up resources)

Before this change, calling useWorkers twice would allocate new workers
but keep old workers around; this resulted in old workers leaking when
the new count was larger; when the new count was smaller, we would still
maintain the worker count but recreate some workers and leak older ones.

Instead we now only create more workers if more workers are requested,
and if the requested count is smaller than current worker count, we ask
some existing workers (nicely) to close by sending a dummy message.

Because messages are processed in order, and processing the worker
response does not access global workers[], it should be safe to call
useWorkers(0) at any point without disrupting already dispatched
requests. This also provides a clean way to reclaim all resources used
by the workers, including WebAssembly memory.
@zeux zeux merged commit 22db2a5 into master Oct 31, 2023
12 checks passed
@zeux zeux deleted the js-ww branch October 31, 2023 14:33
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.

wasm: expose methods to dispose or recreate wasm instances
1 participant