It sounds like you are proposing using the invocation of binding.pry
to interrogate all child threads and suspend them until you end your pry session. That is not possible for technical and practical reasons. The Binding
and Thread
classes don't work that way, and multithreading in Ruby doesn't work that way.
Threads in Ruby can only be suspended by calling Kernel#sleep
or Thread.stop
. (and those are functionally equivalent) Crucially, these methods can only be invoked on the current thread. One thread cannot suspend another thread. (Thread.stop
is a class method, not an instance method)
Let's look at what binding.pry
actually does: objects of class Binding encapsulate the execution context at some particular place in the code and retain this context for future use. So when you put binding.pry
into your code, you're telling Ruby to encapsulate the execution context for the current thread.
What that means is when you call binding.pry
in the main thread the Binding object has context for the current thread and can tell itself to sleep, but the core Ruby Thread class does not allow it to tell any other threads to sleep.
Even if it did support it, it would be weird and error-prone and the cause of a lot of head-scratching. Imagine that you have code like this:
# we are in the main thread
Thread.new do
# we are in the child thread
foo = Foo.new(bar.fetch(:baz, {}))
foo.save
end
# we are in the main thread
binding.pry
Because of the way Ruby handles context-switching, if binding.pry
told all child threads to stop then the child thread might stop ANYWHERE in the call stack, including anywhere in the code for Foo.new
or .save
. Having those threads pause and resume in the middle of executing code that you did not write will cause you trouble. For example, what happens if an ActiveRecord connection from the pool is checked out and used for a SELECT
query but the thread got put to sleep before it returned the connection to the pool and before it got a response? Bad stuff. Lots of bad stuff.
It sounds like the real solution for you is to change the verbosity of the child threads. If you are troubleshooting chatty code and your other threads are being noisy while you're trying to work in a single thread, then set the other threads to use, for example, a lower logging level temporarily.