From 7c3c06224cc8f63230ee2352165cfe9f7365fd4d Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 15 Mar 2020 01:34:01 +0100 Subject: [PATCH] http: don't emit 'readable' after 'close' PR-URL: https://github.com/nodejs/node/pull/32277 Refs: https://github.com/nodejs/node/issues/28710 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- lib/_http_server.js | 6 ++++-- test/parallel/test-async-hooks-http-parser-destroy.js | 5 ++++- test/parallel/test-http-no-read-no-dump.js | 4 ++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index d39c2b781adc7c..7b0cd8cd8ef597 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -665,9 +665,12 @@ function clearIncoming(req) { if (parser && parser.incoming === req) { if (req.readableEnded) { parser.incoming = null; + req.emit('close'); } else { req.on('end', clearIncoming); } + } else { + req.emit('close'); } } @@ -678,7 +681,6 @@ function resOnFinish(req, res, socket, state, server) { assert(state.incoming.length === 0 || state.incoming[0] === req); state.incoming.shift(); - clearIncoming(req); // If the user never called req.read(), and didn't pipe() or // .resume() or .on('data'), then we call req._dump() so that the @@ -687,7 +689,7 @@ function resOnFinish(req, res, socket, state, server) { req._dump(); res.detachSocket(socket); - req.emit('close'); + clearIncoming(req); process.nextTick(emitCloseNT, res); if (res._last) { diff --git a/test/parallel/test-async-hooks-http-parser-destroy.js b/test/parallel/test-async-hooks-http-parser-destroy.js index 22b5a0ff11b889..6cbc2043b09583 100644 --- a/test/parallel/test-async-hooks-http-parser-destroy.js +++ b/test/parallel/test-async-hooks-http-parser-destroy.js @@ -1,5 +1,5 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); const http = require('http'); @@ -45,6 +45,9 @@ async_hooks.createHook({ }).enable(); const server = http.createServer((req, res) => { + req.on('close', common.mustCall(() => { + req.on('readable', common.mustNotCall()); + })); res.end('Hello'); }); diff --git a/test/parallel/test-http-no-read-no-dump.js b/test/parallel/test-http-no-read-no-dump.js index 17d36c56b2eeba..c6ce19845d80ab 100644 --- a/test/parallel/test-http-no-read-no-dump.js +++ b/test/parallel/test-http-no-read-no-dump.js @@ -11,6 +11,10 @@ const server = http.createServer((req, res) => { res.writeHead(200); res.flushHeaders(); + req.on('close', common.mustCall(() => { + req.on('end', common.mustNotCall()); + })); + req.connection.on('pause', () => { res.end(); onPause();