{"record":{"id":"93bf476195d3abdd","repo":"valyala/fasthttp","slug":"cannot-create-temporary-file-for-q-w","errorCode":null,"errorMessage":"cannot create temporary file for %q: %w","messagePattern":"cannot create temporary file for %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"fs.go","lineNumber":1747,"sourceCode":"\t// It is safe opening such a file, since the file creation\n\t// is guarded by file mutex - see getFileLock call.\n\tif _, err := os.Stat(compressedFilePath); err == nil {\n\t\t_ = f.Close()\n\t\treturn h.newCompressedFSFile(compressedFilePath, fileEncoding)\n\t}\n\n\t// Create temporary file, so concurrent goroutines don't use\n\t// it until it is created.\n\t//\n\t// os.CreateTemp gives the file a random name and opens it with O_EXCL and\n\t// 0600, so a symlink pre-planted at the otherwise predictable temp path\n\t// can't be followed to truncate an arbitrary file, and the cache file\n\t// isn't left group/world-readable.\n\tzf, err := os.CreateTemp(filepath.Dir(compressedFilePath), filepath.Base(compressedFilePath)+\".tmp-*\")\n\tif err != nil {\n\t\t_ = f.Close()\n\t\tif !errors.Is(err, fs.ErrPermission) {\n\t\t\treturn nil, fmt.Errorf(\"cannot create temporary file for %q: %w\", compressedFilePath, err)\n\t\t}\n\t\treturn nil, errNoCreatePermission\n\t}\n\ttmpFilePath := zf.Name()\n\tswitch fileEncoding {\n\tcase \"br\":\n\t\tzw := acquireStacklessBrotliWriter(zf, CompressDefaultCompression)\n\t\t_, err = copyZeroAlloc(zw, f)\n\t\tif errf := zw.Flush(); err == nil {\n\t\t\terr = errf\n\t\t}\n\t\treleaseStacklessBrotliWriter(zw, CompressDefaultCompression)\n\tcase \"gzip\":\n\t\tzw := acquireStacklessGzipWriter(zf, CompressDefaultCompression)\n\t\t_, err = copyZeroAlloc(zw, f)\n\t\tif errf := zw.Flush(); err == nil {\n\t\t\terr = errf\n\t\t}","sourceCodeStart":1729,"sourceCodeEnd":1765,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/fs.go#L1729-L1765","documentation":"fasthttp compresses files into cache files written via os.CreateTemp in the directory of the target compressed file. If CreateTemp fails and the error is not fs.ErrPermission, this error wraps the cause. ErrPermission is deliberately mapped to the sentinel errNoCreatePermission instead, so this error signals other temp-creation failures (missing directory, ENOSPC, name issues).","triggerScenarios":"Enabling FS.Compress / CompressBrotli / ServeFile with compression where the cache directory does not exist, is not writable, or the filesystem is full; the compressed file's directory differs from the source dir and lacks write permission for reasons other than plain ErrPermission (e.g. read-only mount).","commonSituations":"Running the server as non-root but pointing FS at a root-owned directory; read-only container filesystems; disk full on the partition holding static files; tmpcleaners deleting the cache directory mid-run.","solutions":["Make the directory containing the target compressed file writable by the server process (chown/chmod, or write to a user-owned dir).","Pre-create the cache directory (FS.CachePath / Compress dirs) before starting the server.","Free disk space if the cause is ENOSPC.","If you see 'permission denied' style errors surfaced as errNoCreatePermission instead, disable compression or point compression output to a writable location.","Run the server in a writable filesystem (not read-only mount)."],"exampleFix":"// before\nfs := &fasthttp.FS{Root: \"/var/www\", Compress: true} // dir not writable\n// after\n_ = os.MkdirAll(\"/var/www\", 0o755)\n// and ensure the service user can write:\n// sudo chown appuser /var/www\nfs := &fasthttp.FS{Root: \"/var/www\", Compress: true}","handlingStrategy":"validation","validationCode":"cacheDir := filepath.Dir(targetCompressedPath)\nif fi, err := os.Stat(cacheDir); err != nil || !fi.IsDir() {\n    os.MkdirAll(cacheDir, 0o755)\n}\nif err := unix.Access(cacheDir, unix.W_OK); err != nil {\n    // chown/chmod before enabling compression\n}","typeGuard":null,"tryCatchPattern":"if strings.Contains(err.Error(), \"cannot create temporary file\") {\n    ctx.Error(\"cache storage failure\", fasthttp.StatusInternalServerError)\n}","preventionTips":["Pre-create and pre-chown the static/cache directories in your deploy/entrypoint.","Monitor disk space (ENOSPC) on the partition holding static files.","Do not run the server against read-only mounts with compression enabled.","Map errNoCreatePermission sentinels to disable-compression fallbacks."],"tags":["filesystem","fasthttp","compression","temp-file","permissions"],"backgroundTag":"temp-file-creation-failed","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}