{"record":{"id":"64320f6e97c3f4be","repo":"denoland/deno","slug":"too-many-temp-files-exist","errorCode":null,"errorMessage":"too many temp files exist","messagePattern":"too many temp files exist","errorType":"exception","errorClass":"FsError","httpStatus":null,"severity":"error","filePath":"ext/fs/ops.rs","lineNumber":1263,"sourceCode":"  for _ in 0..MAX_TRIES {\n    let path = tmp_name(&mut rng, &dir, prefix.as_deref(), suffix.as_deref())?;\n    // PERMISSIONS: this is fine because the dir was checked\n    let path = CheckedPath::unsafe_new(Cow::Owned(path));\n    match fs.open_sync(&path, open_opts) {\n      Ok(_) => {\n        // PERMISSIONS: ensure the absolute path is not leaked\n        let path =\n          strip_dir_prefix(&dir, dir_arg.as_deref(), path.into_owned_path())?;\n        return path_into_string(path.into_os_string());\n      }\n      Err(FsError::Io(ref e)) if e.kind() == io::ErrorKind::AlreadyExists => {\n        continue;\n      }\n      Err(e) => return Err(e).context(\"tmpfile\"),\n    }\n  }\n\n  Err(FsError::Io(io::Error::new(\n    io::ErrorKind::AlreadyExists,\n    \"too many temp files exist\",\n  )))\n  .context(\"tmpfile\")\n}\n\n#[op2(stack_trace)]\n#[string]\npub async fn op_fs_make_temp_file_async(\n  state: Rc<RefCell<OpState>>,\n  #[string] dir_arg: Option<String>,\n  #[string] prefix: Option<String>,\n  #[string] suffix: Option<String>,\n) -> Result<String, FsOpsError> {\n  let (dir, fs) =\n    make_temp_check_async(state, dir_arg.as_deref(), \"Deno.makeTempFile()\")?;\n\n  let open_opts = OpenOptions {","sourceCodeStart":1245,"sourceCodeEnd":1281,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/fs/ops.rs#L1245-L1281","documentation":"Deno.makeTempFileSync() generates up to 10 candidate names (dir + prefix + 8-hex random + suffix) and creates each with an exclusive create; AlreadyExists means the name is taken and the loop retries. If all 10 attempts collide, it fails with io::ErrorKind::AlreadyExists 'too many temp files exist', wrapped with context 'tmpfile'.","triggerScenarios":"Deno.makeTempFileSync({ dir, prefix, suffix }) where every generated name matches an existing file — a stubbed filesystem in tests that always answers AlreadyExists, or a directory pre-seeded with the exact prefix/suffix + hex pattern.","commonSituations":"Test doubles intercepting file creation; file-sync or antivirus agents racing creation; nearly impossible naturally because tmp_name draws from a 64-bit random space — real occurrences almost always involve mocking or name squatting.","solutions":["Remove stale files matching the prefix/suffix pattern from the directory","Choose a fresh prefix or suffix","Omit dir to create in the OS temp directory","Retry with a different prefix when AlreadyExists surfaces"],"exampleFix":"// before\nconst f = Deno.makeTempFileSync({ dir: './tmp', prefix: 'up-', suffix: '.part' });\n\n// after\nconst f = Deno.makeTempFileSync({ dir: './tmp', prefix: `up-${Date.now()}-` });","handlingStrategy":"retry","validationCode":"function makeTempFileSafe(opts: Deno.MakeTempOptions = {}): string {\n  for (let i = 0; i < 3; i++) {\n    try {\n      return Deno.makeTempFileSync({ ...opts, prefix: `${opts.prefix ?? ''}${Date.now()}-` });\n    } catch (e) {\n      if (!(e instanceof Error) || !/too many temp files exist/.test(e.message)) throw e;\n    }\n  }\n  throw new Error('temp file creation exhausted retries');\n}","typeGuard":null,"tryCatchPattern":"try {\n  path = Deno.makeTempFileSync({ dir, prefix, suffix });\n} catch (e) {\n  if (e instanceof Error && /too many temp files exist/.test(e.message)) {\n    path = Deno.makeTempFileSync({ dir, prefix: `${prefix}${crypto.randomUUID().slice(0, 8)}-`, suffix });\n  } else {\n    throw e;\n  }\n}","preventionTips":["Use unique prefixes so the 10-attempt random space never matters","Keep the target directory free of files matching your prefix/suffix pattern","Fix test doubles that blanket-return AlreadyExists"],"tags":["filesystem","temp-file","collision","already-exists","deno"],"backgroundTag":"temp-file-collision","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}