[concurrency-interest] concurrency errors in java.lang.Throwable

Rémi Forax forax at univ-mlv.fr
Thu Aug 3 08:10:50 EDT 2006


David Holmes a écrit :
> Joe,
> 
>> It would be desirable to:
>>
>> 1. Declare detailMessage final
>> 2. Make getCause synchronized
>> 3. Most of all: document intent so we don't need to have these discussions
> 
> getCause being synchronized would only be sufficient if the constructor used
> the synchronized initCause() - otherwise we'd need a volatile, or atomics or
> ...
> 
> But to repeat what I've said elsewhere I don't expect Throwables to be
> passed across threads very often. And if they are passed across threads then
> that mechanism will need to involve synchronization. Hence access to the
> cause would be safe.
> 
> I think this is a non-issue, but might support "fixing" it purely for reason
> 3. Only trouble is today it is Throwable tomorrow  its ...
> 
> Cheers,
> David Holmes

I've a test case that shows the bug :

public class ThrowableConcurrencyBug {
   public static void initCause(Throwable t) {
     t.initCause(new Throwable());
   }

   public static void main(String[] args) {
     final Throwable t=new Throwable();
     new Thread() {
       @Override
       public void run() {
         for(;;) {
           if (t.getCause()!=null)
             System.out.println("bang !");
         }
       }
     }.start();

     for(int i=0;;i++) {
       initCause((i==100000)?t:new Throwable());
     }
   }
}

"bang !" is never (at least 15min :) printed with the server VM.

best regards

Rémi Forax



More information about the Concurrency-interest mailing list