{"record":{"id":"92a429ebd036e114","repo":"RocketChat/Rocket.Chat","slug":"uploadservice-failed-to-cleanup-temp-file-tem","errorCode":null,"errorMessage":"[UploadService] Failed to cleanup temp file: ${tempFilePath}","messagePattern":"\\[UploadService\\] Failed to cleanup temp file: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"apps/meteor/server/api/lib/MultipartUploadHandler.ts","lineNumber":42,"sourceCode":"\tfield: string;\n\tmaxSize?: number;\n\tallowedMimeTypes?: string[];\n\ttransforms?: Transform[]; // Optional transform pipeline (e.g., EXIF stripping)\n\tfileOptional?: boolean;\n};\n\nexport class MultipartUploadHandler {\n\tstatic transforms = {\n\t\tstripExif(): Transform {\n\t\t\treturn new ExifTransformer();\n\t\t},\n\t};\n\n\tstatic async cleanup(tempFilePath: string): Promise<void> {\n\t\ttry {\n\t\t\tawait fs.promises.unlink(tempFilePath);\n\t\t} catch (error: any) {\n\t\t\tconsole.warn(`[UploadService] Failed to cleanup temp file: ${tempFilePath}`, error);\n\t\t}\n\t}\n\n\tstatic async stripExifFromFile(tempFilePath: string): Promise<number> {\n\t\tconst strippedPath = `${tempFilePath}.stripped`;\n\n\t\ttry {\n\t\t\tconst writeStream = fs.createWriteStream(strippedPath);\n\n\t\t\tawait pipeline(fs.createReadStream(tempFilePath), new ExifTransformer(), writeStream);\n\n\t\t\tawait fs.promises.rename(strippedPath, tempFilePath);\n\n\t\t\treturn writeStream.bytesWritten;\n\t\t} catch (error) {\n\t\t\tvoid this.cleanup(strippedPath);\n\n\t\t\tthrow error;","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/api/lib/MultipartUploadHandler.ts#L24-L60","documentation":"MultipartUploadHandler.cleanup best-effort deletes a temporary upload file with fs.promises.unlink and swallows failures with this console warn. The rejection usually means the file was already gone (ENOENT), the process lacks permission on the temp directory (EACCES), or the path lives on a filesystem/container the process cannot write to. Upload processing continues; repeated failures leave temp files accumulating in the OS temp dir.","triggerScenarios":"Double cleanup of the same temp path (retry logic after a failed upload); TMPDIR pointing to a directory with wrong ownership or a read-only mount; containers where the temp path was created in a different, since-replaced, container layer; external tmp reapers (systemd-tmpfiles) removing long-lived upload temp files.","commonSituations":"Kubernetes/Docker deployments with per-container /tmp and horizontal scaling; small tmpfs at TMPDIR aggressively cleaning files; file upload with EXIF stripping on slow disks where streams and cleanup race.","solutions":["Identify the errno in the logged error object: ENOENT is harmless, EACCES/EROFS are configuration problems","For EACCES/EROFS, point TMPDIR to a writable directory owned by the Rocket.Chat process and mount it consistently across replicas","ENOENT: look for duplicate cleanup calls for the same upload and deduplicate them","If it recurs, monitor the temp directory growth to catch silent leftovers"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import { access, constants, unlink } from 'node:fs/promises';\n\nasync function cleanup(tempFilePath: string): Promise<void> {\n\ttry {\n\t\tawait access(tempFilePath, constants.W_OK);\n\t\tawait unlink(tempFilePath);\n\t} catch (error: any) {\n\t\tif (error.code !== 'ENOENT') console.warn('[UploadService] Failed to cleanup temp file:', tempFilePath, error.code);\n\t}\n}","typeGuard":null,"tryCatchPattern":"try {\n\tawait fs.promises.unlink(tempFilePath);\n} catch (error: any) {\n\tif (error?.code !== 'ENOENT') console.warn(`[UploadService] Failed to cleanup temp file: ${tempFilePath}`, error);\n}","preventionTips":["Mount a writable, shared TMPDIR with consistent ownership on all replicas","Never call cleanup twice for the same temp path — track the file lifecycle per upload","Alert on temp-dir disk usage growth to catch silently failing cleanups early"],"tags":["filesystem","uploads","temp-files","multipart","cleanup"],"backgroundTag":"temp-file-cleanup-failed","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","contentChangedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}