{"record":{"id":"3ed3321b83f27b6f","repo":"hashicorp/nomad","slug":"cannot-destroy-running-task-3ed332","errorCode":null,"errorMessage":"cannot destroy running task","messagePattern":"cannot destroy running task","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"drivers/qemu/driver.go","lineNumber":772,"sourceCode":"\t// not be around when we call exec.shutdown\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":754,"sourceCodeEnd":790,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/drivers/qemu/driver.go#L754-L790","documentation":"DestroyTask in the QEMU driver refuses to destroy a task whose handle reports IsRunning() unless force=true. This is a safety guard preventing destruction of a live task without an explicit forced request.","triggerScenarios":"Calling DestroyTask(taskID, false) while the QEMU task handle is still running. Only DestroyTask(taskID, true) bypasses the check.","commonSituations":"Task cleanup after a failed StopTask where the process never exited; scripts calling DestroyTask without stopping the task first; WaitResponse not yet received by the handle.","solutions":["Call StopTask first and wait for the task to exit, then DestroyTask","Call DestroyTask with force=true to kill and destroy the running task","Verify the QEMU process state; if it hung, force-destroy and investigate why shutdown timed out"],"exampleFix":"// before\nerr := driver.DestroyTask(taskID, false) // cannot destroy running task\n// after\nif err := driver.StopTask(taskID, 30*time.Second, \"SIGTERM\"); err != nil {\n    _ = driver.DestroyTask(taskID, true) // force\n} else {\n    _ = driver.DestroyTask(taskID, false)\n}","handlingStrategy":"validation","validationCode":"// Go: check running state before destroy\nif h, ok := drv.tasks.Get(taskID); ok && h.IsRunning() {\n    // must stop first or use force=true\n}","typeGuard":null,"tryCatchPattern":"if err := drv.DestroyTask(id, false); err != nil && strings.Contains(err.Error(), \"cannot destroy running task\") {\n    err = drv.DestroyTask(id, true)\n}","preventionTips":["Stop tasks before destroying them","Use force=true only when intentional","Track task lifecycle state in your tooling"],"tags":["qemu","lifecycle","destroy","nomad-driver"],"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-08T15:18:49.778Z"}