{"record":{"id":"8152b5545817f34e","repo":"kubernetes/kops","slug":"creating-session-for-rename-w","errorCode":null,"errorMessage":"creating session for rename: %w","messagePattern":"creating session for rename: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"util/pkg/vfs/sshfs.go","lineNumber":251,"sourceCode":"\tusePosixRename := true\n\tif usePosixRename {\n\t\t// posix rename fails if destination exists, try to delete just in case\n\t\tif err := sftpClient.Remove(p.path); err != nil {\n\t\t\tif os.IsNotExist(err) {\n\t\t\t\t// expected when file does not exist already\n\t\t\t} else {\n\t\t\t\treturn fmt.Errorf(\"removing destination sftp file %q before rename: %w\", p.path, err)\n\t\t\t}\n\t\t}\n\t\tif err := sftpClient.Rename(tempfile, p.path); err != nil {\n\t\t\treturn fmt.Errorf(\"renaming sftp file %q -> %q (with posix rename): %w\", tempfile, p.path, err)\n\t\t}\n\t\tdeleteTempFile = false\n\t} else {\n\t\tvar session *ssh.Session\n\t\tsession, err = p.client.NewSession()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"creating session for rename: %w\", err)\n\t\t}\n\t\tdefer session.Close()\n\n\t\tcmd := \"mv \" + tempfile + \" \" + p.path\n\t\tif p.sudo {\n\t\t\tcmd = \"sudo \" + cmd\n\t\t}\n\t\tif err := session.Run(cmd); err != nil {\n\t\t\treturn fmt.Errorf(\"renaming file %q -> %q (with %q): %w\", tempfile, p.path, cmd, err)\n\t\t}\n\t\tdeleteTempFile = false\n\t}\n\n\treturn nil\n}\n\n// To prevent concurrent creates on the same file while maintaining atomicity of writes,\n// we take a process-wide lock during the operation.","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/util/pkg/vfs/sshfs.go#L233-L269","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-establish the SSH connection and retry — verify basic connectivity: `ssh <host> true`.","Check sshd MaxSessions on the target host and raise it if constrained: `MaxSessions 10` in sshd_config.","Enable SSH keepalives (ServerAliveInterval) to prevent idle connection teardown.","If the node was rebooted, wait for sshd to come up and retry the operation."],"exampleFix":"// before: sshd_config\nMaxSessions 2\n// after\nMaxSessions 10\n$ sudo systemctl reload sshd","handlingStrategy":"retry","validationCode":"// verify a fresh SSH channel can be opened before the operation\nclient, err := ssh.Dial(\"tcp\", host+\":22\", sshConfig)\nif err != nil { return err }\ns, err := client.NewSession()\nif err != nil { return fmt.Errorf(\"cannot open ssh session: %w\", err) }\ns.Close()","typeGuard":null,"tryCatchPattern":"err := path.WriteFile(ctx, data, acl)\nif err != nil && strings.Contains(err.Error(), \"creating session for rename\") {\n    client.Close()\n    client = redialSSH(host, sshConfig) // replace stale connection\n    path = vfs.NewSSHPath(client, server, targetPath, sudo)\n    return path.WriteFile(ctx, data, acl)\n}","preventionTips":["Redial rather than reusing long-lived cached SSH clients.","Set ServerAliveInterval/CountMax to keep idle connections alive.","Check sshd MaxSessions if you open many channels concurrently."],"tags":["ssh","session","connection","rename"],"backgroundTag":"ssh-session-open-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}