node icon indicating copy to clipboard operation
node copied to clipboard

Provide a way to get worker initialization error synchronously

Open fisker opened this issue 1 year ago • 2 comments

What is the problem this feature will solve?

I'm using a worker to run asynchronous tasks and block the main thread with Atomics.wait(). If the worker has something like syntax errors, it seems that we can only get it by add a listener to error event, the program just hangs since worker can not wake the main thread.

new (require('worker_threads').Worker)(new URL('data:text/javascript,throw new Error'));

What is the feature you are proposing to solve the problem?

Provide a way to get the worker initialization error synchronously.

What alternatives have you considered?

I tried to pass a timeout to Atomics.wait(), but It's just a random magic number because I don't know how long the worker script takes to init.

fisker avatar Feb 06 '24 10:02 fisker

You might be interested in https://www.npmjs.com/package/synchronous-worker

I tried to pass a timeout to Atomics.wait(), but It's just a random magic number because I don't know how long the worker script takes to init.

You can use Atomics.notify() to let the worker thread tell the main thread when it's initialized. You'd still need an arbitrary timeout, but that's always the case with cross-thread communication.

aduh95 avatar Feb 06 '24 16:02 aduh95

You can use Atomics.notify() to let the worker thread tell the main thread when it's initialized.

Yes, that's extracly what I did, https://github.com/fisker/make-synchronized/commit/a5bca6604dcf6a71f3f4e7a9de9daa183137bb3c But an issue was reported that 1000ms is not enough to load a simple worker like this.

fisker avatar Feb 06 '24 18:02 fisker

There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the https://github.com/nodejs/node/labels/never-stale label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document.

github-actions[bot] avatar Aug 05 '24 01:08 github-actions[bot]

Please keep it open.

fisker avatar Aug 05 '24 03:08 fisker