Skip to content

[Bug report] fs.createReadStream set highWaterMark, the same file get different result #35465

@Zclhlmgqzc

Description

@Zclhlmgqzc

What steps will reproduce the bug?

test-202.js

const { createReadStream } = require('fs')

// const highWaterMark = 127
// ./test-file-200
// 23
// ./test-file-200
// 23

// const highWaterMark = 128
// ./test-file-200
// 20
// ./test-file-200
// 20

// const highWaterMark = 129
// ./test-file-202
// 17
// ./test-file-202
// 128
// 18

// const highWaterMark = 160
// ./test-file-202
// 84
// ./test-file-202
// 84

// const highWaterMark = 199
// ./test-file-202
// 6
// ./test-file-202
// 184
// 21

const highWaterMark = 202

async function readFile(inputPath) {
  console.log(inputPath)

  const readStream = createReadStream(inputPath, {
    encoding: 'binary',
    highWaterMark
  })

  for await (const chunk of readStream) {
    if (chunk.length !== highWaterMark) {
      console.log(chunk.length)
    }
  }
}

async function test() {
  for (const file of ['./test-file-202', './test-file-202']) {
    await readFile(file)
  }
}

test()

test-file-202
(10 * 20 + /r/n)

 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789

How often does it reproduce? Is there a required condition?

Always with node v12.2.0-v12.18.4-14.13.0 and highWaterMark > 128
Works fine in node v10.22.1 v12.1.0 or highWaterMark <= 128

What is the expected behavior?

the same result

What do you see instead?

node test-202
./test-file-202
./test-file-202
200
2

Additional information

v12.2.0
fs: align fs.ReadStream buffer pool writes to 8-byte boundary
#24838

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to the fs subsystem / file system.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions