kubernetes/kops · error

creating session for rename: %w

Error message

creating session for rename: %w

What it means

In the non-posix-rename fallback path, WriteFile opens a new SSH session on the existing client to run an `mv` command. If p.client.NewSession() fails — typically because the underlying SSH connection is dead or the server refuses new channels — this error is returned before any rename is attempted.

Source

Thrown at util/pkg/vfs/sshfs.go:251

	usePosixRename := true
	if usePosixRename {
		// posix rename fails if destination exists, try to delete just in case
		if err := sftpClient.Remove(p.path); err != nil {
			if os.IsNotExist(err) {
				// expected when file does not exist already
			} else {
				return fmt.Errorf("removing destination sftp file %q before rename: %w", p.path, err)
			}
		}
		if err := sftpClient.Rename(tempfile, p.path); err != nil {
			return fmt.Errorf("renaming sftp file %q -> %q (with posix rename): %w", tempfile, p.path, err)
		}
		deleteTempFile = false
	} else {
		var session *ssh.Session
		session, err = p.client.NewSession()
		if err != nil {
			return fmt.Errorf("creating session for rename: %w", err)
		}
		defer session.Close()

		cmd := "mv " + tempfile + " " + p.path
		if p.sudo {
			cmd = "sudo " + cmd
		}
		if err := session.Run(cmd); err != nil {
			return fmt.Errorf("renaming file %q -> %q (with %q): %w", tempfile, p.path, cmd, err)
		}
		deleteTempFile = false
	}

	return nil
}

// To prevent concurrent creates on the same file while maintaining atomicity of writes,
// we take a process-wide lock during the operation.

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Re-establish the SSH connection and retry — verify basic connectivity: `ssh <host> true`.
  2. Check sshd MaxSessions on the target host and raise it if constrained: `MaxSessions 10` in sshd_config.
  3. Enable SSH keepalives (ServerAliveInterval) to prevent idle connection teardown.
  4. If the node was rebooted, wait for sshd to come up and retry the operation.

Example fix

// before: sshd_config
MaxSessions 2
// after
MaxSessions 10
$ sudo systemctl reload sshd
Defensive patterns

Strategy: retry

Validate before calling

// verify a fresh SSH channel can be opened before the operation
client, err := ssh.Dial("tcp", host+":22", sshConfig)
if err != nil { return err }
s, err := client.NewSession()
if err != nil { return fmt.Errorf("cannot open ssh session: %w", err) }
s.Close()

Try / catch

err := path.WriteFile(ctx, data, acl)
if err != nil && strings.Contains(err.Error(), "creating session for rename") {
    client.Close()
    client = redialSSH(host, sshConfig) // replace stale connection
    path = vfs.NewSSHPath(client, server, targetPath, sudo)
    return path.WriteFile(ctx, data, acl)
}

Prevention

When it happens

Trigger: usePosixRename is false and p.client.NewSession() errors: SSH connection to the target host timed out/closed, server hit MaxSessions limit, or the client was constructed against a host that has since become unreachable.

Common situations: Long-running kOps operations where the cached SSH client connection went stale (NAT/firewall idle timeout); sshd MaxSessions=0 or heavily restricted; node rebooted mid-operation.

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/8152b5545817f34e. Report an issue: GitHub.