Skip to content
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 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

biinari
Copy link
Member

@biinari biinari commented Aug 30, 2022

https://www.pivotaltracker.com/story/show/183131066

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):

on_thread_exit do
  unless Thread.current[MY_KEY].nil?
    Thread.current[MY_KEY].disconnect
    Thread.current[MY_KEY] = nil
  end
end

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.

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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant