Skip to content

Commit 9ad9fa4

Browse files
committed
[GR-31943] Serialize interrupts and cancels of a thread in NativeCallInterrupt
* Guest safepoints can be triggered in parallel from multiple threads. * We need to serialize all interrupts so we can properly track tasks and eventually cancel them all. Previously, a task might not be cancelled and the thread could finish, which would lead to errors from pthread_kill().
1 parent aacd319 commit 9ad9fa4

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/main/java/org/truffleruby/core/thread/NativeCallInterrupter.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class NativeCallInterrupter implements TruffleSafepoint.Interrupter {
2020

2121
private final Timer timer;
2222
private final long threadID;
23-
private volatile Task currentTask = null;
23+
private Task currentTask = null;
2424

2525
NativeCallInterrupter(Timer timer, long threadID) {
2626
this.timer = timer;
@@ -29,16 +29,25 @@ class NativeCallInterrupter implements TruffleSafepoint.Interrupter {
2929

3030
@Override
3131
public void interrupt(Thread thread) {
32-
final Task task = new Task(threadID);
33-
currentTask = task;
34-
timer.schedule(task, 0, Task.PERIOD);
32+
synchronized (this) {
33+
final Task previousTask = this.currentTask;
34+
if (previousTask != null) {
35+
previousTask.cancel();
36+
}
37+
38+
final Task task = new Task(threadID);
39+
this.currentTask = task;
40+
timer.schedule(task, 0, Task.PERIOD);
41+
}
3542
}
3643

3744
@Override
3845
public void resetInterrupted() {
39-
Task task = currentTask;
40-
if (task != null) {
41-
task.cancel();
46+
synchronized (this) {
47+
final Task task = this.currentTask;
48+
if (task != null) {
49+
task.cancel();
50+
}
4251
}
4352
}
4453

0 commit comments

Comments
 (0)