Skip to content

Commit

Permalink
[GR-52277] Fix a deadlock in IsolateAwareTruffleCompiler.tearDownIsol…
Browse files Browse the repository at this point in the history
…ateOnShutdown.

PullRequest: graal/17066
  • Loading branch information
christianhaeubl committed Mar 18, 2024
2 parents 99d81ab + c809f5b commit 8add253
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static void executeInitializationHooks() {

/**
* Adds a hook which will execute during isolate tear-down. Note it is possible for the
* {@link #tearDownHooks} to called without the {@link #initializationHooks} executing first.
* {@link #tearDownHooks} to be called without the {@link #initializationHooks} executing first.
*/
public void addTearDownHook(Hook tearDownHook) {
addHook(tearDownHooks, tearDownHook);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@
import java.io.StringWriter;
import java.util.concurrent.atomic.AtomicBoolean;

import jdk.graal.compiler.core.common.CompilationIdentifier;
import jdk.graal.compiler.core.common.SuppressFBWarnings;
import jdk.graal.compiler.nodes.PauseNode;
import jdk.graal.compiler.truffle.PartialEvaluator;
import jdk.graal.compiler.truffle.TruffleCompilation;
import jdk.graal.compiler.truffle.phases.TruffleTier;
import jdk.graal.compiler.word.Word;
import org.graalvm.nativeimage.CurrentIsolate;
import org.graalvm.nativeimage.Isolate;
import org.graalvm.nativeimage.IsolateThread;
Expand Down Expand Up @@ -66,6 +59,14 @@
import com.oracle.truffle.compiler.TruffleCompilationTask;
import com.oracle.truffle.compiler.TruffleCompilerListener;

import jdk.graal.compiler.core.common.CompilationIdentifier;
import jdk.graal.compiler.core.common.SuppressFBWarnings;
import jdk.graal.compiler.nodes.PauseNode;
import jdk.graal.compiler.truffle.PartialEvaluator;
import jdk.graal.compiler.truffle.TruffleCompilation;
import jdk.graal.compiler.truffle.phases.TruffleTier;
import jdk.graal.compiler.word.Word;

public class IsolateAwareTruffleCompiler implements SubstrateTruffleCompiler {
private static final Word ISOLATE_INITIALIZING = WordFactory.signed(-1);

Expand Down Expand Up @@ -133,10 +134,18 @@ protected CompilerIsolateThread beforeCompilation() {
Isolate isolate = getSharedIsolate();
if (isolate.isNull()) {
if (sharedIsolate.compareAndSet(WordFactory.nullPointer(), (Isolate) ISOLATE_INITIALIZING)) {
CompilerIsolateThread thread = IsolatedGraalUtils.createCompilationIsolate();
Runtime.getRuntime().addShutdownHook(new Thread(this::sharedIsolateShutdown));
sharedIsolate.set(Isolates.getIsolate(thread));
return thread; // (already attached)
try {
/* Adding the shutdown hook may fail if a shutdown is already in progress. */
Runtime.getRuntime().addShutdownHook(new Thread(this::sharedIsolateShutdown));
CompilerIsolateThread thread = IsolatedGraalUtils.createCompilationIsolate();
sharedIsolate.set(Isolates.getIsolate(thread));
return thread; // (already attached)
} catch (Throwable e) {
/* Reset the value so that the teardown hook doesn't hang. */
assert sharedIsolate.get().equal(ISOLATE_INITIALIZING);
sharedIsolate.set(WordFactory.nullPointer());
throw e;
}
}
isolate = getSharedIsolate();
assert isolate.isNonNull();
Expand All @@ -155,9 +164,11 @@ private Isolate getSharedIsolate() {

private void sharedIsolateShutdown() {
Isolate isolate = getSharedIsolate();
CompilerIsolateThread context = (CompilerIsolateThread) Isolates.attachCurrentThread(isolate);
compilerIsolateThreadShutdown(context);
Isolates.detachThread(context);
if (isolate.isNonNull()) {
CompilerIsolateThread context = (CompilerIsolateThread) Isolates.attachCurrentThread(isolate);
compilerIsolateThreadShutdown(context);
Isolates.detachThread(context);
}
}

@CEntryPoint(include = CEntryPoint.NotIncludedAutomatically.class, publishAs = CEntryPoint.Publish.NotPublished)
Expand Down

0 comments on commit 8add253

Please sign in to comment.