diff options
author | isaacs <i@izs.me> | 2013-04-08 22:00:26 +0400 |
---|---|---|
committer | Fedor Indutny <fedor.indutny@gmail.com> | 2013-04-09 02:09:51 +0400 |
commit | c93af860a079654becb3b1d06184ab7428dedffb (patch) | |
tree | 5a00aac479ff1764cd8d1b3c34dc0686f7107376 /lib | |
parent | e4b716efaa00f34fe4d9f1c141c93e2aa994328c (diff) |
stream: call write cb before finish event
Since 049903e, an end callback could be called before a write
callback if end() is called before the write is done. This patch
resolves the issue.
In collaboration with @gne
Fixes felixge/node-formidable#209
Fixes #5215
Diffstat (limited to 'lib')
-rw-r--r-- | lib/_stream_writable.js | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 346f2cc96ab..c060e015a05 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -240,7 +240,8 @@ function onwrite(stream, er) { if (er) onwriteError(stream, state, sync, er, cb); else { - var finished = finishMaybe(stream, state); + // Check if we're actually ready to finish, but don't emit yet + var finished = needFinish(stream, state); if (!finished && !state.bufferProcessing && state.buffer.length) clearBuffer(stream, state); @@ -259,6 +260,8 @@ function afterWrite(stream, state, finished, cb) { if (!finished) onwriteDrain(stream, state); cb(); + if (finished) + finishMaybe(stream, state); } // Must force callback to be called on nextTick, so that we don't @@ -326,15 +329,21 @@ Writable.prototype.end = function(chunk, encoding, cb) { endWritable(this, state, cb); }; + +function needFinish(stream, state) { + return (state.ending && + state.length === 0 && + !state.finished && + !state.writing); +} + function finishMaybe(stream, state) { - if (state.ending && - state.length === 0 && - !state.finished && - !state.writing) { + var need = needFinish(stream, state); + if (need) { state.finished = true; stream.emit('finish'); } - return state.finished; + return need; } function endWritable(stream, state, cb) { |