destroyWritable may silently swallow legitimate writable errors
The destroyWritable function attaches a no-op once('error') listener before calling writable.destroy(error). This listener consumes the asynchronous 'error' event that Node emits when destroy() is called with an error on a writable that is not yet destroyed. However, if the writable is already destroyed (the early return on line 61-63), the function returns without attaching the error listener. In that case, if writable.destroy(error) was called elsewhere (or the writable was destroyed by some other mechanism) and the error event fires asynchronously, there is no listener to catch it, potentially crashing the process. The early return should also attach a no-op error listener to the already-destroyed writable to ensure any pending error events are consumed.
function destroyWritable(writable: Writable, error: Error) { if (writable.destroyed) { // Ensure any pending error events from a previous destroy are consumed writable.once("error", () => {}); return; }
// The write promise carries this error to the caller. Also consume the // asynchronous error event from destroy so it cannot crash the process // after the writable error monitor has been cleaned up. writable.once("error", () => {}); writable.destroy(error); }