diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index d853441b3cc844..4e2f26dc63d120 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -493,30 +493,30 @@ function onwrite(stream, er) { onwriteError(stream, state, er, cb); } } else { - // Check if we're actually ready to finish, but don't emit yet - var finished = needFinish(state) || stream.destroyed; - - if (!finished && - !state.corked && + if (!state.corked && !state.bufferProcessing && state.bufferedRequest) { clearBuffer(stream, state); } - if (sync) { - // It is a common case that the callback passed to .write() is always - // the same. In that case, we do not schedule a new nextTick(), but rather - // just increase a counter, to improve performance and avoid memory - // allocations. - if (state.afterWriteTickInfo !== null && - state.afterWriteTickInfo.cb === cb) { - state.afterWriteTickInfo.count++; + if (state.ending || state.needDrain || cb !== nop) { + if (sync) { + // It is a common case that the callback passed to .write() is always + // the same. In that case, we do not schedule a new nextTick(), but + // rather just increase a counter, to improve performance and avoid + // memory allocations. + if (state.afterWriteTickInfo !== null && + state.afterWriteTickInfo.cb === cb) { + state.afterWriteTickInfo.count++; + } else { + state.afterWriteTickInfo = { count: 1, cb, stream, state }; + process.nextTick(afterWriteTick, state.afterWriteTickInfo); + } } else { - state.afterWriteTickInfo = { count: 1, cb, stream, state }; - process.nextTick(afterWriteTick, state.afterWriteTickInfo); + afterWrite(stream, state, 1, cb); } } else { - afterWrite(stream, state, 1, cb); + state.pendingcb--; } } }