diff --git a/agent/task.go b/agent/task.go index 797f93c769..e7625adeac 100644 --- a/agent/task.go +++ b/agent/task.go @@ -201,6 +201,17 @@ func (tm *taskManager) run(ctx context.Context) { } case <-shutdown: if cancel != nil { + // Wait for operation to finish if the task + // is in preparing state. This avoid the race + // between Prepare and Remove operations, + // which could cause orphan containers. + if tm.task.Status.State == api.TaskStatePreparing { + select { + case <-errs: + case <-statusq: + } + } + // cancel outstanding operation. cancel() }