hashicorp/nomad · error

failed to set driver state: %v

Error message

failed to set driver state: %v

What it means

After building the task handle, StartTask persists the mock driver's TaskState via handle.SetDriverState. If serialization/storage of the driver state fails, the task is not registered and this error wraps the underlying cause.

Source

Thrown at drivers/mock/driver.go:516

		logger:          d.logger.With("task_name", cfg.Name),
		waitCh:          make(chan interface{}),
		killCh:          killCtx.Done(),
		kill:            killCancel,
		startedAt:       time.Now(),
	}

	driverState := MockTaskState{
		StartedAt:       h.startedAt,
		Command:         driverConfig.Command,
		ExecCommand:     driverConfig.ExecCommand,
		PluginExitAfter: driverConfig.pluginExitAfterDuration,
		KillAfter:       driverConfig.killAfterDuration,
	}
	handle := drivers.NewTaskHandle(taskHandleVersion)
	handle.Config = cfg
	if err := handle.SetDriverState(&driverState); err != nil {
		d.logger.Error("failed to start task, error setting driver state", "error", err, "task_name", cfg.Name)
		return nil, nil, fmt.Errorf("failed to set driver state: %v", err)
	}

	d.tasks.Set(cfg.ID, h)

	d.logger.Debug("starting task", "task_name", cfg.Name)
	go h.run()
	return handle, net, nil

}

func (d *Driver) WaitTask(ctx context.Context, taskID string) (<-chan *drivers.ExitResult, error) {
	handle, ok := d.tasks.Get(taskID)
	if !ok {
		return nil, drivers.ErrTaskNotFound
	}

	ch := make(chan *drivers.ExitResult)
	go d.handleWait(ctx, handle, ch)

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Inspect the logged 'error setting driver state' message for the root cause.
  2. Retry StartTask; transient state-store failures typically resolve on re-attempt.
  3. If it persists after an upgrade, clean the client's allocation/state data for the affected task.
Defensive patterns

Strategy: try-catch

Try / catch

h, net, err := drv.StartTask(ctx, cfg)
if err != nil {
    if strings.Contains(err.Error(), "failed to set driver state") {
        // inspect client logs for 'error setting driver state', then retry
        h, net, err = drv.StartTask(ctx, cfg)
    }
    if err != nil {
        return err
    }
}

Prevention

When it happens

Trigger: handle.SetDriverState returning an error during StartTask — e.g. the TaskHandle's state store failing to encode or persist the driverState struct.

Common situations: Internal client state-store corruption; incompatible driver state schema after a Nomad client upgrade/restoring an old allocation directory; disk issues on the client.

Related errors


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