{"record":{"id":"db0d1566b1dbdbeb","repo":"hashicorp/nomad","slug":"cannot-destroy-running-task","errorCode":null,"errorMessage":"cannot destroy running task","messagePattern":"cannot destroy running task","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"drivers/exec/driver.go","lineNumber":643,"sourceCode":"\n\tif err := handle.exec.Shutdown(signal, timeout); err != nil {\n\t\tif handle.pluginClient.Exited() {\n\t\t\treturn nil\n\t\t}\n\t\treturn fmt.Errorf(\"executor Shutdown failed: %v\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (d *Driver) DestroyTask(taskID string, force bool) error {\n\thandle, ok := d.tasks.Get(taskID)\n\tif !ok {\n\t\treturn drivers.ErrTaskNotFound\n\t}\n\n\tif handle.IsRunning() && !force {\n\t\treturn fmt.Errorf(\"cannot destroy running task\")\n\t}\n\n\tif !handle.pluginClient.Exited() {\n\t\tif err := handle.exec.Shutdown(\"\", 0); err != nil {\n\t\t\thandle.logger.Error(\"destroying executor failed\", \"error\", err)\n\t\t}\n\n\t\thandle.pluginClient.Kill()\n\t}\n\n\td.tasks.Delete(taskID)\n\treturn nil\n}\n\nfunc (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) {\n\thandle, ok := d.tasks.Get(taskID)\n\tif !ok {\n\t\treturn nil, drivers.ErrTaskNotFound","sourceCodeStart":625,"sourceCodeEnd":661,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/drivers/exec/driver.go#L625-L661","documentation":"DestroyTask removes a task's resources and process. As a safety measure, if the task is still running and force is false, the driver refuses to destroy it and returns this error, preventing accidental loss of a live task.","triggerScenarios":"Calling driver.DestroyTask(taskID, false) while handle.IsRunning() is true.","commonSituations":"Tooling or plugins calling DestroyTask without first stopping the task; a task that failed to shut down during StopTask followed by a non-forced destroy; race between a check and the destroy call.","solutions":["Call StopTask first and wait for the task to exit, then call DestroyTask.","If immediate teardown is intended, pass force=true to DestroyTask.","Check task state via TaskStatus/inspect to confirm the task has exited before destroying."],"exampleFix":"// before\nerr := driver.DestroyTask(taskID, false)\n// after\nif err := driver.StopTask(taskID, 30*time.Second, \"SIGINT\"); err != nil {\n    handle.logger.Warn(\"stop failed, forcing destroy\", \"error\", err)\n}\nerr := driver.DestroyTask(taskID, true)","handlingStrategy":"validation","validationCode":"status, err := driver.TaskStatus(taskID)\nif err != nil { return err }\nif status.State == drivers.TaskRunning && !force {\n    return fmt.Errorf(\"task %s still running; stop it first\", taskID)\n}\nreturn driver.DestroyTask(taskID, force)","typeGuard":null,"tryCatchPattern":"if err := driver.DestroyTask(taskID, false); err != nil && strings.Contains(err.Error(), \"cannot destroy running task\") {\n    _ = driver.StopTask(taskID, 30*time.Second, \"SIGINT\")\n    err = driver.DestroyTask(taskID, true)\n}","preventionTips":["Always StopTask before DestroyTask in lifecycle tooling.","Check TaskStatus before destroying.","Use force=true only when teardown is explicitly intended."],"tags":["exec-driver","task-lifecycle","nomad"],"backgroundTag":"task-still-running","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}