Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: the position of _read() is wrong #38292

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/internal/fs/streams.js
Expand Up @@ -255,6 +255,10 @@ ReadStream.prototype._read = function(n) {
if (er) {
errorOrDestroy(this, er);
} else if (bytesRead > 0) {
if (this.pos !== undefined) {
this.pos += bytesRead;
}

this.bytesRead += bytesRead;

if (bytesRead !== buf.length) {
Expand All @@ -271,10 +275,6 @@ ReadStream.prototype._read = function(n) {
this.push(null);
}
});

if (this.pos !== undefined) {
this.pos += n;
}
};

ReadStream.prototype._destroy = function(err, cb) {
Expand Down
69 changes: 69 additions & 0 deletions test/parallel/test-fs-read-stream-pos.js
@@ -0,0 +1,69 @@
'use strict';
helloyou2012 marked this conversation as resolved.
Show resolved Hide resolved

// Refs: https://github.com/nodejs/node/issues/33940

const common = require('../common');
const tmpdir = require('../common/tmpdir');
const fs = require('fs');
const assert = require('assert');
const path = require('path');

tmpdir.refresh();

const file = path.join(tmpdir.path, '/read_stream_pos_test.txt');

fs.writeFileSync(file, '');

let counter = 0;

setInterval(() => {
counter = counter + 1;
const line = `hello at ${counter}\n`;
fs.writeFileSync(file, line, { flag: 'a' });
}, 1);

const hwm = 10;
let bufs = [];
let isLow = false;
let cur = 0;
let stream;

setInterval(() => {
if (stream) return;

stream = fs.createReadStream(file, {
highWaterMark: hwm,
start: cur
});
stream.on('data', common.mustCallAtLeast((chunk) => {
Copy link
Member

Choose a reason for hiding this comment

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

Any reason this is a mustCAllAtLeast and not a mustCall?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mustCall is exact times, but this place will call at least 1 time not exact 1 time.

cur += chunk.length;
bufs.push(chunk);
if (isLow) {
const brokenLines = Buffer.concat(bufs).toString()
.split('\n')
.filter((line) => {
const s = 'hello at'.slice(0, line.length);
if (line && !line.startsWith(s)) {
return true;
}
return false;
});
assert.strictEqual(brokenLines.length, 0);
process.exit();
return;
}
if (chunk.length !== hwm) {
isLow = true;
}
}));
stream.on('end', () => {
stream = null;
isLow = false;
bufs = [];
});
}, 10);

// Time longer than 90 seconds to exit safely
setTimeout(() => {
process.exit();
}, 90000);