hashicorp/nomad · error

cannot destroy running task

Error message

cannot destroy running task

What it means

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.

Source

Thrown at drivers/qemu/driver.go:772

	// not be around when we call exec.shutdown
	if err := handle.exec.Shutdown(signal, timeout); err != nil {
		if handle.pluginClient.Exited() {
			return nil
		}
		return fmt.Errorf("executor Shutdown failed: %v", err)
	}

	return nil
}

func (d *Driver) DestroyTask(taskID string, force bool) error {
	handle, ok := d.tasks.Get(taskID)
	if !ok {
		return drivers.ErrTaskNotFound
	}

	if handle.IsRunning() && !force {
		return fmt.Errorf("cannot destroy running task")
	}

	if !handle.pluginClient.Exited() {
		if err := handle.exec.Shutdown("", 0); err != nil {
			handle.logger.Error("destroying executor failed", "error", err)
		}

		handle.pluginClient.Kill()
	}

	d.tasks.Delete(taskID)
	return nil
}

func (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) {
	handle, ok := d.tasks.Get(taskID)
	if !ok {
		return nil, drivers.ErrTaskNotFound

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Call StopTask first and wait for the task to exit, then DestroyTask
  2. Call DestroyTask with force=true to kill and destroy the running task
  3. Verify the QEMU process state; if it hung, force-destroy and investigate why shutdown timed out

Example fix

// before
err := driver.DestroyTask(taskID, false) // cannot destroy running task
// after
if err := driver.StopTask(taskID, 30*time.Second, "SIGTERM"); err != nil {
    _ = driver.DestroyTask(taskID, true) // force
} else {
    _ = driver.DestroyTask(taskID, false)
}
Defensive patterns

Strategy: validation

Validate before calling

// Go: check running state before destroy
if h, ok := drv.tasks.Get(taskID); ok && h.IsRunning() {
    // must stop first or use force=true
}

Try / catch

if err := drv.DestroyTask(id, false); err != nil && strings.Contains(err.Error(), "cannot destroy running task") {
    err = drv.DestroyTask(id, true)
}

Prevention

When it happens

Trigger: Calling DestroyTask(taskID, false) while the QEMU task handle is still running. Only DestroyTask(taskID, true) bypasses the check.

Common situations: 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.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/3ed3321b83f27b6f. Report an issue: GitHub.