# Deadlock in ThreadPool using backtick

**URL:** <https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755>\
**Category:** ruby-talk\
**Created:** [8 August 2007 17:11 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755 "2007-08-08T17:11:55Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justin\_Johnson2](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@Justin\_Johnson2](https://rubytalk.org/u/Justin_Johnson2)\
**Post date:** [8 August 2007 17:11 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/1 "2007-08-08T17:11:55Z")

</div>

Hello,

I'm using the ThreadPool class as defined in the Ruby Cookbook, recipe  
20.7 and running on Windows XP with the One Click installer of Ruby  
1.8.6 patch level 0. Normally it works fine. However, whenever I call  
a command using backticks I get a deadlock. The script I'm running is  
below, followed by the output. To see it succeed, comment out the  
backtick line and uncomment out the system line.

Does anyone know what the problem is?

Thanks,  
Justin

##### BEGIN FILE ######  
require 'thread'

class ThreadPool  
&nbsp;&nbsp;def initialize(max\_size)  
&nbsp;&nbsp;&nbsp;&nbsp;@pool = []  
&nbsp;&nbsp;&nbsp;&nbsp;@max\_size = max\_size  
&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex = Mutex.new  
&nbsp;&nbsp;&nbsp;&nbsp;@pool\_cv = ConditionVariable.new  
&nbsp;&nbsp;end

> **···**
>
> #---  
> &nbsp;&nbsp;def dispatch(\*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;Thread.new do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Wait for space in the pool.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while @pool.size \>= @max\_size  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Pool is full; waiting to run #{args.join(',')}...\n" if  
> $DEBUG  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Sleep until some other thread calls @pool\_cv.signal.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_cv.wait(@pool\_mutex)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> #---  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool \<\< Thread.current  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield(\*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue =\> e  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exception(self, e, \*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Remove the thread from the pool.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool.delete(Thread.current)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Signal the next waiting thread that there's a space in the  
> pool.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_cv.signal  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def shutdown  
> &nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize { @pool\_cv.wait(@pool\_mutex) until  
> @pool.empty? }  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def exception(thread, exception, \*original\_args)  
> &nbsp;&nbsp;&nbsp;&nbsp;# Subclass this method to handle an exception within a thread.  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Exception in thread #{thread}: #{exception}"  
> &nbsp;&nbsp;end  
> end  
> #---
> 
> pool = ThreadPool.new 2  
> begin  
> &nbsp;&nbsp;1.upto(200) do |i|  
> &nbsp;&nbsp;&nbsp;&nbsp;pool.dispatch do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Job #{i} started."  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`dir c:\\winnt \> NUL`  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#system("dir c:\\winnt \> NUL")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Job #{i} stopped."  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> ensure  
> &nbsp;&nbsp;pool.shutdown  
> end  
> ##### END FILE ######
> 
> ##### BEGIN OUTPUT ######  
> C:\temp\>ruby thread\_test.rb  
> Job 1 started.  
> Job 2 started.  
> Job 1 stopped.  
> Job 2 stopped.  
> Job 3 started.  
> Job 6 started.  
> Job 5 started.  
> Job 4 started.  
> Job 3 stopped.  
> Job 6 stopped.  
> Job 5 stopped.  
> Job 4 stopped.  
> deadlock 0x2b45008: sleep:- - thread\_test.rb:18  
> deadlock 0x2b450d0: sleep:- - thread\_test.rb:18  
> deadlock 0x2b45198: sleep:- - thread\_test.rb:18  
> deadlock 0x2b45260: sleep:- - thread\_test.rb:18  
> ... Skip many similar lines ...  
> deadlock 0x2b5627c: sleep:- - thread\_test.rb:28  
> deadlock 0x2b5636c: sleep:- - thread\_test.rb:28  
> deadlock 0x27ac748: sleep:- (main) - thread\_test.rb:39  
> thread\_test.rb:39:in `wait': Thread(0x27ac748): deadlock (fatal)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from thread\_test.rb:39:in `shutdown'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from thread\_test.rb:39:in `synchronize'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from thread\_test.rb:39:in `shutdown'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from thread\_test.rb:62
> 
> C:\temp\>  
> ##### END OUTPUT ######  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [9 August 2007 01:49 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/2 "2007-08-09T01:49:50Z")

</div>

Hmm. I'm not a thread pro, but I'll give it a shot.  
I don't think it's necessarily a problem with backtick, but with the  
code itself 🙂

first off

Justin Johnson wrote:

> Hello,
> 
> I'm using the ThreadPool class as defined in the Ruby Cookbook, recipe  
> 20.7 and running on Windows XP with the One Click installer of Ruby  
> 1.8.6 patch level 0. Normally it works fine. However, whenever I call  
> a command using backticks I get a deadlock. The script I'm running is  
> below, followed by the output. To see it succeed, comment out the  
> backtick line and uncomment out the system line.
> 
> Does anyone know what the problem is?
> 
> Thanks,  
> Justin
> 
> ##### BEGIN FILE ######  
> require 'thread'
> 
> class ThreadPool  
> &nbsp;&nbsp;def initialize(max\_size)  
> &nbsp;&nbsp;&nbsp;&nbsp;@pool =   
> &nbsp;&nbsp;&nbsp;&nbsp;@max\_size = max\_size  
> &nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex = Mutex.new  
> &nbsp;&nbsp;&nbsp;&nbsp;@pool\_cv = ConditionVariable.new  
> &nbsp;&nbsp;end  
> #---  
> &nbsp;&nbsp;def dispatch(\*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;Thread.new do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Wait for space in the pool.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while @pool.size \>= @max\_size  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Pool is full; waiting to run #{args.join(',')}...\n" if  
> $DEBUG  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Sleep until some other thread calls @pool\_cv.signal.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_cv.wait(@pool\_mutex)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> #---

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool \<\< Thread.current

The above line should be within the synchronize (above), or it might  
spawn more than @max\_size threads (I think).  
Replace with  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while @pool.size \>= @max\_size  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Pool is full; waiting to run #{args.join(',')}...\n" if  
$DEBUG  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Sleep until some other thread calls @pool\_cv.signal.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_cv.wait(@pool\_mutex)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool \<\< Thread.current  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield(\*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue =\> e  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exception(self, e, \*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize do

In reality you don't need to synchronize here, as the @pool size/add to  
is synchronized elsewhere. Taking this synch out may introduce new  
problems, but...I'm not sure what they are 🙂  
My question is if the wait within the shutdown function will 'suck'  
signals away from processes, decreasing the number that can go at a  
time, if all are at the 'wait' phase. Perhaps rethinking this code  
would be nice 🙂

-Roger

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Remove the thread from the pool.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool.delete(Thread.current)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Signal the next waiting thread that there's a space in the  
> pool.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_cv.signal  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def shutdown  
> &nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize { @pool\_cv.wait(@pool\_mutex) until  
> @pool.empty? }  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def exception(thread, exception, \*original\_args)  
> &nbsp;&nbsp;&nbsp;&nbsp;# Subclass this method to handle an exception within a thread.  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Exception in thread #{thread}: #{exception}"  
> &nbsp;&nbsp;end  
> end  
> #---
> 
> pool = ThreadPool.new 2  
> begin  
> &nbsp;&nbsp;1.upto(200) do |i|  
> &nbsp;&nbsp;&nbsp;&nbsp;pool.dispatch do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Job #{i} started."  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`dir c:\\winnt \> NUL`  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#system("dir c:\\winnt \> NUL")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Job #{i} stopped."  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> ensure  
> &nbsp;&nbsp;pool.shutdown  
> end  
> ##### END FILE ######
> 
> ##### BEGIN OUTPUT ######  
> C:\temp\>ruby thread\_test.rb  
> Job 1 started.  
> Job 2 started.  
> Job 1 stopped.  
> Job 2 stopped.  
> Job 3 started.  
> Job 6 started.  
> Job 5 started.  
> Job 4 started.  
> Job 3 stopped.  
> Job 6 stopped.  
> Job 5 stopped.  
> Job 4 stopped.  
> deadlock 0x2b45008: sleep:- - thread\_test.rb:18  
> deadlock 0x2b450d0: sleep:- - thread\_test.rb:18  
> deadlock 0x2b45198: sleep:- - thread\_test.rb:18  
> deadlock 0x2b45260: sleep:- - thread\_test.rb:18  
> ... Skip many similar lines ...  
> deadlock 0x2b5627c: sleep:- - thread\_test.rb:28  
> deadlock 0x2b5636c: sleep:- - thread\_test.rb:28  
> deadlock 0x27ac748: sleep:- (main) - thread\_test.rb:39  
> thread\_test.rb:39:in `wait': Thread(0x27ac748): deadlock (fatal)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from thread\_test.rb:39:in `shutdown'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from thread\_test.rb:39:in `synchronize'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from thread\_test.rb:39:in `shutdown'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from thread\_test.rb:62
> 
> C:\temp\>  
> ##### END OUTPUT ######

There may also (I'm not positive about this), be a way that multiple  
signals may then occur very quickly, and only one processed. Another  
possible problem, too.

GL  
-Roger

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [31 December 2007 15:46 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/3 "2007-12-31T15:46:07Z")

</div>

> I'm using the ThreadPool class as defined in the Ruby Cookbook, recipe  
> 20.7 and running on Windows XP with the One Click installer of Ruby  
> 1.8.6 patch level 0. Normally it works fine. However, whenever I call  
> a command using backticks I get a deadlock. The script I'm running is  
> below, followed by the output. To see it succeed, comment out the  
> backtick line and uncomment out the system line.

Turns out that using backticks with OS X can cause threads to lock up,  
too, in a multi-threaded environment. Not sure if this is a prob with  
backticks or just Ruby I/O in general, or just mac OS X/win32 specific  
(doubtful on the last one).  
GL  
-Roger

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [31 December 2007 18:33 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/4 "2007-12-31T18:33:02Z")

</div>

my mind is much too feeble to write robust threaded code that's safe on all platforms. invert your process flow and it's easy: this works on \*nix and winblows -\>

cfp:~ \> cat a.rb

require 'thread'

class ThreadPool \< ::Array  
&nbsp;&nbsp;&nbsp;Done = Object.new.freeze

&nbsp;&nbsp;&nbsp;attr 'q'  
&nbsp;&nbsp;&nbsp;attr 'exception\_handler'

&nbsp;&nbsp;&nbsp;def initialize size = 42, &exception\_handler  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@q = Queue.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@exception\_handler = exception\_handler || lambda{|e| raise}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Integer(size).times{ push new\_consumer }  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def new\_consumer  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.new do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.current.abort\_on\_exception  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;object = q.pop  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if object == Done  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;object.call  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue Exception =\> e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exception\_handler.call e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def dispatch &block  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@q.push block  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def shutdown  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size.times{ q.push Done }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each{|t| t.join}  
&nbsp;&nbsp;&nbsp;end  
end

null = test(?e, "/dev/null") ? "/dev/null" : "NUL"  
pool = ThreadPool.new 2

begin  
&nbsp;&nbsp;&nbsp;1.upto(200) do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pool.dispatch do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Job #{ i } started."  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command = "ruby -e' fill\_stdout\_pipe = 42.chr \* 8193; puts fill\_stdout\_pipe '"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` #{ command } `  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system "#{ command } \> #{ null } 2\>&1"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Job #{ i } stopped."  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
ensure  
&nbsp;&nbsp;&nbsp;pool.shutdown  
end

by spooling up all the thread at once you'll save the overhead of creating all those threads. the @q.pop stuff does all the condition variable stuff you were trying to do so you can check that code out if you want to.

ps - when testing stuff that reads output (backtick, popen, or blatant io) be sure to test will a full pipe on stdout - typically \> 8192 bytes.

kind regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Aug 8, 2007, at 11:11 AM, Justin Johnson wrote:
> 
> > Does anyone know what the problem is?
> 
> --  
> it is not enough to be compassionate. you must act.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![Justin\_Johnson2](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@Justin\_Johnson2](https://rubytalk.org/u/Justin_Johnson2)\
**Post date:** [9 August 2007 13:22 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/5 "2007-08-09T13:22:17Z")

</div>

> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool \<\< Thread.current
> 
> The above line should be within the synchronize (above), or it might  
> spawn more than @max\_size threads (I think).  
> Replace with  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while @pool.size \>= @max\_size  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Pool is full; waiting to run #{args.join(',')}...\n" if  
> $DEBUG  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Sleep until some other thread calls @pool\_cv.signal.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_cv.wait(@pool\_mutex)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool \<\< Thread.current  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

I tried your suggestion but still get the error, and only when using  
backticks or any other method that reads from stdout or stderr.

> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield(\*args)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue =\> e  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exception(self, e, \*args)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize do
> 
> In reality you don't need to synchronize here, as the @pool size/add to  
> is synchronized elsewhere. Taking this synch out may introduce new  
> problems, but...I'm not sure what they are 🙂

I'm not sure what you mean. This makes sense to me, as we do not want  
more than one thread removing itself from the list at a time.

> My question is if the wait within the shutdown function will 'suck'  
> signals away from processes, decreasing the number that can go at a  
> time, if all are at the 'wait' phase. Perhaps rethinking this code  
> would be nice 🙂

I'm not sure what you mean by "suck signals away from processes". The  
shutdown method is just waiting until all threads have ended so we don't  
end our program before the threads are done.

It still seems to me that stdout and stderr have something to do with my  
problem, since it always occurs when using backticks or even  
win32/popen3 methods that read from stdout and stderr, but never with  
system. Anyone else have any ideas on this?

Thanks for the reply.

Justin

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Charles\_Oliver\_Nutte](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@Charles\_Oliver\_Nutte](https://rubytalk.org/u/Charles_Oliver_Nutte)\
**Post date:** [31 December 2007 20:59 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/6 "2007-12-31T20:59:22Z")

</div>

ara.t.howard wrote:

> > Does anyone know what the problem is?
> 
> my mind is much too feeble to write robust threaded code that's safe on all platforms. invert your process flow and it's easy: this works on \*nix and winblows -\>

...

> by spooling up all the thread at once you'll save the overhead of creating all those threads. the @q.pop stuff does all the condition variable stuff you were trying to do so you can check that code out if you want to.

Isn't this largely just working around a lack of concurrency in Ruby's threading model (or perhaps a bug in backtick handling)? With concurrent threads I can implement the same thing in a trivial amount of code and not have to worry about backticks et al blocking, nor about spinning up processes. In fact with native threads the original ThreadPool from Ruby Cookbook ought to work fine for basically anything you want to throw at it (including backticks).

Perhaps backtick needs to be fixed in MRI so it doesn't block the entire runtime?

- Charlie

> **···**
>
> > On Aug 8, 2007, at 11:11 AM, Justin Johnson wrote:

---

<div class="post-metadata">

**Author:** ![Justin\_Johnson2](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@Justin\_Johnson2](https://rubytalk.org/u/Justin_Johnson2)\
**Post date:** [9 August 2007 14:05 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/7 "2007-08-09T14:05:36Z")

</div>

> It still seems to me that stdout and stderr have something to do with my  
> problem, since it always occurs when using backticks or even  
> win32/popen3 methods that read from stdout and stderr, but never with  
> system. Anyone else have any ideas on this?

I should also point out that the exact same script never fails on UNIX.  
Maybe this is a Windows bug? ☹

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [9 August 2007 17:06 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/8 "2007-08-09T17:06:59Z")

</div>

Justin Johnson wrote:

> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool \<\< Thread.current
> > 
> > The above line should be within the synchronize (above), or it might  
> > spawn more than @max\_size threads (I think).  
> > Replace with  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while @pool.size \>= @max\_size  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Pool is full; waiting to run #{args.join(',')}...\n" if  
> > $DEBUG  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Sleep until some other thread calls @pool\_cv.signal.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_cv.wait(@pool\_mutex)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool \<\< Thread.current  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> I tried your suggestion but still get the error, and only when using  
> backticks or any other method that reads from stdout or stderr.
> 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield(\*args)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue =\> e  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exception(self, e, \*args)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize do
> > 
> > In reality you don't need to synchronize here, as the @pool size/add to  
> > is synchronized elsewhere. Taking this synch out may introduce new  
> > problems, but...I'm not sure what they are 🙂

Because you're removing (and only once, and from a hash), I believe it's  
thread safe to remove at any time (though, in retrospect, it might cause  
a teeny bit of redundancy, but no problems)--i.e. taking out the  
synchronize here

> I'm not sure what you mean. This makes sense to me, as we do not want  
> more than one thread removing itself from the list at a time.
> 
> > My question is if the wait within the shutdown function will 'suck'  
> > signals away from processes, decreasing the number that can go at a  
> > time, if all are at the 'wait' phase. Perhaps rethinking this code  
> > would be nice 🙂

My hypothesis is that the shutdown function somehow or other messes up  
the way the pool works. Maybe/maybe not.

Another problem with shutdown is that is seems like shutdown could be  
called early enough to 'disallow' certain threads from entering the pool  
(those that have not yet started to wait on the condition variable). So  
that is another problem with it.

> I'm not sure what you mean by "suck signals away from processes". The  
> shutdown method is just waiting until all threads have ended so we don't  
> end our program before the threads are done.
> 
> It still seems to me that stdout and stderr have something to do with my  
> problem, since it always occurs when using backticks or even  
> win32/popen3 methods that read from stdout and stderr, but never with  
> system. Anyone else have any ideas on this?

I was able to recreate the bug using system OR backticks, as a note. I  
think that the difference is only in timing, not in functionality (one  
takes longer, so aggravates the problem more, perhaps?)

In reality, though, I think this might (might) be a real bug in Ruby, or  
maybe I misunderstand the functionality of signal.

My guess is still that shutdown is messing it up somehow

[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/818d88a5eae23820](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/818d88a5eae23820)  
discusses some type of IO binding problem (but not a concurrency one).

I do also notice some 'weird lines'  
like  
Job 184 stopped.♪◙File Not Found  
Which might be indicative of the problem you described.

What I dislike about this bug is that sometimes it shows itself and  
sometimes it doesn't, so it's hard to know if you've actually overcome  
it or not!

Another thought is that it might just be Ruby's threads 'misassigning'  
variables or what not.

Another idea would be to have every thread that adds itself to the pool  
'signal' immediately after, to allow some other waiting thread to  
'check' if the pool has decreased. That would be a kind of bandaid hack  
but heck maybe it would work 🙂

In my opinion it needs a different condition variable like "pool empty"  
which is signaled by each thread on exit, for the shutdown method to  
wait on.

Another concern is that it seems to me that there may be a possibility  
that shutdown will end too early, in extreme cases where two threads  
remove themselves simultaneously from the pool. Not our problem, but  
hey, it might be.

From the interthreaded output it appears that Ruby does indeed have some  
output problems. Not sure. Another opinion is that maybe it's ruby's  
deadlock detection going amuck--you'll notice in teh original post two  
threads on line 28--these two are at a synchronize point. You'd think  
that this could NOT deadlock, since those two should be able to always  
continue, unless there's a loop of some sort within a synchronize.  
you might try rewriting dispatch to be something like

&nbsp;&nbsp;def dispatch(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;Thread.new do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Wait for space in the pool.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize do  
&nbsp;&nbsp;if @pool.size \> @max\_size  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "WHAT GREATER?"  
&nbsp;&nbsp;end  
&nbsp;&nbsp;if @pool.size == @max\_size  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Pool is full; waiting to run #{args.join(',')}...\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Sleep until some other thread calls @pool\_cv.signal.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_cv.wait(@pool\_mutex) # receiving a signal should ALWAYS  
mean there's space now available  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool \<\< Thread.current  
&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @pool.size \> @max\_size  
&nbsp;&nbsp;&nbsp;&nbsp;print "huh? it has GOTTA be les than or equal here!"  
&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

Then there are no loops so you ensure that, if there is a deadlock, and  
some threads are waiting on a synchronize, then it's the deadlock  
protection's fault [it thinks the mutex is deadlocked, but we don't  
think it is]. This is very very possibly the problem.

As per the post  
"Ditto. AFAIK all external IO is blocking in Ruby on Windows..."

If this is the case you might try using a synchronize around the system  
command, itself, to avoid concurrency on the IO. Maybe all running  
processes are 'blocked' on IO [though I've seen interleaved results on  
the screen, so they don't seem to block on write] so deadlock thinks  
they're frozen? (except---you have processes stuck on synchronize, and  
the IO command runs from not within a synchronize block, so you wouldn't  
think blocking IO would be a synchronize problem). Maybe it is. Sigh 🙂  
I have written programs with hundreds of threads that output to the  
screen and (sure maybe they block when the write happens, perhaps,  
but...) never had a deadlock issue from it (well, then again I didn't  
try to synchronize it, either).

If it is a Ruby IO problem, then maybe two of the processes are 'getting  
stuck' in IO and never ending--you could put in some debug info to test  
out that hypothesis. Might not be the real problem, though, seeing the  
two threads on line 28.

It seems (in my opinion--haven't checked it out too closely yet) that  
the problem is only caused when the shutdown method is 'in the mix'--you  
might try rewriting it to create an array of all threads trying to enter  
the pool [i.e. array of size 200] and then joining on each element of  
the array  
while !allThreadArray.empty?  
&nbsp;&nbsp;nextThread = allThreadArray.shift  
&nbsp;&nbsp;nextThread.join  
end

that type of thing.

For my runs I used windows. Haven't tried it in Linux at all.

In short I don't know. Lots of hypotheses 🙂  
Good luck!  
-Roger

--I like it 🙂 [free bible - Google Search](http://www.google.com/search?q=free+bible)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [31 December 2007 22:27 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/9 "2007-12-31T22:27:27Z")

</div>

in general you may be correct - but i think the OP's code has a race condition. if you look at the stacktrace it's not reading stdout (backticks) that's got everyone's undies in a wedgie - it's the shutdown method. so basically the original code starts out by firing off a bunch of threads, limited at the number 2. let's assume it safely got two running, but that none of have completed, that leaves every other thread blocked here

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while @pool.size \>= @max\_size  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Sleep until some other thread calls @pool\_cv.signal.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_cv.wait(@pool\_mutex)

so let's assume they've all just went to sleep...

now the main code starts to shutdown, calling

&nbsp;&nbsp;&nbsp;def shutdown  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize ...

so it has the lock

now the first two finish and try to take them selves out of the @pool by doing

&nbsp;&nbsp;&nbsp;&nbsp;ensure  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool\_mutex.synchronize do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pool.delete(Thread.current)

and of course go to sleep since the main thread has that lock

now the main thread goes to sleep via

&nbsp;&nbsp;&nbsp;@pool\_cv.wait(@pool\_mutex) ...

so now everyone is asleep and no one will be signaled - deadlock.

i think the backticks are a red herring and that the shutdown method would need to be reworked, at minimum.

threads are just too complicated without Queue IMHO. the classic race condition you see all over the place is

&nbsp;&nbsp;&nbsp;Thread.new{ thread\_working }

&nbsp;&nbsp;&nbsp;something\_that\_depends\_on\_thread\_working

but that only works sometimes, specifically when the thread\_working gets started before the next line - which is not always true. etc. etc.

i just glanced at the code so maybe i've overlooked something - but i think the OP's code is racing, not ruby.

cheers.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Dec 31, 2007, at 1:59 PM, Charles Oliver Nutter wrote:
> 
> > Isn't this largely just working around a lack of concurrency in Ruby's threading model (or perhaps a bug in backtick handling)? With concurrent threads I can implement the same thing in a trivial amount of code and not have to worry about backticks et al blocking, nor about spinning up processes. In fact with native threads the original ThreadPool from Ruby Cookbook ought to work fine for basically anything you want to throw at it (including backticks).
> > 
> > Perhaps backtick needs to be fixed in MRI so it doesn't block the entire runtime?
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![Alex\_Dd](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Alex\_Dd](https://rubytalk.org/u/Alex_Dd)\
**Post date:** [9 August 2007 15:23 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/10 "2007-08-09T15:23:28Z")

</div>

> \> It still seems to me that stdout and stderr have something to do with

my

> \> problem, since it always occurs when using backticks or even  
> \> win32/popen3 methods that read from stdout and stderr, but never with  
> \> system. Anyone else have any ideas on this?
> 
> I should also point out that the exact same script never fails on UNIX.  
> Maybe this is a Windows bug? ☹

AFAIK Windows Ruby threads do not work properly when used in conjunction  
with external I/O. I think this is something to do with the Ruby and  
Windows select loops.

HTH

Alex.

> **···**
>
> Justin Johnson wrote on 09/08/2007 15:05:36:

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [1 October 2007 16:59 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/11 "2007-10-01T16:59:07Z")

</div>

> It seems (in my opinion--haven't checked it out too closely yet) that  
> the problem is only caused when the shutdown method is 'in the mix'--you  
> might try rewriting it to create an array of all threads trying to enter  
> the pool [i.e. array of size 200] and then joining on each element of  
> the array  
> while !allThreadArray.empty?  
> &nbsp;&nbsp;nextThread = allThreadArray.shift  
> &nbsp;&nbsp;nextThread.join  
> end
> 
> that type of thing.

Attempting it with the latest patch level of ruby might help.  
-Roger

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [31 December 2007 23:29 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/12 "2007-12-31T23:29:50Z")

</div>

Turns out that the largest problem with the OP was that they were using  
1.8.6 patch level 0, which had some buggy buggy thread stuff, so it was  
declaring 'deadlock' in error. You'll notice some of the threads  
'stuck' on lines that should go forward always.

I would point out, however, that there still seem to be some concurrency  
problems in the 1.8.6 MRI.

> in general you may be correct - but i think the OP's code has a race  
> condition. if you look at the stacktrace it's not reading stdout  
> (backticks) that's got everyone's undies in a wedgie - it's the  
> shutdown method. so basically the original code starts out by firing

I always questioned the shutdown method. I think overall it might still  
work using the current shutdown method 'if the spawned threads happen to  
all get to cv.wait before the shutdown thread does' as it probably puts  
them in an array and sends 'signal' to the first ones that waited for  
the signal, which would be the 'real' threads not the shutdown thread.  
Just my $0.02.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Charles\_Oliver\_Nutte](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@Charles\_Oliver\_Nutte](https://rubytalk.org/u/Charles_Oliver_Nutte)\
**Post date:** [31 December 2007 23:44 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/13 "2007-12-31T23:44:56Z")

</div>

ara.t.howard wrote:

> > Isn't this largely just working around a lack of concurrency in Ruby's threading model (or perhaps a bug in backtick handling)? With concurrent threads I can implement the same thing in a trivial amount of code and not have to worry about backticks et al blocking, nor about spinning up processes. In fact with native threads the original ThreadPool from Ruby Cookbook ought to work fine for basically anything you want to throw at it (including backticks).
> > 
> > Perhaps backtick needs to be fixed in MRI so it doesn't block the entire runtime?
> 
> in general you may be correct - but i think the OP's code has a race condition. if you look at the stacktrace it's not reading stdout (backticks) that's got everyone's undies in a wedgie - it's the shutdown method. so basically the original code starts out by firing off a bunch of threads, limited at the number 2. let's assume it safely got two running, but that none of have completed, that leaves every other thread blocked here

Yes, what you pointed out is completely busted. I hadn't seen that code. But why would you spin things off into a bunch of processes? Just fixing shutdown should make it work fine, if backticks aren't the problem.

- Charlie

> **···**
>
> > On Dec 31, 2007, at 1:59 PM, Charles Oliver Nutter wrote:

---

<div class="post-metadata">

**Author:** ![Sam\_Smoot](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Sam\_Smoot](https://rubytalk.org/u/Sam_Smoot)\
**Post date:** [9 August 2007 16:10 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/14 "2007-08-09T16:10:04Z")

</div>

Ditto. AFAIK all external IO is blocking in Ruby on Windows. Which is  
why  
the green threads are next to useless on Windows. You can't even use  
DRb  
to get around it with multiple distinct processes since they'll all  
block  
until the remote execution returns.

> **···**
>
> On Aug 9, 10:23 am, al...@nominet.org.uk wrote:
> 
> > Justin Johnson wrote on 09/08/2007 15:05:36:
> > 
> > \> \> It still seems to me that stdout and stderr have something to do with  
> > my  
> > \> \> problem, since it always occurs when using backticks or even  
> > \> \> win32/popen3 methods that read from stdout and stderr, but never with  
> > \> \> system. Anyone else have any ideas on this?
> > 
> > \> I should also point out that the exact same script never fails on UNIX.  
> > \> Maybe this is a Windows bug? ☹
> > 
> > AFAIK Windows Ruby threads do not work properly when used in conjunction  
> > with external I/O. I think this is something to do with the Ruby and  
> > Windows select loops.
> > 
> > HTH
> > 
> > Alex.

---

<div class="post-metadata">

**Author:** ![Clifford\_Heath5](https://avatars.discourse-cdn.com/v4/letter/c/7ab992/32.png) [@Clifford\_Heath5](https://rubytalk.org/u/Clifford_Heath5)\
**Post date:** [2 October 2007 06:35 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/15 "2007-10-02T06:35:04Z")

</div>

Roger Pack wrote:

> Attempting it with the latest patch level of ruby might help.

I had the same problem, posted here last Friday, and sure enough,  
the problem seems fixed with the -p110 build. I wasn't doing any  
I/O or using backticks - just two threads synchronizing using Mutex  
and ConditionVariable. The -p0 implemention is catastrophically  
broken and unusable, and apparently untested as well.

One thread is waiting in the ConditionVariable, another tries to  
enter the Mutex so it can change things and signal the condition,  
but deadlock is declared, killing the process. How hard can this be  
to test for, really? Appalling.

I believe the -p110 version of the One-Click installer is waiting  
on some fixes to the ODBC drivers. I hadn't realized what a boon  
this installer is until I tried to configure everything manually.  
Hint: Get the Windows build (URL below), the rubygems ZIP, and  
minimally, ssleay32.dll and libeay32.dll from openssl. Put it all  
together and you can install the rest from GEMs.

\<[http://www.garbagecollect.jp/ruby/mswin32/en/download/release.html&gt;](http://www.garbagecollect.jp/ruby/mswin32/en/download/release.html&gt;),  
\<[http://jarp.does.notwork.org/win32/&gt;\](http://jarp.does.notwork.org/win32/&gt;%5C).

... or offer Curt Hibbs some help getting the latest one-click  
installer finished.

Clifford Heath.

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [1 January 2008 21:33 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/16 "2008-01-01T21:33:13Z")

</div>

you could just fix shutdown to be sure. but the way i think is that i'd be right back in the hot seat when it came time to implement #pause and #start, etc. condition vars are, to me, just code smell - they're asking for trouble. the other reason is that, to me, the name 'ThreadPool' suggests a pool of threads, not a stack of ever changing threads. so, in much the same way as i'd expect something called 'ConnectionPool' to reuse connections i except a thread pool to reuse a few threads. that's reading into the code i realize - but that's my personal through process. as you well know there are always many ways to skin a cat.

kind regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Dec 31, 2007, at 4:44 PM, Charles Oliver Nutter wrote:
> 
> > Yes, what you pointed out is completely busted. I hadn't seen that code. But why would you spin things off into a bunch of processes? Just fixing shutdown should make it work fine, if backticks aren't the problem.
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![Clifford\_Heath5](https://avatars.discourse-cdn.com/v4/letter/c/7ab992/32.png) [@Clifford\_Heath5](https://rubytalk.org/u/Clifford_Heath5)\
**Post date:** [2 October 2007 06:40 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/17 "2007-10-02T06:40:03Z")

</div>

Clifford Heath wrote:

> Put it all together and you can install the rest from GEMs.

Oh, whoops, you need zlib as well for gems to work.  
Same location as openssl. Sorry.

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [3 October 2007 15:40 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/18 "2007-10-03T15:40:22Z")

</div>

Maybe once you get a working copy you can zip it up and put it online  
for lazy folks like me to get (until one-click gets fixed). Or if you  
need some space I could host it or whatever.  
-Roger

> **···**
>
> > on some fixes to the ODBC drivers. I hadn't realized what a boon  
> > this installer is until I tried to configure everything manually.  
> > Hint: Get the Windows build (URL below), the rubygems ZIP, and  
> > minimally, ssleay32.dll and libeay32.dll from openssl. Put it all  
> > together and you can install the rest from GEMs.
> > 
> > \<[http://www.garbagecollect.jp/ruby/mswin32/en/download/release.html&gt;](http://www.garbagecollect.jp/ruby/mswin32/en/download/release.html&gt;),
> > 
> > ... or offer Curt Hibbs some help getting the latest one-click  
> > installer finished.
> > 
> > Clifford Heath.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Clifford\_Heath5](https://avatars.discourse-cdn.com/v4/letter/c/7ab992/32.png) [@Clifford\_Heath5](https://rubytalk.org/u/Clifford_Heath5)\
**Post date:** [7 October 2007 22:10 UTC](https://rubytalk.org/t/deadlock-in-threadpool-using-backtick/39755/19 "2007-10-07T22:10:17Z")

</div>

Roger Pack wrote:

> Maybe once you get a working copy you can zip it up and put it online for lazy folks like me to get (until one-click gets fixed).

I don't think it's worth it. You only have to add rubygems and 3 DLLs d/l'd  
from one site to the Windows binary build, then whatever else you need comes  
from gems. My bundle includes Gtk and RMagick, which you probably don't want.
