Skip to content

src: retain buffer memory for pending writes - #67907

Open
araujogui wants to merge 3 commits into
nodejs:mainfrom
araujogui:src-retain-pending-write-buffers
Open

araujogui wants to merge 3 commits into
nodejs:mainfrom
araujogui:src-retain-pending-write-buffers

Conversation

@araujogui

Copy link
Copy Markdown
Member

Stream writes pass raw pointers into Buffer memory to the stream. Nothing kept that memory alive while the write was pending. Detaching the ArrayBuffer freed it, and the peer received whatever reused the memory.

This keeps the backing store of each Buffer chunk in the WriteWrap until the write completes.

const net = require('node:net');

const size = 16 * 1024 * 1024;
const server = net.createServer((socket) => {
  const buf = Buffer.alloc(size, 'a');
  socket.end(buf);
  buf.buffer.transfer(1); // Frees the memory of the pending write.
  Buffer.alloc(size, 'b');
}).listen(0, () => {
  net.connect(server.address().port)
    .on('data', (chunk) => {
      if (chunk.includes('b')) throw new Error('received reused memory');
    })
    .on('end', () => server.close());
});

On v26.13.1 this throws. With this change the client receives only a.

`StreamBase::WriteBuffer()` and `StreamBase::Writev()` pass raw
pointers into Buffer memory to the stream. Nothing kept that memory
alive while the write was pending, so detaching the ArrayBuffer (e.g.
with `transfer()` or `postMessage()`) freed it, and the stream sent
whatever reused the memory to the peer.

Keep the backing store of each Buffer chunk in the `WriteWrap` until
the write completes.

Assisted-by: Claude Code
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 30, 2026 17:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Nov 30, 2026
@araujogui araujogui added net Issues and PRs related to the net subsystem. stream Issues and PRs related to Node.js streams. buffer Issues and PRs related to the buffer subsystem. labels Nov 30, 2026
@araujogui

Copy link
Copy Markdown
Member Author

CC @nodejs/streams @nodejs/net

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 62.16216% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.57%. Comparing base (d3f8545) to head (6e2d56a).
⚠️ Report is 66 commits behind head on main.

Files with missing lines Patch % Lines
src/stream_base-inl.h 31.25% 10 Missing and 1 partial ⚠️
src/stream_base.h 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67907      +/-   ##
==========================================
+ Coverage   90.53%   90.57%   +0.03%     
==========================================
  Files         805      802       -3     
  Lines      285842   286690     +848     
  Branches    55187    55376     +189     
==========================================
+ Hits       258793   259668     +875     
+ Misses      17167    17130      -37     
- Partials     9882     9892      +10     
Files with missing lines Coverage Δ
src/stream_base.cc 79.17% <100.00%> (+0.52%) ⬆️
src/stream_base.h 56.66% <0.00%> (-4.05%) ⬇️
src/stream_base-inl.h 81.98% <31.25%> (-8.55%) ⬇️

... and 88 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.

@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

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 1, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 1, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@araujogui araujogui added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 1, 2026
@araujogui

Copy link
Copy Markdown
Member Author

@jasnell @mcollina CI tests failed on Windows, so I changed the test behavior a bit. I wonder if there's a better way to test this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. net Issues and PRs related to the net subsystem. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. stream Issues and PRs related to Node.js streams.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants