{"record":{"id":"4b56c05ff46dcc40","repo":"hashicorp/nomad","slug":"failed-to-create-snapshot-file-v","errorCode":null,"errorMessage":"failed to create snapshot file: %v","messagePattern":"failed to create snapshot file: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"helper/snapshot/snapshot.go","lineNumber":100,"sourceCode":"\t}\n\n\treturn writeSnapshot(logger, metadata, snap)\n}\n\nfunc writeSnapshot(logger hclog.Logger, metadata *raft.SnapshotMeta, snap io.ReadCloser) (*Snapshot, error) {\n\n\tdefer func() {\n\t\tif err := snap.Close(); err != nil {\n\t\t\tlogger.Error(\"Failed to close Raft snapshot\", \"error\", err)\n\t\t}\n\t}()\n\n\t// Make a scratch file to receive the contents so that we don't buffer\n\t// everything in memory. This gets deleted in Close() since we keep it\n\t// around for re-reading.\n\tarchive, err := os.CreateTemp(\"\", \"snapshot\")\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create snapshot file: %v\", err)\n\t}\n\n\t// If anything goes wrong after this point, we will attempt to clean up\n\t// the temp file. The happy path will disarm this.\n\tvar keep bool\n\tdefer func() {\n\t\tif keep {\n\t\t\treturn\n\t\t}\n\n\t\tif err := os.Remove(archive.Name()); err != nil {\n\t\t\tlogger.Error(\"Failed to clean up temp snapshot\", \"error\", err)\n\t\t}\n\t}()\n\n\thash := sha256.New()\n\tout := io.MultiWriter(hash, archive)\n","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/helper/snapshot/snapshot.go#L82-L118","documentation":"writeSnapshot wraps os.CreateTemp failures when creating the temporary file that receives the snapshot archive. The library streams the snapshot to disk rather than buffering it in memory, so the very first step is creating this scratch file; if the OS cannot create it, snapshot creation (via New or NewFromFSM) aborts with this message. The temp file is normally deleted in Close() unless the snapshot succeeds.","triggerScenarios":"Calling New or NewFromFSM when os.CreateTemp(\"\", \"snapshot\") fails: no writable temp directory, TMPDIR pointing to a nonexistent or read-only path, disk full (no inodes/space), or sandboxed environments restricting /tmp access.","commonSituations":"Containers with a read-only root filesystem or tiny tmpfs; TMPDIR set to a path that doesn't exist; running under security profiles (seccomp/AppArmor) that block temp file creation; disk quota exhausted on the node.","solutions":["Check disk space and inode availability on the temp filesystem (df -h /tmp, df -i).","Verify the TMPDIR environment variable points to an existing, writable directory, or fix it to a valid path.","Ensure the process runs with permission to create files in the temp directory (mount a writable volume in containers).","If /tmp is noexec/read-only, set TMPDIR to a writable location such as the data directory before starting the process."],"exampleFix":"// before: TMPDIR=/nonexistent ./app  →  failed to create snapshot file: open /nonexistent/...: no such file or directory\n// after\nexport TMPDIR=/var/lib/myapp/tmp   # existing, writable directory\nmkdir -p $TMPDIR && ./app","handlingStrategy":"validation","validationCode":"func ensureTempWritable() error {\n\td := os.TempDir()\n\tif st, err := os.Stat(d); err != nil || !st.IsDir() {\n\t\treturn fmt.Errorf(\"temp dir %q missing: %v\", d, err)\n\t}\n\tf, err := os.CreateTemp(d, \"probe\")\n\tif err != nil { return err }\n\tname := f.Name(); f.Close(); os.Remove(name)\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":"snap, err := snapshot.New(...) // or NewFromFSM\nif err != nil && strings.Contains(err.Error(), \"failed to create snapshot file\") {\n\tlogger.Error(\"snapshot temp file creation failed; check TMPDIR/disk\", \"err\", err)\n\treturn err\n}","preventionTips":["Point TMPDIR at an existing, writable local directory before starting.","Monitor free space and inodes on the temp volume.","Grant the service account write access to the temp dir; avoid read-only container rootfs.","Probe temp writability at startup and fail fast with a clear config message."],"tags":["filesystem","io","snapshot","disk"],"backgroundTag":"temp-file-creation-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}