threads.js icon indicating copy to clipboard operation
threads.js copied to clipboard

feat: catch worker errors

Open Ugzuzg opened this issue 5 years ago • 2 comments

Depends on https://github.com/avoidwork/tiny-worker/pull/43 Reject the promise if the worker was killed by the system or unexpectedly exited.

A nice feature of [email protected] was that you could listen for exit event of the spawned worker and reject the promise based on it.

await new Promise((resolve, reject) => thread
  .run('./src/ChildProcessHelper')
  .send({ data })
  .on('exit', (code, signal) => {
    if (code !== 0) {
      reject(new Error(`child process exited with ${code} ${signal}`));
    } else {
      resolve([]);
    }
  })
  .promise()
  .then(resolve, reject));

With worker_threads this isn't possible at all, as they are running in the same process, so if something bad happens there, the main process is affected (e.g. segfault).

That's one more reason for enabling tiny-worker on latest node versions. https://github.com/andywer/threads.js/issues/229

The code just illustrates the idea.

Ugzuzg avatar Aug 06 '20 18:08 Ugzuzg

Thanks for sharing that PR, @Ugzuzg.

Can you check why the new test case is failing?

andywer avatar Aug 16 '20 10:08 andywer

@andywer, it fails because there is a dependency on a tiny-worker https://github.com/avoidwork/tiny-worker/pull/43 PR.

Ugzuzg avatar Aug 28 '20 12:08 Ugzuzg