Skip to content

Commit

Permalink
* Fixed race condition between shutdown hook and CampaignRunner#run w…
Browse files Browse the repository at this point in the history
…here campaign process could be restarted after the shutdown hook ran.
  • Loading branch information
katherine-hough committed Oct 18, 2023
1 parent c212962 commit 9867c72
Showing 1 changed file with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class CampaignRunner {
private final CampaignValues values;
private Process process = null;
private boolean shutdown = false;

public CampaignRunner(CampaignValues values) {
if (values == null) {
Expand All @@ -34,27 +35,32 @@ public void run() throws MojoExecutionException {
private void run(FuzzFramework framework, Duration duration)
throws InterruptedException, IOException, MojoExecutionException {
long endTime = System.currentTimeMillis() + duration.toMillis();
if (ProcessUtil.waitFor(setProcess(framework.startCampaign()), duration.toMillis(),
TimeUnit.MILLISECONDS)) {
if (!framework.canRestartCampaign()) {
throw new MojoExecutionException("Campaign process terminated unexpectedly");
}
while (endTime > System.currentTimeMillis()) {
long remaining = endTime - System.currentTimeMillis();
if (!ProcessUtil.waitFor(setProcess(framework.restartCampaign()), remaining,
TimeUnit.MILLISECONDS)) {
return;
}
while (endTime > System.currentTimeMillis()) {
long remaining = endTime - System.currentTimeMillis();
if (forkAndWait(framework, remaining)) {
return;
}
}
}

private synchronized Process setProcess(Process process) {
this.process = process;
return this.process;
private boolean forkAndWait(FuzzFramework framework, long timeout)
throws IOException, MojoExecutionException, InterruptedException {
synchronized (this) {
if (shutdown) {
return true;
} else if (process == null) {
process = framework.startCampaign();
} else if (!framework.canRestartCampaign()) {
throw new MojoExecutionException("Campaign process terminated unexpectedly");
} else {
process = framework.restartCampaign();
}
}
return ProcessUtil.waitFor(process, timeout, TimeUnit.MILLISECONDS);
}

private synchronized void shutdown() {
shutdown = true;
if (process != null) {
try {
ProcessUtil.stop(process);
Expand Down

0 comments on commit 9867c72

Please sign in to comment.