{"record":{"id":"a09eab2f60fd7010","repo":"valyala/fasthttp","slug":"error-when-compressing-file-q-to-q-w","errorCode":null,"errorMessage":"error when compressing file %q to %q: %w","messagePattern":"error when compressing file %q to %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"fs.go","lineNumber":1779,"sourceCode":"\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}\n\t\treleaseStacklessGzipWriter(zw, CompressDefaultCompression)\n\tcase \"zstd\":\n\t\tzw := acquireStacklessZstdWriter(zf, CompressZstdDefault)\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\treleaseStacklessZstdWriter(zw, CompressZstdDefault)\n\t}\n\t_ = zf.Close()\n\t_ = f.Close()\n\tif err != nil {\n\t\t_ = os.Remove(tmpFilePath)\n\t\treturn nil, fmt.Errorf(\"error when compressing file %q to %q: %w\", filePath, tmpFilePath, err)\n\t}\n\tif err = os.Chtimes(tmpFilePath, time.Now(), fileInfo.ModTime()); err != nil {\n\t\t_ = os.Remove(tmpFilePath)\n\t\treturn nil, fmt.Errorf(\"cannot change modification time to %v for tmp file %q: %v\",\n\t\t\tfileInfo.ModTime(), tmpFilePath, err)\n\t}\n\tif err = os.Rename(tmpFilePath, compressedFilePath); err != nil {\n\t\t_ = os.Remove(tmpFilePath)\n\t\treturn nil, fmt.Errorf(\"cannot move compressed file from %q to %q: %w\", tmpFilePath, compressedFilePath, err)\n\t}\n\treturn h.newCompressedFSFile(compressedFilePath, fileEncoding)\n}\n\n// newCompressedFSFileCache use memory cache compressed files.\nfunc (h *fsHandler) newCompressedFSFileCache(f fs.File, fileInfo fs.FileInfo, filePath, fileEncoding string) (*fsFile, error) {\n\tvar (\n\t\tw   = &bytebufferpool.ByteBuffer{}\n\t\terr error","sourceCodeStart":1761,"sourceCodeEnd":1797,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/fs.go#L1761-L1797","documentation":"This error occurs inside fasthttp's file compression routine when the actual compression step (gzip/brotli/zstd writer writing the temp file) returns an error. The library closes the temp and source files, removes the partial temp file, and wraps the compression error with the source path and temp path. It indicates the compressed output could not be produced, not a filesystem permission problem.","triggerScenarios":"Compressing a very large file with a zstd/gzip/brotli writer that hits an I/O error writing to the temp file (disk full mid-write), or a writer-level failure while generating the compressed cache file during the first request for that file+encoding.","commonSituations":"Disk filling up while the first request triggers compression; serving from flaky network storage where writes to the temp file fail; memory pressure killing/resetting compressor state in embedded environments.","solutions":["Check the wrapped cause: ENOSPC means free disk space in the directory holding compressed cache files.","Pre-generate compressed assets at deploy time (e.g. gzip -k) so the server only serves existing cache files.","Retry the request: the failed temp file is removed, and a subsequent request retries compression.","Ensure the cache filesystem is healthy and writable (avoid unreliable network mounts for cache)."],"exampleFix":"// before: letting server compress at first request on a full disk\nfs := &fasthttp.FS{Root: \"/srv/assets\", Compress: true}\n// after: precompress at build/deploy\n// find /srv/assets -type f -exec gzip -9k {} \\;\nfs := &fasthttp.FS{Root: \"/srv/assets\", Compress: true, CompressedFileSuffix: \".gz\"}","handlingStrategy":"retry","validationCode":"// pre-generate compressed assets so runtime compression is skipped:\n// exec.Command(\"gzip\", \"-9k\", path).Run()","typeGuard":null,"tryCatchPattern":"if strings.Contains(err.Error(), \"error when compressing file\") {\n    // temp file already removed; retry once, then serve uncompressed\n    ctx.SetStatusCode(fasthttp.StatusOK) // fall back path\n}","preventionTips":["Pre-compress assets at deploy time (gzip -k / brotli) so the server only serves them.","Keep ample free disk space where cache files are written.","Avoid unreliable network filesystems for compression output."],"tags":["filesystem","fasthttp","compression","io-error"],"backgroundTag":"compression-write-failed","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}