Skip to content

Commit 5a5e32b

Browse files
committed
Fix concurrency issue in Scheduler.cancel
1 parent 70fd477 commit 5a5e32b

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/main/java/com/coreoz/wisp/Scheduler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ public CompletionStage<Job> cancel(String jobName) {
235235
Job job = findJob(jobName).orElseThrow(IllegalArgumentException::new);
236236

237237
synchronized (this) {
238-
if(job.status() == JobStatus.DONE) {
238+
JobStatus jobStatus = job.status();
239+
if(jobStatus == JobStatus.DONE) {
239240
return CompletableFuture.completedFuture(job);
240241
}
241242
CompletableFuture<Job> existingHandle = cancelHandles.get(jobName);
@@ -244,15 +245,15 @@ public CompletionStage<Job> cancel(String jobName) {
244245
}
245246

246247
job.schedule(Schedule.willNeverBeExecuted);
247-
if(job.status() == JobStatus.READY && threadPoolExecutor.remove(job.runningJob())) {
248+
if(jobStatus == JobStatus.READY && threadPoolExecutor.remove(job.runningJob())) {
248249
scheduleNextExecution(job);
249250
return CompletableFuture.completedFuture(job);
250251
}
251252

252-
if(job.status() == JobStatus.RUNNING
253+
if(jobStatus == JobStatus.RUNNING
253254
// if the job status is/was READY but could not be removed from the thread pool,
254255
// then we have to wait for it to finish
255-
|| job.status() == JobStatus.READY) {
256+
|| jobStatus == JobStatus.READY) {
256257
CompletableFuture<Job> promise = new CompletableFuture<>();
257258
cancelHandles.put(jobName, promise);
258259
return promise;

0 commit comments

Comments
 (0)