andywer / threads.js

🧵 Make web workers & worker threads as simple as a function call.
https://threads.js.org/
MIT License
3.06k stars 164 forks source link

Recover from a spawn error? #420

Open kellyselden opened 2 years ago

kellyselden commented 2 years ago

I'm trying to build a high resiliency system. Once in a while we'll get a Error: Timeout: Did not receive an init message from worker after 10000ms. Make sure the worker calls expose(). error. I'm wondering if there's a way to trap that error, and allow the thread pool to continue with one less thread?

andywer commented 2 years ago

Hey Kelly! Hmm, tricky. The error is thrown in the worker in case the initial handshake with the master process fails.

Hard to recover from something like that as there is no communication with the other side. Question is what goes wrong. I could imagine that there might be a race condition between the initialization of the worker and the master process sending the init message.

If that's true, re-sending the init message should do the trick if no message from the worker has been received after some timeout. You would need to change that in threads.js, though.

If you have got time to work on it, feel free and I will review the PR. Unfortunately I am lacking the time to work on it myself.

dohard-ma commented 2 years ago

After how long is it appropriate to resend the initial message? I think it is more appropriate to use the timeout set by the user, but it is not easy to get it in the worker

asasas234 commented 10 months ago

@andywer I encountered the same problem in the Safari iOS environment. Is it because Safari iOS does not support Web Workers?