Skip to content

fs: fix duplicate bytes when Utf8Stream splits a character - #67725

Open
SAMARTHJAISWAL wants to merge 2 commits into
nodejs:mainfrom
SAMARTHJAISWAL:fix-utf8stream-split-char
Open

SAMARTHJAISWAL wants to merge 2 commits into
nodejs:mainfrom
SAMARTHJAISWAL:fix-utf8stream-split-char

Conversation

@SAMARTHJAISWAL

Copy link
Copy Markdown

When a short write ended inside a multi-byte character, Utf8Stream backed up
to the start of that character and wrote all of it again, although its first
bytes were already in the destination. So those bytes ended up there twice.

This keeps the unwritten bytes of the split character and writes them before
anything else, in the async, sync and flushSync() paths.

The existing partial-write test asserted that the whole character is sent
again, which is the behaviour reported in the issue, so I rewrote it to make
real short writes to a file and compare the file content with the input.

Not changed: flushSync() called while an async write is in flight can still
reorder output. The code already documents that as not guaranteed.

AI disclosure: I used an AI coding assistant to help write the fix and the
tests. I read through the diff, ran the repro from the issue before and after
the change, and ran the full CI on my fork.

Fixes: #67679

When a partial write ended inside a multi-byte UTF-8 character,
Utf8Stream backed up to the start of that character and wrote all of
it again. The leading bytes of the character were already in the
destination, so they ended up there twice.

Keep the unwritten bytes of the split character instead and write them
before anything else, in the asynchronous, synchronous and flushSync()
paths.

Fixes: nodejs#67679
Signed-off-by: Samarth Jaiswal <samarthjaiswal30@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. stream Issues and PRs related to Node.js streams. labels Nov 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Caution

AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context.

@evilive3000

Copy link
Copy Markdown

Could we add two regression cases to the existing test? Both cases can silently lose data. The first drops a queued write, and the second reports a successful flush even though the file ends with an incomplete UTF-8 character.

  • After a short write in the middle of 中, have the write listener enqueue NEXT and call flushSync(). The file should contain 中NEXT, but on 31cafa3 it contains only 中. Code
  • Return EIO once while writing the remaining bytes of 中, then call flush(cb) and end(). The flush callback reports success and the stream finishes, but the file still contains only the first byte (e4). Code

flushSync() dropped the next queued string after it wrote the rest of
a split character, and lost the rest of a string when a write threw.
flush() and end() did not count the rest of a split character as
pending, so they could report success with the character incomplete.

Signed-off-by: Samarth Jaiswal <samarthjaiswal30@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agentscan:automated-account 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.

Utf8Stream writes duplicate bytes when a short write splits a multi-byte character

3 participants