{"record":{"id":"ab69b25260539afd","repo":"hashicorp/nomad","slug":"cannot-destroy-running-task-ab69b2","errorCode":null,"errorMessage":"cannot destroy running task","messagePattern":"cannot destroy running task","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"drivers/java/driver.go","lineNumber":653,"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":635,"sourceCodeEnd":671,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/drivers/java/driver.go#L635-L671","documentation":"DestroyTask refuses to destroy a task whose handle reports IsRunning() unless force=true is passed. This is a safety guard so callers cannot accidentally destroy a live task and lose its state; the task must be stopped first, or destruction must be explicitly forced.","triggerScenarios":"Calling Driver.DestroyTask(taskID, force=false) (or equivalent GC path) while handle.IsRunning() is true — the executor is still up and the task has not been stopped — and the driver returns this plain error before any shutdown attempt.","commonSituations":"Operators or tooling calling destroy/GC out of order (destroy before stop), a StopTask that failed earlier leaving the task running, or automation invoking force=false destroy during cleanup.","solutions":["Call StopTask (or DestroyTask with force=true) instead","If you intend to discard a running task, pass force=true — the driver will then shut the executor down before cleanup","If the task is actually dead but still reported running, check plugin client state and force-destroy to clear the stale handle","Fix any earlier StopTask failure (e.g. hung JVM) so normal stop/destroy ordering works"],"exampleFix":"// before: destroys a running task without stopping it\nerr := driver.DestroyTask(taskID, false)\n// after: stop first, then destroy\nif err := driver.StopTask(taskID, \"SIGINT\", 30*time.Second); err != nil {\n    // log and proceed to force-destroy\n}\nerr := driver.DestroyTask(taskID, true)","handlingStrategy":"validation","validationCode":"// check task state before destroying\nif driver.IsRunning(taskID) {\n    if err := driver.StopTask(taskID, \"SIGINT\", 30*time.Second); err != nil {\n        // log and continue to forced destroy\n    }\n}\nerr := driver.DestroyTask(taskID, true)","typeGuard":"func canDestroy(d *drivers.Driver, taskID string, force bool) bool {\n    return force || !d.IsRunning(taskID)\n}","tryCatchPattern":"if err := driver.DestroyTask(taskID, force); err != nil {\n    if strings.Contains(err.Error(), \"cannot destroy running task\") {\n        _ = driver.StopTask(taskID, \"SIGINT\", 30*time.Second)\n        err = driver.DestroyTask(taskID, true)\n    }\n    return err\n}","preventionTips":["Always stop tasks before destroying them","Use force=true only when the intent is to discard a running task","Handle earlier StopTask failures so tasks don't linger as running"],"tags":["nomad","java-driver","task-destroy","lifecycle","guard"],"backgroundTag":"cannot-destroy-running-task","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"}