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
Add on_thread_exit hook #2920
Add on_thread_exit hook #2920
Conversation
LGTM 👌 Needs a rebase though. One thought, should we call it |
Should there be a hook on the front end, immediately after the thread block starts? Also, current code in this PR rescues outside of |
Add a hook to run when a worker thread is trimmed (exits normally). This can be useful to clean up thread local resources that do not want to be cleaned between every request (see clean_thread_locals for that).
b067630
to
a43167f
Compare
Thanks for the suggestions. I've rebased and followed the naming from the @MSP-Greg, it sounds like you're asking for an additional feature with a hook at the start of running a thread. If you have a use case for needing this, perhaps create a separate issue? (Or correct me if I've misunderstood). Changed the rescue to be per hook call as suggested, that's a good idea. |
Not an additional feature, but possibly a more complete implementation of the feature. In this case, a logical place to create the 'thread-local resources' might be a companion Edit: I'll defer to others on this one, so maybe wait for their input. I just want to avoid someone asking for it at a later time. |
@MSP-Greg, actually that does sound sensible to add in with this as it would be a place to create thread-local resources or to monitor when threads are starting and stopping. I'll take a look at it soon assuming nobody disagrees. |
@biinari Works for me too, please do investigate 👍 |
It looks like OP needed to move on to other things. I don't think adding |
Thanks for merging this in. Sorry you're right, I've had my focus other projects for a while. |
No worries homie, no one's on a timeline here 👍 Thanks for your contribution 🙇 |
Closes #2100
Description
For apps that need to tidy up thread-local resources when a thread exits, add a new
on_thread_exit
hook.This will be called when a thread is being trimmed, just before the thread exits.
An example config entry involving rabbitmq channels being held in thread local variables (one channel per thread):
The hook should be configured with a quick non-blocking operation as it will block new requests from being started in other threads. I wondered about moving the call to the hook outside of the mutex section, but that becomes quite a bit less readable and I can't see what anyone would need to do that would be a blocking operation.
Your checklist for this pull request
[ci skip]
to the title of the PR.#issue
" to the PR description or my commit messages.