Skip to content

Commit 4b2d533

Browse files
committed
[GR-67221] Avoid dereferencing possibly dead weakref in thread disposal
PullRequest: graal/21357
2 parents 9be24ba + d95db68 commit 4b2d533

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotContextImpl.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import java.util.function.Supplier;
8484
import java.util.logging.Level;
8585

86+
import org.graalvm.collections.Pair;
8687
import org.graalvm.options.OptionValues;
8788
import org.graalvm.polyglot.Context;
8889
import org.graalvm.polyglot.PolyglotException;
@@ -928,7 +929,7 @@ void resume(Future<Void> pauseFuture) {
928929
@TruffleBoundary
929930
Object[] enterThreadChanged(boolean enterReverted, boolean pollSafepoint, boolean mustSucceed, PolyglotThreadTask polyglotThreadFirstEnter,
930931
boolean leaveAndEnter) {
931-
List<Map.Entry<Thread, PolyglotThreadInfo>> deadThreads = null;
932+
List<Pair<Thread, PolyglotThreadInfo>> deadThreads = null;
932933
PolyglotThreadInfo enteredThread = null;
933934
boolean localEnterReverted = enterReverted;
934935
Object[] prev = null;
@@ -1121,21 +1122,21 @@ Object[] enterThreadChanged(boolean enterReverted, boolean pollSafepoint, boolea
11211122
}
11221123
}
11231124

1124-
private void finalizeAndDisposeThreads(List<Map.Entry<Thread, PolyglotThreadInfo>> deadThreads) {
1125+
private void finalizeAndDisposeThreads(List<Pair<Thread, PolyglotThreadInfo>> deadThreads) {
11251126
assert !Thread.holdsLock(this);
11261127
Throwable ex = null;
1127-
for (Map.Entry<Thread, PolyglotThreadInfo> removedThreadInfoEntryToRemove : deadThreads) {
1128-
ex = notifyThreadFinalizing(removedThreadInfoEntryToRemove.getValue(), ex, false);
1128+
for (Pair<Thread, PolyglotThreadInfo> removedThreadInfoEntryToRemove : deadThreads) {
1129+
ex = notifyThreadFinalizing(removedThreadInfoEntryToRemove.getRight(), ex, false);
11291130
}
11301131

1131-
for (Map.Entry<Thread, PolyglotThreadInfo> threadInfoEntryToRemove : deadThreads) {
1132-
ex = notifyThreadDisposing(threadInfoEntryToRemove.getValue(), ex);
1132+
for (Pair<Thread, PolyglotThreadInfo> threadInfoEntryToRemove : deadThreads) {
1133+
ex = notifyThreadDisposing(threadInfoEntryToRemove.getRight(), ex);
11331134
}
11341135

11351136
synchronized (this) {
1136-
for (Map.Entry<Thread, PolyglotThreadInfo> threadInfoEntryToRemove : deadThreads) {
1137-
threadInfoEntryToRemove.getValue().setContextThreadLocals(DISPOSED_CONTEXT_THREAD_LOCALS);
1138-
threads.remove(threadInfoEntryToRemove.getKey());
1137+
for (Pair<Thread, PolyglotThreadInfo> threadInfoEntryToRemove : deadThreads) {
1138+
threadInfoEntryToRemove.getRight().setContextThreadLocals(DISPOSED_CONTEXT_THREAD_LOCALS);
1139+
threads.remove(threadInfoEntryToRemove.getLeft());
11391140
}
11401141
}
11411142

@@ -1144,20 +1145,22 @@ private void finalizeAndDisposeThreads(List<Map.Entry<Thread, PolyglotThreadInfo
11441145
}
11451146
}
11461147

1147-
private List<Map.Entry<Thread, PolyglotThreadInfo>> collectDeadThreads() {
1148+
private List<Pair<Thread, PolyglotThreadInfo>> collectDeadThreads() {
11481149
assert Thread.holdsLock(this);
1149-
List<Map.Entry<Thread, PolyglotThreadInfo>> deadThreads = null;
1150+
List<Pair<Thread, PolyglotThreadInfo>> deadThreads = null;
11501151
/*
11511152
* A thread is added to the threads map only by the thread itself, so when the thread is in
11521153
* the map, and it is not alive, then it surely won't be used ever again.
11531154
*/
11541155
for (Map.Entry<Thread, PolyglotThreadInfo> threadInfoEntry : threads.entrySet()) {
1155-
if (!threadInfoEntry.getKey().isAlive() && !threadInfoEntry.getValue().isFinalizingDeadThread()) {
1156+
Thread thread = threadInfoEntry.getKey();
1157+
PolyglotThreadInfo threadInfo = threadInfoEntry.getValue();
1158+
if (thread != null && threadInfo != null && !thread.isAlive() && !threadInfo.isFinalizingDeadThread()) {
11561159
if (deadThreads == null) {
11571160
deadThreads = new ArrayList<>();
11581161
}
1159-
deadThreads.add(threadInfoEntry);
1160-
threadInfoEntry.getValue().setFinalizingDeadThread();
1162+
deadThreads.add(Pair.create(thread, threadInfo));
1163+
threadInfo.setFinalizingDeadThread();
11611164
}
11621165
}
11631166
return deadThreads;

0 commit comments

Comments
 (0)