{"record":{"id":"e11520d8395bd5a3","repo":"nats-io/nats-server","slug":"failed-to-write-to-temporary-file-w","errorCode":null,"errorMessage":"failed to write to temporary file: %w","messagePattern":"failed to write to temporary file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"server/filestore.go","lineNumber":8062,"sourceCode":"\t\t// We only need to write out the metadata header if compression is enabled.\n\t\t// If we're trying to uncompress the file on disk at this point, don't bother\n\t\t// writing metadata.\n\t\tmeta := &CompressionInfo{\n\t\t\tAlgorithm:    alg,\n\t\t\tOriginalSize: uint64(originalSize),\n\t\t}\n\t\tbuf = append(meta.MarshalMetadata(), buf...)\n\t}\n\n\t// Re-encrypt the block if necessary.\n\tif err = mb.encryptOrDecryptIfNeeded(buf); err != nil {\n\t\treturn errorCleanup(err)\n\t}\n\n\t// Write the new block data (which might be compressed or encrypted) to the\n\t// temporary file.\n\tif n, err := tmpFD.Write(buf); err != nil {\n\t\treturn errorCleanup(fmt.Errorf(\"failed to write to temporary file: %w\", err))\n\t} else if n != len(buf) {\n\t\treturn errorCleanup(fmt.Errorf(\"short write to temporary file (%d != %d)\", n, len(buf)))\n\t}\n\tif err := tmpFD.Sync(); err != nil {\n\t\treturn errorCleanup(fmt.Errorf(\"failed to sync temporary file: %w\", err))\n\t}\n\tif err := tmpFD.Close(); err != nil {\n\t\treturn errorCleanup(fmt.Errorf(\"failed to close temporary file: %w\", err))\n\t}\n\n\t// Now replace the original file with the newly updated temp file.\n\tif err := os.Rename(tmpFN, origFN); err != nil {\n\t\treturn fmt.Errorf(\"failed to move temporary file into place: %w\", err)\n\t}\n\n\t// Since the message block might be retained in memory, make sure the\n\t// compression algorithm is up-to-date, since this will be needed when\n\t// compacting or truncating.","sourceCodeStart":8044,"sourceCodeEnd":8080,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/filestore.go#L8044-L8080","documentation":"Returned when the file store fails to write the (possibly compressed/encrypted) block buffer to the temporary file during a block update. The write to tmpFD returned an OS-level error, so the update is aborted via errorCleanup. The original file remains untouched.","triggerScenarios":"tmpFD.Write(buf) returns a non-nil error while writing new block data to the temp file during a message block update (writeMsgs/update of a file-store stream).","commonSituations":"Disk full (ENOSPC), I/O errors on failing disks, temp directory on a full/unmounted filesystem, file descriptor limits hit, or permissions changed on the stream's data directory.","solutions":["Check free disk space on the stream's storage directory (df -h).","Check filesystem health and mount status; dmesg for I/O errors.","Verify permissions on the store directory and that no ulimit (nofile) is exhausted.","Retry the publish/update once space is freed; errorCleanup removed the temp file so no manual cleanup is needed."],"exampleFix":"// before: publish while disk is 100% full\nnc.Publish(subj, payload)\n// after: ensure capacity before writing, or monitor\n// df -h /path/to/jetstream  ; free space or relocate store dir","handlingStrategy":"validation","validationCode":"// before writing, check available space on the store volume\nusage := diskUsage(storeDir)\nif usage.Free < requiredBytes {\n    return fmt.Errorf(\"insufficient disk space: %d free\", usage.Free)\n}","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"failed to write to temporary file\") {\n    // alert ops: check disk space / I/O on store volume\n}","preventionTips":["Alert on disk usage thresholds (e.g. 80%) for the JetStream store volume.","Use reliable local storage, not network filesystems.","Monitor fd limits and directory permissions.","Set up log monitoring for ENOSPC and I/O errors."],"tags":["filestore","filesystem","io","jetstream","go"],"backgroundTag":"disk-full-write-failed","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}