-
Notifications
You must be signed in to change notification settings - Fork 124
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
Api allows workers to do initialization but it is not clear how to do cleanup #87
Comments
Cleanup is difficult because it is possible for the thread the worker is running on to be terminated abruptly. And it can be difficult to reason about when the cleanup logic should run. @addaleax ... what do you think? Understanding that the mechanism would not be perfect, a mechanism to send a message to each worker that it should run cleanup logic after it completes it's current job (if any) should be possible. |
@jasnell thank you for quick reply!
Are you meaning
What do you mean? Inside message pool we know when we're going to terminate a thread. What if at |
It’s referring to
That would change the behavior here significantly, because if we allow graceful shutdown, then terminating the Worker thread is no longer an option, and we would have to rely on it cleaning up properly, even after an uncaught exception. But I’m not 100 % clear on what your concern is here. If you’re establishing database connections – network sockets are shut down when a Worker that owns them is being terminated, so there’s probably more to it than that? |
Thanks for explanation!
I don't have deep knowledge of which resources clean automatically and which one don't in worker threads, so yes, maybe cleanup is not that necessary. |
So … yeah. Files are a more complicated topic: The So yes, maybe it makes sense to provide some explicit cleanup hook here, even if that changes the behavior a bit. |
I definitely think we should implement something but we have to be explicit that the semantics are Best Effort in that we cannot guarantee that the cleanup logic will run every time for every worker instance. Sometimes it will, other times it won't. For something like a raw file descriptor, I'd expect that it would be better for the main thread to handle those regardless of what we do here. Specifically, I'm wondering if there's a way in core (not in piscina) that we could track raw file descriptors opened in a worker thread and have those automatically closed (if necessary) or reported with a warning on the main thread after the worker terminates. |
Here’s a PR to implement that: nodejs/node#34303 I think we could disable that in Piscina by default without further issues, and maybe also flip the default in Node.js in a semver-major follow-up. |
Just an update on this: We've landed and shipped the support for tracking unmanaged FDs. In Node.js core, we'll hopefully soon land an implementation of |
Hi there,
Docs says that it is possible to do initialization inside worker. I am using that feature to create a database connection like this:
During its lifetime, pool may decide to close inactive threads. In that case I need to close database connection.
But currently I didn't found any api which allows to do that.
Currently I set
minThreads
andmaxThreads
to the same value and have the following as a workaround:Not only it is complicated, but also fragile (depending on how Piscina schedules tasks under the hood, setting
idleTimeout
to very high number). Also, this way it is not possible to have dynamic number of threads in the pool.Interesting, but cleanup functionality is something which is missing in all worker pool implementations I can find on npm. I am wondering why?
Maybe there is a completely different way to do cleanup inside worker thread?
I'll be grateful for any explanation/reading resource on this topic.
If this is something which is in scope of Piscina package I am ready to work on PR as well.
The text was updated successfully, but these errors were encountered: