Skip to content

Commit

Permalink
http: don't emit 'readable' after 'close'
Browse files Browse the repository at this point in the history
PR-URL: #32277
Refs: #28710
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
ronag authored and addaleax committed Mar 30, 2020
1 parent 6d4d299 commit 7c3c062
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand All @@ -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
Expand All @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-async-hooks-http-parser-destroy.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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');
});

Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-http-no-read-no-dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7c3c062

Please sign in to comment.