Races in plain code
Steps on threads switch threads between steps and at the lock(…)s a test hands
the code. Code written the plain way has neither: its races sit between a field’s read and its
write, inside a synchronized method, behind a lock someone forgot. To find them, proptastic
needs to switch threads inside the code itself. That’s what the agent does.
Add the agent
Section titled “Add the agent”The agent is a jar of its own, io.proptastic:proptastic-agent, put on the test JVM with the
packages it should rewrite. It needs JDK 24 or later in the test JVM:
val agent by configurations.creating
dependencies { agent("io.proptastic:proptastic-agent:1.0.0")}
tasks.test { jvmArgs("-javaagent:${agent.singleFile}=com/example/")}As each class of com/example/ loads, the agent rewrites it so that .threads(n) can switch
threads before each read and write of its fields, at each monitor and lock, around each wait and
notify, and before each call into java.util.concurrent. Nothing in the JDK changes, and off a
schedule each point does nothing.
A lost update
Section titled “A lost update”A counter, with no lock at all:
public final class Counter { private int count;
public void increment() { count = count + 1; }Two threads, each incrementing it:
forAll(steps(Counter::new).step("increment", Counter::increment)) .threads(2)no order of the threads' steps, one at a time, gives what they saw they saw: thread 1: increment() thread 2: increment() one at a time: increment(), then increment(), and the state ends as count 2 the threads left: count 1 thread 1's increment() began before thread 2's increment() and ended after it
aLostUpdateIsFound failed after 2 tries (seed 0x1) shrunk steps: thread 1 thread 2 1. increment() paused before it writes Counter.count 1. increment() pin it: .example(thread(step("increment")), thread(step("increment"))) // typed pins come from @StepBuilderThe first thread read 0 and was paused before it wrote 1; the second read 0 and wrote 1; then the first wrote its 1. One step at a time, two increments make 2. The line under the paused thread says where it was stopped, field by field.
A deadlock through monitors
Section titled “A deadlock through monitors”Each move locks its two books in the order it’s given:
public final class Transfers { private final Object london = new Object(); private final Object newYork = new Object(); private long moved;
public void move(boolean fromLondon) { var first = fromLondon ? london : newYork; var second = fromLondon ? newYork : london; synchronized (first) { synchronized (second) { moved++; } } }deadlock: thread 1 holds monitor of Transfers.newYork, waits for monitor of Transfers.london; thread 2 holds monitor of Transfers.london, waits for monitor of Transfers.newYork
aDeadlockThroughSynchronizedNamesTheFieldsMonitors failed after 1 try (seed 0x1) shrunk steps: thread 1 thread 2 1. move(false) locks monitor of Transfers.newYork paused before it takes monitor of Transfers.london 1. move(true) locks monitor of Transfers.london waits for monitor of Transfers.newYork waits for monitor of Transfers.london pin it: .example(thread(step("move", false)), thread(step("move", true))) // typed pins come from @StepBuilderA monitor is named by the field that holds it, so the deadlock reads as the code does.
What else it finds
Section titled “What else it finds”The test kit the agent is held to finds, each the same way every run:
- a class whose methods are each
synchronized, but whosewithdrawchecks the balance in one call and takes it in another; - a
ReentrantLocktaken inaddand forgotten inaddUnlocked; - a
ConcurrentHashMapchecked withcontainsKey, thenput, by two threads at once; - a
wait()underifwhere it belongs underwhile, which takes a letter that wasn’t there; - a count read, then set, through a
VarHandle,paused before it calls VarHandle.setVolatile; - a count kept in a final
int[],counts[0]++,paused before it writes an element of int[]; - a belt with room for one, checked empty, then put on, so the second thread
waits for room in ArrayBlockingQueue #1no thread will make; - a lock built on
LockSupport.parkthat wakes the next thread before it lets go, so the nextparks, and no thread will unpark it; - two
StampedLocks taken in either order, each thread waiting for the other’s write lock.
And it passes what’s right: a mailbox that waits in a loop, a timed wait nothing ends but its
time, a total taken under its lock, a count taken by compareAndSet in a loop, a relay through a
blocking queue, the park-built lock that lets go first, and books under StampedLocks read and
written as they should be.