You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlrpc-dev@ws.apache.org by Dave Glowacki <da...@icecube.wisc.edu> on 2008/02/01 23:07:32 UTC

[PATCH] ThreadPool race condition

I seem to have turned up a race condition in the ThreadPool class.

I was running the latest xmlrpc (from Subversion) under the JRockit  
JVM on a multicore machine and the XML-RPC server was locking up.  I  
eventually tracked it down to these two bits of code:

     public void run() {
         while (!isShuttingDown()) {
             final Task t = getTask();
             if (t == null) {
                 try {
                     synchronized (this) {
                         wait();
                     }

and:

     synchronized void start(Task pTask) {
         task = pTask;
         synchronized (thread) {
             thread.notify();
         }
     }

The first bit of code is run by a newly created thread, while the  
second bit of code is run in the original thread.  As far as I can  
tell, JRockit did the following:

1) the new Thread started up, called getTask() and got back null.
2) the original thread ran the entire start() method, assigning  
'task' and calling thread.notify()
3) the new Thread then entered the 'synchronized' block and did the  
wait(), but since the notify() was already called, it never wakes up

I've attached a patch which fixed the problem for me.


Re: [PATCH] ThreadPool race condition

Posted by Dave Glowacki <da...@icecube.wisc.edu>.
On Feb 1, 2008, at 4:07 PM, Dave Glowacki wrote:

> I seem to have turned up a race condition in the ThreadPool class.

Here's a patch for an aggressively locked version of the  
ThreadPool.Poolable class which works under JRockit.

I removed the 'synchronized' from all the Poolable methods and  
instead locked the individual data objects so that the lock order  
could be guaranteed.

I'm not sure it's necessary to lock the 'shuttingDown' boolean as  
I've done in the patch -- the worst thing that could happen is that  
one or two threads runs a little longer than necessary.

Please let me know if you have any questions or comments (and thanks  
for the XML-RPC library -- you might be happy to know that it's  
running on machines at the South Pole!)

Here's the patch:


Re: [PATCH] ThreadPool race condition

Posted by Dave Glowacki <da...@icecube.wisc.edu>.
On Feb 1, 2008, at 4:07 PM, Dave Glowacki wrote:

> I seem to have turned up a race condition in the ThreadPool class.
>
> I was running the latest xmlrpc (from Subversion) under the JRockit  
> JVM on a multicore machine and the XML-RPC server was locking up.   
> I eventually tracked it down to these two bits of code:
>
>     public void run() {
>         while (!isShuttingDown()) {
>             final Task t = getTask();
>             if (t == null) {
>                 try {
>                     synchronized (this) {
>                         wait();
>                     }
>
> and:
>
>     synchronized void start(Task pTask) {
>         task = pTask;
>         synchronized (thread) {
>             thread.notify();
>         }
>     }
>
> The first bit of code is run by a newly created thread, while the  
> second bit of code is run in the original thread.  As far as I can  
> tell, JRockit did the following:
>
> 1) the new Thread started up, called getTask() and got back null.
> 2) the original thread ran the entire start() method, assigning  
> 'task' and calling thread.notify()
> 3) the new Thread then entered the 'synchronized' block and did the  
> wait(), but since the notify() was already called, it never wakes up
>
> I've attached a patch which fixed the problem for me.<xml.patch>

Actually, further testing reveals that my "fix" is susceptible to a  
deadlock.  I'll try to come up with a better fix...

---------------------------------------------------------------------
To unsubscribe, e-mail: xmlrpc-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: xmlrpc-dev-help@ws.apache.org