diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 286bae090a1..a6077abb063 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -2567,6 +2567,15 @@ class Http2Stream extends Duplex { }); } } + + // A write in flight may never get its native completion callback once the + // stream tears down; settle it so the Writable can clean up. + if (state.writeCb !== null) { + const writeCb = state.writeCb; + state.writeCb = null; + state.writePending = 0; + writeCb(err); + } callback(err); } // The Http2Stream can be destroyed if it has closed and if the readable diff --git a/test/parallel/test-http2-close-while-writing.js b/test/parallel/test-http2-close-while-writing.js index c0a05c4a8da..f8395002580 100644 --- a/test/parallel/test-http2-close-while-writing.js +++ b/test/parallel/test-http2-close-while-writing.js @@ -7,6 +7,7 @@ if (!common.hasCrypto) { common.skip('missing crypto'); } +const assert = require('assert'); const http2 = require('http2'); const key = fixtures.readKey('agent8-key.pem', 'binary'); @@ -23,11 +24,24 @@ let client_stream; server.on('session', common.mustCall(function(session) { session.on('stream', common.mustCall(function(stream) { - stream.resume(); + // Client destroys mid-stream without END_STREAM (clean RST code). + // Peer reset before END_STREAM surfaces as ERR_HTTP2_STREAM_ABORTED. + stream.on('error', common.mustNotCall()); + + // Every write dispatched before close must have its callback invoked. + let writes = 0; + let writeCallbacks = 0; stream.on('data', function() { - this.write(Buffer.alloc(1)); + writes++; + this.write(Buffer.alloc(1), () => { + writeCallbacks++; + }); process.nextTick(() => client_stream.destroy()); }); + stream.on('close', common.mustCall(() => { + assert.strictEqual(writeCallbacks, writes); + })); + stream.resume(); })); }));