Skip to content

8338281: jshell does not run shutdown hooks #3595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private boolean processCommand() throws IOException {
} catch (Throwable ex) {
// JShell-core not waiting for a result, ignore
}
return true;
return false;
}
default: {
Object arg = in.readObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.sun.jdi.ThreadReference;
import com.sun.jdi.VMDisconnectedException;
import com.sun.jdi.VirtualMachine;
import java.util.concurrent.TimeUnit;
import jdk.jshell.spi.ExecutionControl;
import jdk.jshell.spi.ExecutionEnv;
import static jdk.jshell.execution.Util.remoteInputOutput;
Expand All @@ -65,6 +66,8 @@
*/
public class JdiDefaultExecutionControl extends JdiExecutionControl {

private static final int SHUTDOWN_TIMEOUT = 1; //1 second

private VirtualMachine vm;
private Process process;
private final String remoteAgent;
Expand Down Expand Up @@ -218,6 +221,20 @@ public void stop() throws EngineTerminationException, InternalException {
@Override
public void close() {
super.close();

Process remoteProcess;

synchronized (this) {
remoteProcess = this.process;
}

if (remoteProcess != null) {
try {
remoteProcess.waitFor(SHUTDOWN_TIMEOUT, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
debug(ex, "waitFor remote");
}
}
disposeVM();
}

Expand Down
56 changes: 55 additions & 1 deletion test/langtools/jdk/jshell/ShutdownTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@
* @run testng ShutdownTest
*/

import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;

import jdk.jshell.JShell;
import jdk.jshell.JShell.Subscription;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import org.testng.annotations.BeforeMethod;

@Test
public class ShutdownTest extends KullaTesting {

int shutdownCount;
Expand All @@ -53,13 +59,15 @@ public void testExit() {
assertEquals(shutdownCount, 1);
}

@Test
public void testCloseCallback() {
shutdownCount = 0;
getState().onShutdown(this::shutdownCounter);
getState().close();
assertEquals(shutdownCount, 1);
}

@Test
public void testCloseUnsubscribe() {
shutdownCount = 0;
Subscription token = getState().onShutdown(this::shutdownCounter);
Expand All @@ -68,6 +76,7 @@ public void testCloseUnsubscribe() {
assertEquals(shutdownCount, 0);
}

@Test
public void testTwoShutdownListeners() {
ShutdownListener listener1 = new ShutdownListener();
ShutdownListener listener2 = new ShutdownListener();
Expand Down Expand Up @@ -118,6 +127,51 @@ public void testSubscriptionAfterShutdown() {
getState().onShutdown(e -> {});
}

@Test
public void testRunShutdownHooks() throws IOException {
Path temporary = Paths.get("temp");
Files.newOutputStream(temporary).close();
assertEval("import java.io.*;");
assertEval("import java.nio.file.*;");
assertEval("""
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
Files.delete(Paths.get("$TEMPORARY"));
} catch (IOException ex) {
//ignored
}
}))
""".replace("$TEMPORARY", temporary.toAbsolutePath()
.toString()
.replace("\\", "\\\\")));
getState().close();
assertFalse(Files.exists(temporary));
}

private Method currentTestMethod;

@BeforeMethod
public void setUp(Method testMethod) {
currentTestMethod = testMethod;
super.setUp();
}

@BeforeMethod
public void setUp() {
}

@Override
public void setUp(Consumer<JShell.Builder> bc) {
Consumer<JShell.Builder> augmentedBuilder = switch (currentTestMethod.getName()) {
case "testRunShutdownHooks" -> builder -> {
builder.executionEngine(Presets.TEST_STANDARD_EXECUTION);
bc.accept(builder);
};
default -> bc;
};
super.setUp(augmentedBuilder);
}

private static class ShutdownListener implements Consumer<JShell> {
private int count;

Expand Down