hashicorp/packer · warning

Disconnect while removing temporary script.

Error message

Disconnect while removing temporary script.

What it means

Inside the cleanup retry loop, if the `rm -f` command's exit status is CmdDisconnect (2300218), the provisioner returns this error so the surrounding retry.Config loop treats the disconnect as retryable and re-attempts the removal until StartRetryTimeout expires. It is an internal retry signal, surfaced to the user only if retries are exhausted.

Source

Thrown at provisioner/shell/provisioner.go:411

	return nil
}

func (p *Provisioner) cleanupRemoteFile(path string, comm packersdk.Communicator) error {
	ctx := context.TODO()
	err := retry.Config{StartTimeout: p.config.StartRetryTimeout}.Run(ctx, func(ctx context.Context) error {
		cmd := &packersdk.RemoteCmd{
			Command: fmt.Sprintf("rm -f %s", path),
		}
		if err := comm.Start(ctx, cmd); err != nil {
			return fmt.Errorf(
				"Error removing temporary script at %s: %s",
				path, err)
		}
		cmd.Wait()
		// treat disconnects as retryable by returning an error
		if cmd.ExitStatus() == packersdk.CmdDisconnect {
			return fmt.Errorf("Disconnect while removing temporary script.")
		}
		if cmd.ExitStatus() != 0 {
			return fmt.Errorf(
				"Error removing temporary script at %s!",
				path)
		}
		return nil
	})

	if err != nil {
		return err
	}

	return nil
}

func (p *Provisioner) escapeEnvVars() ([]string, map[string]string) {
	envVars := make(map[string]string)

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Give the retry more room: increase start_retry_timeout so cleanup survives the reboot window
  2. Set skip_clean = true to skip remote file cleanup entirely when acceptable
  3. Ensure scripts finish any reboot and the machine is reachable before provisioning ends
  4. Investigate the underlying connection instability (keepalives, security groups, sshd settings)
Defensive patterns

Strategy: retry

Try / catch

// Cleanup already treats disconnects as retryable:
err := retry.Config{StartTimeout: p.config.StartRetryTimeout}.Run(ctx, func(ctx context.Context) error {
    if cmd.ExitStatus() == packersdk.CmdDisconnect {
        return fmt.Errorf("Disconnect while removing temporary script.") // retry signal
    }
    return nil
})

Prevention

When it happens

Trigger: The remote command `rm -f <path>` completes with the disconnect sentinel exit status — typically because the machine rebooted (script-triggered) or the connection dropped while cleanup ran.

Common situations: Shell scripts that reboot the machine, causing cleanup to race the reboot; unstable network to the instance at end of provisioning; hosts that terminate sessions aggressively.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/c32cfd69f66929db. Report an issue: GitHub.