Browse Source

Closes GH-535 Immediate pause/resume race condition

Calling resume() immediately after calling pause() would trigger
a race condition where it would try to read() from a file
descriptor that was already being read from, causing an EBADF
v0.7.4-release
isaacs 14 years ago
committed by Ryan Dahl
parent
commit
e1a72f0e2e
  1. 5
      lib/fs.js
  2. 7
      test/simple/test-fs-read-stream.js

5
lib/fs.js

@ -838,7 +838,9 @@ ReadStream.prototype.setEncoding = function(encoding) {
ReadStream.prototype._read = function() { ReadStream.prototype._read = function() {
var self = this; var self = this;
if (!self.readable || self.paused) return; if (!self.readable || self.paused || self.reading) return;
self.reading = true;
if (!pool || pool.length - pool.used < kMinPoolSpace) { if (!pool || pool.length - pool.used < kMinPoolSpace) {
// discard the old pool. Can't add to the free list because // discard the old pool. Can't add to the free list because
@ -864,6 +866,7 @@ ReadStream.prototype._read = function() {
} }
function afterRead(err, bytesRead) { function afterRead(err, bytesRead) {
self.reading = false;
if (err) { if (err) {
self.emit('error', err); self.emit('error', err);
self.readable = false; self.readable = false;

7
test/simple/test-fs-read-stream.js

@ -42,8 +42,13 @@ file.addListener('open', function(fd) {
callbacks.open++; callbacks.open++;
assert.equal('number', typeof fd); assert.equal('number', typeof fd);
assert.ok(file.readable); assert.ok(file.readable);
});
// GH-535
file.pause();
file.resume();
file.pause();
file.resume();
});
file.addListener('data', function(data) { file.addListener('data', function(data) {
assert.ok(data instanceof Buffer); assert.ok(data instanceof Buffer);

Loading…
Cancel
Save