{"record":{"id":"c89dcb993a55244b","repo":"nats-io/nats-server","slug":"failed-to-create-temporary-file-w","errorCode":null,"errorMessage":"failed to create temporary file: %w","messagePattern":"failed to create temporary file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/filestore.go","lineNumber":8024,"sourceCode":"func (mb *msgBlock) atomicOverwriteFile(buf []byte, allowCompress bool) error {\n\tif mb.mfd != nil {\n\t\tmb.closeFDsLockedNoCheck()\n\t\tdefer mb.enableForWriting(mb.fs.fip)\n\t}\n\n\torigFN := mb.mfn               // The original message block on disk.\n\ttmpFN := mb.mfn + blkTmpSuffix // The new block will be written here.\n\n\t// Rather than modifying the existing block on disk (which is a dangerous\n\t// operation if something goes wrong), create a new temporary file. We will\n\t// write out the new block here and then swap the files around afterwards\n\t// once everything else has succeeded correctly.\n\tmb.fs.dios.acquire()\n\ttmpFD, err := os.OpenFile(tmpFN, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, defaultFilePerms)\n\tmb.fs.dios.release()\n\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create temporary file: %w\", err)\n\t}\n\n\terrorCleanup := func(err error) error {\n\t\t_ = tmpFD.Close()\n\t\t_ = os.Remove(tmpFN)\n\t\treturn err\n\t}\n\n\talg := NoCompression\n\tif calg := mb.fs.fcfg.Compression; calg != NoCompression && allowCompress {\n\t\talg = calg\n\t\t// The original buffer at this point is uncompressed, so we will now compress\n\t\t// it if needed. Note that if the selected algorithm is NoCompression, the\n\t\t// Compress function will just return the input buffer unmodified.\n\t\toriginalSize := len(buf)\n\t\tif buf, err = alg.Compress(buf); err != nil {\n\t\t\treturn errorCleanup(fmt.Errorf(\"failed to compress block: %w\", err))\n\t\t}","sourceCodeStart":8006,"sourceCodeEnd":8042,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/filestore.go#L8006-L8042","documentation":"Returned during atomic block overwrite (used by recompression) when creating the temporary file in the block directory fails. The rewrite strategy is write-to-temp-then-rename for atomicity; if the temp file cannot be created, the operation aborts without touching the original block.","triggerScenarios":"os.OpenFile(tmpFN, O_CREATE|O_TRUNC|O_WRONLY, ...) failing during recompression/overwrite: read-only filesystem, no write permission in the msgblk directory, disk full (ENOSPC), or too many open files.","commonSituations":"Datadir volume full during a compression migration; directory permissions changed by external tooling; running container with a read-only rootfs while jetstream data lives on it; EMFILE from many concurrent block operations.","solutions":["Check the wrapped cause: run df -h for ENOSPC or fix directory permissions for EACCES","Ensure the jetstream datadir is on a writable, non-full volume","Raise the fd limit if too many open files (ulimit -n / LimitNOFILE)","Retry the recompression after storage is healthy; the original block is untouched"],"exampleFix":"# ENOSPC creating temp file during recompress\ndf -h /var/lib/nats\n# free space or expand volume, then retry:\nnats stream update ORDERS --compression=s2","handlingStrategy":"validation","validationCode":"// Go: pre-flight writable-space check before triggering recompression\nif free, err := diskFree(blockDir); err != nil || free < blockSize*2 {\n    return fmt.Errorf(\"need 2x block size free in %s for atomic overwrite\", blockDir)\n}\nif err := probeWritable(blockDir); err != nil {\n    return fmt.Errorf(\"block dir not writable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"failed to create temporary file\") {\n    switch {\n    case strings.Contains(err.Error(), \"no space left\"):\n        return freeSpaceThenRetry(cfg)\n    case strings.Contains(err.Error(), \"permission denied\"):\n        return fixDirPermsThenRetry(blockDir)\n    default:\n        return err\n    }\n}","preventionTips":["Alert on disk usage (80%+) before scheduling compression changes","Mount jetstream storage read-write with stable ownership","Raise fd limits for servers doing many concurrent block rewrites","Remember the original block is left intact — safe to retry after fixing storage"],"tags":["jetstream","filestore","disk-full","permissions","temp-file"],"backgroundTag":"disk-full-flush-failed","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}