Skip to content

Commit

Permalink
client: ensure task only runs with prestart hooks (#18662)
Browse files Browse the repository at this point in the history
Since the allocation in the task runner is updated in a separate
goroutine, a race condition may happen where the task is started but the
prestart hooks are skipped because the allocation became terminal.

Checking for a terminal allocation before proceeding with the task start
ensures the task only runs if the prestart hooks are also executed.

Since `shouldShutdown()` only uses terminal allocation status, it
remains `true` after the first transition, so it's safe to check it
again after the prestart hooks as it will never revert to `false`.
  • Loading branch information
lgfa29 committed Oct 5, 2023
1 parent d701925 commit ed204e0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/18662.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
client: prevent tasks from starting without the prestart hooks running
```
6 changes: 6 additions & 0 deletions client/allocrunner/taskrunner/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,12 @@ MAIN:
goto RESTART
}

// Check for a terminal allocation once more before proceeding as the
// prestart hooks may have been skipped.
if tr.shouldShutdown() {
break MAIN
}

select {
case <-tr.killCtx.Done():
break MAIN
Expand Down

0 comments on commit ed204e0

Please sign in to comment.