-
Notifications
You must be signed in to change notification settings - Fork 308
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
Do not join @thread if that is the current Thread #590
Conversation
If you're running multiple Bunny connections in a pooled manner, for example using the gem `connection_pool`, the threading model can be altered by the pooling gem to disable handling of exceptions. For example, the gem `connection_pool` disables the handling when it creates an instance, and enables the handling when you're using a connection: ```ruby def with(options = {}) Thread.handle_interrupt(Exception => :never) do conn = checkout(options) begin Thread.handle_interrupt(Exception => :immediate) do yield conn end ensure checkin end end end ``` When you've setup a connection and released it to the pool, it slumbers in a Thread of which the handling of exceptions is disabled. If you receive a (forced) disconnect or a NetworkError occurs, it triggers the recovery (when enabled) of Bunny. This recovery relies on a ShutdownSignal to be raised on the `reader_loop` thread which needs to be caught by the `clean_up_on_shutdown` in `Session`. Because the exception is not handled anymore, the code in `maybe_shutdown_reader_loop` in `Session` tries to join the `reader_loop` thread to force a re-raise of a thrown unhandled exception. That join is done on the `reader_loop` thread which is the same thread that started the recovery so we'll receive a ThreadError telling us "Target thread must not be current thread (ThreadError)". To prevent the above from happening we can check if the thread we're trying to join is the same thread that is currently running and not join if it is the same thread.
Backported to |
@michaelklishin: just as an update, we've encountered no irregularities during testing. We did see correctly recovering connections. |
OK, I'll cut a new release now. Thanks for following up. |
If you're running multiple Bunny connections in a pooled manner, for example using the gem
connection_pool
, the threading model can be altered by the pooling gem to disable handling of exceptions.For example, the gem
connection_pool
disables the handling when it creates an instance, and enables the handling when you're using a connection:When you've setup a connection and released it to the pool, it slumbers in a Thread of which the handling of exceptions is disabled. If you receive a (forced) disconnect or a NetworkError occurs, it triggers the recovery (when enabled) of Bunny. This recovery relies on a ShutdownSignal to be raised on the
reader_loop
thread which needs to be caught by theclean_up_on_shutdown
inSession
. Because the exception is not handled anymore, the code inmaybe_shutdown_reader_loop
inSession
tries to join thereader_loop
thread to force a re-raise of a thrown unhandled exception. That join is done on thereader_loop
thread which is the same thread that started the recovery so we'll receive a ThreadError telling us "Target thread must not be current thread (ThreadError)".To prevent the above from happening we can check if the thread we're trying to join is the same thread that is currently running and not join if it is the same thread.
This PR fixes #589.