js-sdk icon indicating copy to clipboard operation
js-sdk copied to clipboard

clarification on behavior of `runInWorker`

Open jlipps opened this issue 1 year ago • 4 comments

This minimal example:

import createPlugin from "@extism/extism";

async function main() {
  const plugin = await createPlugin('<some_wasm_file>', {useWasi: true, runInWorker: true});
}

main().catch(console.error);

Will hang, unless I call plugin.close(). Without runInWorker set to true, the process will terminate on its own, without calling plugin.close(). Is this expected? If so, it might be worth calling out in the docs as a usage distinction.

jlipps avatar Dec 05 '24 23:12 jlipps

@chrisdickinson i made the assumption that plugin going out of scope would close it. but maybe not? Or is plugin just not going out of scope here? i'm not really sure how node does top-level async maybe node is not closing because main is not being awaited?

bhelx avatar Dec 06 '24 00:12 bhelx

if close() must be called we should probably get that spelled out in docs somewhere.

bhelx avatar Dec 06 '24 00:12 bhelx

Good catch! Yes, close() is required when runInWorker: true. I'll add it to the docs! (In the future, we might use a FinalizationRegistry to do the cleanup for us.)

chrisdickinson avatar Dec 06 '24 00:12 chrisdickinson

Great, thanks for the clarification :-) Feel free to leave this open to track the docs update or close--I'm all set on my end!

jlipps avatar Dec 06 '24 00:12 jlipps