{"record":{"id":"45147617b3ae0fc7","repo":"kopia/kopia","slug":"error-writing-temporary-file","errorCode":null,"errorMessage":"error writing temporary file","messagePattern":"error writing temporary file","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"repo/blob/sftp/sftp_storage.go","lineNumber":359,"sourceCode":"\to := s.Impl.(*sftpImpl).Options //nolint:forcetypeassert\n\treturn fmt.Sprintf(\"SFTP %v@%v\", o.Username, o.Host)\n}\n\nfunc (s *sftpStorage) Close(ctx context.Context) error {\n\ts.Impl.(*sftpImpl).rec.CloseActiveConnection(ctx) //nolint:forcetypeassert\n\treturn nil\n}\n\nfunc writeKnownHostsDataStringToTempFile(data string) (string, error) {\n\ttf, err := os.CreateTemp(\"\", \"kopia-known-hosts\")\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"error creating temp file\")\n\t}\n\n\tdefer tf.Close() //nolint:errcheck\n\n\tif _, err := tf.WriteString(data); err != nil {\n\t\treturn \"\", errors.Wrap(err, \"error writing temporary file\")\n\t}\n\n\treturn tf.Name(), nil\n}\n\n// getHostKeyCallback returns a HostKeyCallback that validates the connected host based on KnownHostsFile or KnownHostsData.\nfunc getHostKeyCallback(opt *Options) (ssh.HostKeyCallback, error) {\n\tif opt.KnownHostsData != \"\" {\n\t\t// if known hosts data is provided, it takes precedence of KnownHostsFile\n\t\t// We need to write to temporary file so we can parse, unfortunately knownhosts.New() only accepts\n\t\t// file names, but known_hosts data is not really sensitive so it can be briefly written to disk.\n\t\ttmpFile, err := writeKnownHostsDataStringToTempFile(opt.KnownHostsData)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\t// this file is no longer needed after `knownhosts.New` returns, so we can delete it.\n\t\tdefer os.Remove(tmpFile) //nolint:errcheck","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/kopia/kopia/blob/82495e54b584c1ef6073c9e1be048f57f8aef078/repo/blob/sftp/sftp_storage.go#L341-L377","documentation":"After successfully creating the temp known-hosts file, writeKnownHostsDataStringToTempFile writes the KnownHostsData string into it with tf.WriteString. If that write fails, the error is wrapped as 'error writing temporary file'. This indicates the local filesystem rejected the write after the file was created.","triggerScenarios":"Calling getHostKeyCallback with non-empty KnownHostsData when tf.WriteString(data) fails — typically ENOSPC (disk full) on the temp filesystem, an I/O error, or the file being closed/invalidated externally mid-write.","commonSituations":"tmpfs too small for the write (rare — known_hosts data is small, so usually disk-full or I/O errors); hardware/storage issues in containers; quota enforcement on the temp directory.","solutions":["Free disk space on the filesystem holding the temp directory (usually /tmp or $TMPDIR).","Check dmesg/system logs for I/O errors on the backing device.","Set TMPDIR to a different, healthy filesystem and retry.","Switch to KnownHostsFile (absolute path) instead of KnownHostsData to avoid the temp-file write path."],"exampleFix":"// before (KnownHostsData forces temp-file write)\nopt.KnownHostsData = knownHostsLine\n// after (write the file yourself once, reference it)\nos.WriteFile(\"/etc/kopia/known_hosts\", []byte(knownHostsLine), 0o600)\nopt.KnownHostsFile = \"/etc/kopia/known_hosts\"","handlingStrategy":"validation","validationCode":"if err := syscallAccess(os.TempDir()); err != nil {\n    return fmt.Errorf(\"temp dir not writable: %v\", err)\n}\ndf, _ := checkFreeSpace(os.TempDir())\nif df < 1<<20 { return errors.New(\"less than 1MB free in temp dir\") }","typeGuard":null,"tryCatchPattern":"tmp, err := writeKnownHostsDataStringToTempFile(data)\nif err != nil && strings.Contains(err.Error(), \"error writing temporary file\") {\n    // disk-full / IO error: fall back to KnownHostsFile\n    return knownhosts.New(\"/etc/kopia/known_hosts\")\n}","preventionTips":["Keep the temp filesystem from filling up; monitor with df alerts.","Prefer KnownHostsFile to avoid writing known_hosts data to disk at all.","Investigate I/O errors promptly (dmesg) — writes rarely fail for other reasons."],"tags":["filesystem","temp-file","ssh","known-hosts"],"backgroundTag":"file-write-failed","analyzedSha":"82495e54b584c1ef6073c9e1be048f57f8aef078","analyzedAt":"2026-09-07T20:35:21.689Z","contentChangedAt":"2026-09-07T20:35:21.689Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}