{"record":{"id":"f08a7eef3fdfafe2","repo":"vxcontrol/pentagi","slug":"failed-to-create-temp-file-w","errorCode":null,"errorMessage":"failed to create temp file: %w","messagePattern":"failed to create temp file: %w","errorType":"exception","errorClass":null,"httpStatus":500,"severity":"error","filePath":"backend/pkg/resources/resources.go","lineNumber":280,"sourceCode":"// EscapeLike escapes special LIKE pattern characters (%, _) in s so the string\n// can be safely embedded in a SQL LIKE clause.\nfunc EscapeLike(s string) string {\n\ts = strings.ReplaceAll(s, `\\`, `\\\\`)\n\ts = strings.ReplaceAll(s, `%`, `\\%`)\n\ts = strings.ReplaceAll(s, `_`, `\\_`)\n\treturn s\n}\n\n// SaveToTemp writes r into a new temporary file in dir and returns the path to\n// the temp file. The caller is responsible for removing the temp file on error.\nfunc SaveToTemp(r io.Reader, dir string) (tmpPath string, hash string, size int64, err error) {\n\tif err := os.MkdirAll(dir, 0755); err != nil {\n\t\treturn \"\", \"\", 0, fmt.Errorf(\"failed to create temp directory: %w\", err)\n\t}\n\n\ttmp, err := os.CreateTemp(dir, \".resource-upload-*\")\n\tif err != nil {\n\t\treturn \"\", \"\", 0, fmt.Errorf(\"failed to create temp file: %w\", err)\n\t}\n\ttmpPath = tmp.Name()\n\tdefer tmp.Close()\n\n\th := md5.New()\n\tmw := io.MultiWriter(tmp, h)\n\twritten, copyErr := io.Copy(mw, r)\n\tif copyErr != nil {\n\t\tos.Remove(tmpPath)\n\t\treturn \"\", \"\", 0, fmt.Errorf(\"failed to write temp file: %w\", copyErr)\n\t}\n\tif err := tmp.Chmod(0644); err != nil {\n\t\tos.Remove(tmpPath)\n\t\treturn \"\", \"\", 0, fmt.Errorf(\"failed to set temp file permissions: %w\", err)\n\t}\n\n\treturn tmpPath, hex.EncodeToString(h.Sum(nil)), written, nil\n}","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/resources/resources.go#L262-L298","documentation":"SaveToTemp creates the temporary file with os.CreateTemp(dir, \".resource-upload-*\"); this error wraps CreateTemp's failure. Even when the directory exists, file creation can fail due to permissions, exhausted inodes, or filesystem errors.","triggerScenarios":"dir exists but is not writable by the process user; the filesystem ran out of inodes; security policies (e.g. read-only tmpfs, AppArmor/SELinux) block creation; too many open file descriptors. Raised via UploadResources or direct calls.","commonSituations":"ulimit -n exhaustion under heavy upload load; Kubernetes securityContext with readOnlyRootFilesystem; SELinux denials on container volumes; tmp-cleaner scripts removing the directory between MkdirAll and CreateTemp.","solutions":["Check write permission on dir for the running user and the mount flags (mount | grep <dir>)","Check open file descriptor limits (ulimit -n) and raise them if uploads fail under load","Verify the volume is mounted rw and no security module is denying creation (dmesg/audit logs)","Ensure dir still exists at call time — don't delete it concurrently"],"exampleFix":"// before\nulimit -n 1024  # too low for concurrent uploads\n// after\nulimit -n 65536  # or set LimitNOFILE=65536 in the systemd unit","handlingStrategy":"retry","validationCode":"func canCreateTemp(dir string) error {\n\tf, err := os.CreateTemp(dir, \".probe-*\")\n\tif err != nil {\n\t\treturn err\n\t}\n\tname := f.Name()\n\tf.Close()\n\treturn os.Remove(name)\n}","typeGuard":null,"tryCatchPattern":"var tmpPath, hash string\nvar size int64\nerr := retry.Do(func() error {\n\ttp, h, s, e := resources.SaveToTemp(r, dir)\n\tif e != nil {\n\t\tvar perr *fs.PathError\n\t\tif errors.As(e, &perr) && errors.Is(perr.Err, syscall.ENOSPC) {\n\t\t\treturn retry.Unrecoverable(e) // don't retry disk-full\n\t\t}\n\t\treturn e\n\t}\n\ttmpPath, hash, size = tp, h, s\n\treturn nil\n}, retry.Attempts(3))","preventionTips":["Raise RLIMIT_NOFILE for services handling many concurrent uploads","Monitor inode usage, not just disk bytes (df -i)","Avoid read-only root filesystems without a writable temp volume","Don't run concurrent cleanup jobs against the temp directory during uploads"],"tags":["filesystem","permissions","temp-file","io"],"backgroundTag":"temp-file-creation-failed","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}