When an exception kills a thread in a Group

Chris Vest mr.chrisvest at gmail.com
Wed Jun 25 10:11:32 UTC 2014


This one is consistently getting stuck without printing stack traces for me (JMH 0.9):

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.locks.StampedLock;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Group;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;

@State(Scope.Benchmark)
public class StuckBenchmark {

    public StampedLock lock;

    @Setup
    public void setUp() {
        lock = new StampedLock();
    }

    @Group
    @Benchmark
    public void sad() {
        long stamp = lock.writeLock();
        ThreadLocalRandom.current().nextInt(0);
        lock.unlockWrite(stamp); // this will never get called
    }

    @Group
    @Benchmark
    public void indifferent() {
        long stamp = lock.writeLock();
        lock.unlockWrite(stamp);
    }
}

Cheers,
Chris

On 25 Jun 2014, at 10:28, Aleksey Shipilev <aleksey.shipilev at oracle.com> wrote:

> Hi Chris,
> 
> On 06/25/2014 12:36 AM, Chris Vest wrote:
>> I just had a head-scratchy 30 minutes that could have been avoided if
>> stack traces had been eagerly printed when an exception kills a
>> thread that is part of a @Group (I haven’t checked if it’s the same
>> for ordinary non- at Group worker threads).
> 
> They are supposed to be printed early if once benchmark thread fails.
> However, there might be corner cases when one thread is failing and
> exiting the sync iteration loop, and all other threads are stuck then.
> We try to recover in those cases, but it might be not enough.
> 
>> I had accidentally put a ThreadLocalRandom.current().next(0) in one
>> of my benchmark methods, right after taking a write lock on a
>> StampedLock that I foolishly didn’t unlock in a finally-clause. The
>> next(0) threw an exception and killed the thread, leaving the lock
>> taken, while the other threads in the group quickly got stuck waiting
>> for that lock. The exception from next(0) was never printed to my
>> console for some reason, leaving me quite puzzled as to what was
>> going on.
> 
> A simple reproducer would be nice.
> 
> -Aleksey.



More information about the jmh-dev mailing list