Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/893415 to your computer and use it in GitHub Desktop.
Save isaacs/893415 to your computer and use it in GitHub Desktop.
From fad91373a2673684ec4f50de0794d0060dac0323 Mon Sep 17 00:00:00 2001
From: isaacs <i@izs.me>
Date: Tue, 29 Mar 2011 14:54:49 -0700
Subject: [PATCH] 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
---
lib/fs.js | 5 ++++-
test/simple/test-fs-read-stream.js | 7 ++++++-
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/lib/fs.js b/lib/fs.js
index 99002ba..062b4a3 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -867,7 +867,9 @@ ReadStream.prototype.setEncoding = function(encoding) {
ReadStream.prototype._read = function() {
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) {
// discard the old pool. Can't add to the free list because
@@ -893,6 +895,7 @@ ReadStream.prototype._read = function() {
}
function afterRead(err, bytesRead) {
+ self.reading = false;
if (err) {
self.emit('error', err);
self.readable = false;
diff --git a/test/simple/test-fs-read-stream.js b/test/simple/test-fs-read-stream.js
index 8b821c0..188c95d 100644
--- a/test/simple/test-fs-read-stream.js
+++ b/test/simple/test-fs-read-stream.js
@@ -42,8 +42,13 @@ file.addListener('open', function(fd) {
callbacks.open++;
assert.equal('number', typeof fd);
assert.ok(file.readable);
-});
+ // GH-535
+ file.pause();
+ file.resume();
+ file.pause();
+ file.resume();
+});
file.addListener('data', function(data) {
assert.ok(data instanceof Buffer);
--
1.7.2.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment