diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index a6ade2a9c4..1617665b52 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. + } } }