Skip to content

stream: close Utf8Stream fd after pending write - #67355

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
christianaurichzm:deflake-periodicflush
Nov 14, 2026
Merged

nodejs-github-bot merged 1 commit into
nodejs:mainfrom
christianaurichzm:deflake-periodicflush

Conversation

@christianaurichzm

@christianaurichzm christianaurichzm commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Utf8Stream#destroy() closes the fd while an fs.write() on it may still be running on the thread pool. The write then fails with EBADF, which crashes the process if nothing listens for 'error', or lands in whatever file reuses the fd number. test-fastutf8stream-periodicflush hit the EBADF case in CI.

const fs = require('node:fs');
const slowFs = { write: (...args) => setTimeout(() => fs.write(...args), 50) };

const stream = new fs.Utf8Stream({ fd: fs.openSync('a.log', 'w'), fs: slowFs });
stream.write('meant for a.log\n');
stream.destroy();
stream.on('close', () => fs.openSync('b.log', 'w')); // reuses the fd number

a.log stays empty and b.log gets the line.

destroy() now closes the fd once the in-flight write completes, as fs.WriteStream does. A write that hits EAGAIN around destroy(), either already waiting on the retry timer or coming back with EAGAIN afterwards, is still retried on the closed fd, as on main. So is the rest of a partial write when destroy() is called from a 'write' listener. I'd fix those separately.

With every fs.write() delayed by 50 ms, test-fastutf8stream-periodicflush fails 50/50 on main and passes with this change. An earlier version of this PR only changed the test.

Refs: https://github.com/nodejs/reliability/blob/main/reports/2026-11-09.md

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test Issues and PRs related to Node.js core tests and test infrastructure. labels Nov 9, 2026
@codecov

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.30769% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.70%. Comparing base (933cb11) to head (1d3c5a8).
⚠️ Report is 25 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/streams/fast-utf8-stream.js 92.30% 4 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #67355   +/-   ##
=======================================
  Coverage   90.69%   90.70%           
=======================================
  Files         801      801           
  Lines      281875   281932   +57     
  Branches    54317    54324    +7     
=======================================
+ Hits       255652   255715   +63     
+ Misses      16348    16346    -2     
+ Partials     9875     9871    -4     
Files with missing lines Coverage Δ
lib/internal/streams/fast-utf8-stream.js 88.78% <92.30%> (+0.15%) ⬆️

... and 33 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@christianaurichzm christianaurichzm added the flaky-test Issues and PRs involving tests that fail intermittently in CI. label Nov 10, 2026
destroy() fsynced and closed the fd right away, even while an
fs.write() on it was still running on the thread pool. The write then
failed with EBADF, emitted as an 'error' that crashed the process when
nothing listened for it, or landed in whatever file reused the fd
number.

Close the fd once the in-flight write completes, as fs.WriteStream
does. Data buffered behind it is still dropped, so 'finish' is not
emitted in that case.

Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
@christianaurichzm christianaurichzm changed the title test: deflake test-fastutf8stream-periodicflush stream: close Utf8Stream fd after pending write Nov 13, 2026
@christianaurichzm christianaurichzm added stream Issues and PRs related to Node.js streams. and removed test Issues and PRs related to Node.js core tests and test infrastructure. labels Nov 13, 2026
@nodejs-github-bot

This comment was marked as outdated.

@panva panva added the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Nov 13, 2026
@christianaurichzm

Copy link
Copy Markdown
Member Author

@mcollina could you take a look when you get a chance? sonic-boom's actualClose() also closes the fd while a write is in flight, so I think it has the same bug. Happy to send the fix there too. Thanks!

cc @nodejs/streams

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@nodejs-github-bot

This comment was marked as outdated.

@christianaurichzm christianaurichzm removed the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Nov 13, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@christianaurichzm christianaurichzm added the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Nov 14, 2026
@panva panva added the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Nov 14, 2026
@github-actions github-actions Bot removed the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Nov 14, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@christianaurichzm christianaurichzm added the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 14, 2026
@nodejs-github-bot
nodejs-github-bot merged commit 7996b18 into nodejs:main Nov 14, 2026
76 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 7996b18

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 14, 2026
aduh95 pushed a commit that referenced this pull request Nov 19, 2026
destroy() fsynced and closed the fd right away, even while an
fs.write() on it was still running on the thread pool. The write then
failed with EBADF, emitted as an 'error' that crashed the process when
nothing listened for it, or landed in whatever file reused the fd
number.

Close the fd once the in-flight write completes, as fs.WriteStream
does. Data buffered behind it is still dropped, so 'finish' is not
emitted in that case.

Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
PR-URL: #67355
Refs: https://github.com/nodejs/reliability/blob/main/reports/2026-11-09.md
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. flaky-test Issues and PRs involving tests that fail intermittently in CI. needs-ci PRs that need a full CI run. stream Issues and PRs related to Node.js streams.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants