Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/natives/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ export function interruptibleWait(t: JThread, ms: number | null, register?: (wak
};
t.wake = done;
register?.(done);
if (ms !== null) {
timer = setTimeout(done, Math.max(0, ms));
// timers fire at once past MAX_DELAY, so a longer wait re-arms
const arm = (left: number) => {
const step = Math.min(Math.max(0, left), MAX_DELAY);
timer = setTimeout(step < left ? () => { t.vm.timers.delete(timer!); arm(left - step); } : done, step);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Race condition: if an interrupt wakes the thread (calling done, which sets t.wake = null) after a step timer fires but before its re-arm callback runs, the callback still executes and re-arms a new timer. The wait has already resolved, so this creates a stray timer that will fire later and call done again. Fix: check if (t.wake === null) return; at the start of the re-arm callback before deleting the timer and calling arm.

t.vm.timers.add(timer);
}
};
if (ms !== null) arm(ms);
});
}

const MAX_DELAY = 0x7fffffff;

function* waitImpl(t: JThread, self: JObject, ms: bigint): NativeGen {
const vm = t.vm;
const mon = self.mon;
Expand Down
50 changes: 50 additions & 0 deletions test/java/LongTimeouts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.util.concurrent.locks.LockSupport;

// Timeouts longer than a host timer can hold must not wake early.
public class LongTimeouts {
static volatile boolean interrupting;

static String how(boolean interrupted) {
return interrupting && interrupted ? "interrupt" : "early";
}

public static void main(String[] args) throws Exception {
String[] result = new String[4];
Object lock = new Object();
Thread[] threads = {
new Thread(() -> {
LockSupport.parkNanos(Long.MAX_VALUE);
result[0] = how(Thread.currentThread().isInterrupted());
}),
new Thread(() -> {
LockSupport.parkUntil(Long.MAX_VALUE);
result[1] = how(Thread.currentThread().isInterrupted());
}),
new Thread(() -> {
synchronized (lock) {
try {
lock.wait(Long.MAX_VALUE / 2);
result[2] = "early";
} catch (InterruptedException e) {
result[2] = how(true);
}
}
}),
new Thread(() -> {
try {
Thread.sleep(Long.MAX_VALUE);
result[3] = "early";
} catch (InterruptedException e) {
result[3] = how(true);
}
}),
};
for (Thread t : threads) t.start();
Thread.sleep(300);
interrupting = true;
for (Thread t : threads) t.interrupt();
for (Thread t : threads) t.join();
String[] names = { "parkNanos", "parkUntil", "wait", "sleep" };
for (int i = 0; i < 4; i++) System.out.println(names[i] + ": " + result[i]);
}
}
Loading