Skip to content
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

Issue #7344 - Fix stop in process #7639

Merged
merged 3 commits into from
Feb 24, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,32 @@ public void execute() throws MojoExecutionException, MojoFailureException
//wait for pid to stop
getLog().info("Waiting " + stopWait + " seconds for jetty " + pid + " to stop");
Optional<ProcessHandle> optional = ProcessHandle.of(pid);
final long remotePid = pid.longValue();
optional.ifPresentOrElse(p ->
{
try
{
send(stopKey + "\r\n" + command + "\r\n", 0);
CompletableFuture<ProcessHandle> future = p.onExit();
if (p.isAlive())
//if running in the same process, just send the stop
//command and wait for the response
if (ProcessHandle.current().pid() == remotePid)
{
p = future.get(stopWait, TimeUnit.SECONDS);
send(stopKey + "\r\n" + command + "\r\n", stopWait);
}

if (p.isAlive())
getLog().info("Couldn't verify server process stop");
else
getLog().info("Server process stopped");
{
//running forked, so wait for the process
send(stopKey + "\r\n" + command + "\r\n", 0);
CompletableFuture<ProcessHandle> future = p.onExit();
if (p.isAlive())
{
p = future.get(stopWait, TimeUnit.SECONDS);
}

if (p.isAlive())
getLog().info("Couldn't verify server process stop");
else
getLog().info("Server process stopped");
}
}
catch (ConnectException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jetty.maven.plugin.AbstractWebAppMojo.DeploymentMode;
import org.eclipse.jetty.server.ShutdownMonitor;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -209,6 +210,30 @@ public void testStopWaitBadPid() throws Exception
log.assertContains("Server reports itself as stopped");
}

@Test
public void testStopSameProcess() throws Exception
{
//test that if we need to stop a jetty in the same process as us
//we will wait for it to exit
String stopKey = "foo";
long thisPid = ProcessHandle.current().pid();
MockShutdownMonitorRunnable runnable = new MockShutdownMonitorRunnable();
runnable.setPidResponse(Long.toString(thisPid));
MockShutdownMonitor monitor = new MockShutdownMonitor(stopKey, runnable);
monitor.start();

TestLog log = new TestLog();
JettyStopMojo mojo = new JettyStopMojo();
mojo.stopWait = 5;
mojo.stopKey = stopKey;
mojo.stopPort = monitor.getPort();
mojo.setLog(log);

mojo.execute();

log.assertContains("Waiting 5 seconds for jetty " + Long.toString(thisPid) + " to stop");
}

@Test
public void testStopWait() throws Exception
{
Expand Down