{"record":{"id":"a87cb635bc367358","repo":"hashicorp/nomad","slug":"cannot-destroy-running-task-a87cb6","errorCode":null,"errorMessage":"cannot destroy running task","messagePattern":"cannot destroy running task","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"drivers/rawexec/driver.go","lineNumber":556,"sourceCode":"\t}\n\n\t// Wait for handle to finish\n\t<-handle.doneCh\n\n\t// Kill executor\n\thandle.pluginClient.Kill()\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":538,"sourceCodeEnd":574,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/drivers/rawexec/driver.go#L538-L574","documentation":"DestroyTask cleans up a finished task's resources. raw_exec refuses to destroy a task whose handle still reports IsRunning() unless force=true, returning 'cannot destroy running task'. This protects live processes from being torn down accidentally.","triggerScenarios":"Calling DestroyTask(taskID, false) (force=false) on a task whose handle is still marked running — the executor has not reported exit yet.","commonSituations":"Scripts/garbage collectors calling DestroyTask too early after StopTask; tasks that failed to stop within kill_timeout so the handle still appears running; GC racing with a slow-shutting task.","solutions":["Wait for the task to fully exit (StopTask's doneCh / handle done) before calling DestroyTask","Call DestroyTask with force=true if you intend to kill it regardless","Check client logs to see why the task is still considered running (executor hung? signal ignored?)","Restart the client if the handle is stuck running though the process is gone"],"exampleFix":"// before\nerr := driver.DestroyTask(taskID, false)\n// after\nif err := driver.StopTask(taskID, 30*time.Second, \"SIGTERM\"); err != nil { log.Warn(err) }\nerr := driver.DestroyTask(taskID, true)","handlingStrategy":"validation","validationCode":"if handle.IsRunning() {\n    // stop first or use force\n}\ndriver.DestroyTask(taskID, force)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always StopTask and wait for exit before DestroyTask","Pass force=true only when intentional teardown is desired","Track task state to avoid racing GC with active tasks"],"tags":["lifecycle","task-destroy","nomad","rawexec"],"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"}