diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index ce4cd6496fd1d2..a6f9ed9fa476fd 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -602,12 +602,18 @@ function setupChannel(target, channel) { } else { process.nextTick(function() { req.oncomplete(); }); } - } else if (!swallowErrors) { - const ex = errnoException(err, 'write'); - if (typeof callback === 'function') { - process.nextTick(callback, ex); - } else { - this.emit('error', ex); // FIXME(bnoordhuis) Defer to next tick. + } else { + // Cleanup handle on error + if (obj && obj.postSend) + obj.postSend(handle); + + if (!swallowErrors) { + const ex = errnoException(err, 'write'); + if (typeof callback === 'function') { + process.nextTick(callback, ex); + } else { + this.emit('error', ex); // FIXME(bnoordhuis) Defer to next tick. + } } }