Created
September 15, 2010 23:45
-
-
Save reid/581697 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| From a5964ad256219738997f7b97505e3e3fe4d50f60 Mon Sep 17 00:00:00 2001 | |
| From: Reid Burke <me@reidburke.com> | |
| Date: Wed, 15 Sep 2010 16:38:05 -0700 | |
| Subject: [PATCH] sys.pump: only write to a stream if it's writable. | |
| If the writeStream's writable property is false, | |
| do not attempt to write to it. Otherwise, plenty | |
| of uncaught exceptions occur before the stream closes. | |
| --- | |
| lib/sys.js | 4 ++- | |
| test/simple/test-pump-file2tcp-nowritable.js | 33 ++++++++++++++++++++++++++ | |
| 2 files changed, 36 insertions(+), 1 deletions(-) | |
| create mode 100644 test/simple/test-pump-file2tcp-nowritable.js | |
| diff --git a/lib/sys.js b/lib/sys.js | |
| index 1d34713..6cfb860 100644 | |
| --- a/lib/sys.js | |
| +++ b/lib/sys.js | |
| @@ -332,7 +332,9 @@ exports.pump = function (readStream, writeStream, callback) { | |
| if (!readStream.resume) readStream.resume = function () {readStream.emit("resume")}; | |
| readStream.addListener("data", function (chunk) { | |
| - if (writeStream.write(chunk) === false) readStream.pause(); | |
| + var pause = !writeStream.writable; | |
| + if (!pause) pause = writeStream.write(chunk); | |
| + if (false === pause) readStream.pause(); | |
| }); | |
| writeStream.addListener("pause", function () { | |
| diff --git a/test/simple/test-pump-file2tcp-nowritable.js b/test/simple/test-pump-file2tcp-nowritable.js | |
| new file mode 100644 | |
| index 0000000..83c3d71 | |
| --- /dev/null | |
| +++ b/test/simple/test-pump-file2tcp-nowritable.js | |
| @@ -0,0 +1,33 @@ | |
| +common = require("../common"); | |
| +assert = common.assert | |
| +net = require("net"); | |
| +fs = require("fs"); | |
| +sys = require("sys"); | |
| +path = require("path"); | |
| +fn = path.join(common.fixturesDir, 'elipses.txt'); | |
| + | |
| +server = net.createServer(function (stream) { | |
| + common.error('pump!'); | |
| + sys.pump(fs.createReadStream(fn), stream, function () { | |
| + common.error('server stream close'); | |
| + common.error('server close'); | |
| + server.close(); | |
| + }); | |
| + process.nextTick(function () { | |
| + // simulate a failure in the writableStream | |
| + // say, stream was a child_process that failed | |
| + common.error('server stream destroy'); | |
| + stream.destroy(); | |
| + }); | |
| +}); | |
| + | |
| +server.listen(common.PORT, function () { | |
| + conn = net.createConnection(common.PORT); | |
| + conn.addListener("close", function () { | |
| + common.error('client connection close'); | |
| + }); | |
| +}); | |
| + | |
| +process.addListener('exit', function () { | |
| + assert.ok(1); // no uncaughtException | |
| +}); | |
| -- | |
| 1.7.0.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment