← Back to Pull requests

fs: make mkstemp disposable #67419

Open
unknown opened this pull request with 236 lines diff across 2 files
View on GitHub
unknown unknown commented on Nov 11, 2026 21:10 UTC
Author

Follow-up to #66484 as requested in #67360.

This adds Explicit Resource Management support to mkstemp:

// Synchronous using statement
{
  using file = fs.mkstempSync(prefix);
  fs.writeSync(file.fd, 'data');
  // file.fd is closed and the file is removed when leaving scope
}

// Asynchronous await using statement
{
  await using file = await fs.promises.mkstemp(prefix);
  await file.handle.writeFile('data');
  // file.handle is closed and the file is removed when leaving scope
}

Changes

1. lib/fs.js:
- mkstempSync() returns { path, fd, remove, [Symbol.dispose] }.
- mkstemp() passes { path, fd, remove, [Symbol.dispose] } to the callback.
- Captured full path at call time to remain resistant across process.chdir().
- Repeated calls to remove() are idempotent; closing the descriptor before disposal catches EBADF.

2. lib/internal/fs/promises.js:
- mkstemp() returns { __proto__: null, path, handle, remove, [Symbol.asyncDispose] }.
- remove() awaits handle.close() followed by unlink(fullPath) (ignoring ENOENT).

3. lib/internal/vfs/file_system.js & lib/internal/vfs/setup.js:
- Implemented disposal methods on VirtualFileSystem.prototype.mkstempSync and promises.mkstemp.
- Preserved disposable return values in VFS handler wrappers.

4. Documentation:
- Updated doc/api/fs.md for fsPromises.mkstemp, fs.mkstemp, and fs.mkstempSync with disposal methods and using / await using examples.

5. Tests:
- Added test/parallel/test-fs-mkstemp-disposable.js covering sync, callback, and promise variants for disposal, idempotence, prior manual close, buffer prefixes, and process.chdir().
- Updated test/parallel/test-vfs-mkstemp.js for VFS disposal coverage.

Fixes: https://github.com/nodejs/node/issues/67360
Refs: https://github.com/nodejs/node/pull/66484

Diff: 236 lines (+177 / -59) across 2 files Reviewer: bakkot Review duration: 29065m
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.37) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 236 lines diff across 2 files
0.37
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
46%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Shallow Review Depth shallow_review_depth
0.344
Diff Size diff_size
0.327
Real PR Conversation Timeline
1 comments
"Review active on #67419"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 46.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 37.2% (MEDIUM) · Higher = larger blast radius
37.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 46.7% · Lower = rushed or shallow review
46.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 19.8% · Safe to merge
19.8%
Repository