Skip to content

stream: fix decoded fromList chunk boundary check#61884

Open
watson wants to merge 2 commits intonodejs:mainfrom
watson:fix-readable-stream-bug
Open

stream: fix decoded fromList chunk boundary check#61884
watson wants to merge 2 commits intonodejs:mainfrom
watson:fix-readable-stream-bug

Conversation

@watson
Copy link
Member

@watson watson commented Feb 19, 2026

Correct fromList() in decoded string mode to compare n against the current chunk length, not the buffer array length.

This prevents over-consuming chunks, which can corrupt readable state and crash with TypeError when mixing setEncoding() and read(n).

I reproduced this in all non-EoL release lines.

For reference, here's the script I used to make read() throw (usually it throws almost instantly, but a few times it can take a little while):

'use strict'

const https = require('node:https')

const url = process.argv[2] || 'https://www.google.com'
const maxRuns = Number(process.argv[3] || 2000)
let runs = 0

function runOnce () {
  runs++
  https.get(url, (res) => {
    res.setEncoding('utf8') // If this line is removed, the crash doesn't happen.

    res.on('readable', () => {
      // Race condition: The call to `res.read(100)` might throw
      while (res.read(100) !== null) {}
    })

    res.on('end', () => {
      if (runs >= maxRuns) {
        console.log(`completed ${runs} runs without crash`)
      } else {
        runOnce()
      }
    })
  })
}

console.log(`reproducing with ${url} for up to ${maxRuns} runs...`)
runOnce()

And here's the output:

reproducing with https://www.google.com for up to 2000 runs...
node:internal/streams/readable:1650
  } else if (n < buf[idx].length) {
                          ^

TypeError: Cannot read properties of undefined (reading 'length')
    at fromList (node:internal/streams/readable:1650:27)
    at Readable.read (node:internal/streams/readable:756:11)
    at IncomingMessage.<anonymous> (/Users/thomas.watson/go/src/github.com/DataDog/dd-trace-js/foo.js:16:18)
    at IncomingMessage.emit (node:events:508:20)
    at emitReadable_ (node:internal/streams/readable:837:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:89:21)

Node.js v25.6.1

@nodejs-github-bot
Copy link
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 the stream subsystem. labels Feb 19, 2026
Correct `fromList()` in decoded string mode to compare `n` against the
current chunk length, not the buffer array length.

This prevents over-consuming chunks, which can corrupt readable state
and crash with `TypeError` when mixing `setEncoding()` and `read(n)`.
@watson watson force-pushed the fix-readable-stream-bug branch from 8a962b7 to 5ef134f Compare February 19, 2026 10:30
@watson watson changed the title fix(stream): fix decoded fromList chunk boundary check stream: fix decoded fromList chunk boundary check Feb 19, 2026
@BridgeAR BridgeAR added request-ci Add this label to start a Jenkins CI on a PR. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. labels Feb 19, 2026
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 19, 2026
@nodejs-github-bot
Copy link
Collaborator

@codecov
Copy link

codecov bot commented Feb 19, 2026

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 89.73%. Comparing base (d5279e5) to head (2984f13).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/streams/readable.js 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #61884      +/-   ##
==========================================
- Coverage   91.81%   89.73%   -2.08%     
==========================================
  Files         338      675     +337     
  Lines      140073   204855   +64782     
  Branches    22081    39372   +17291     
==========================================
+ Hits       128605   183829   +55224     
- Misses      11242    13287    +2045     
- Partials      226     7739    +7513     
Files with missing lines Coverage Δ
lib/internal/streams/readable.js 97.16% <0.00%> (+0.88%) ⬆️

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

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina added request-ci Add this label to start a Jenkins CI on a PR. commit-queue Add this label to land a pull request using GitHub Actions. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue Add this label to land a pull request using GitHub Actions. needs-ci PRs that need a full CI run. stream Issues and PRs related to the stream subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

Comments