Skip to content

Commit

Permalink
http: also warn the user when preventing the leak
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Feb 23, 2023
1 parent 9717e90 commit 77a2064
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
15 changes: 15 additions & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,12 +822,27 @@ const requestHeaderFieldsTooLargeResponse = Buffer.from(
`HTTP/1.1 431 ${STATUS_CODES[431]}\r\n` +
'Connection: close\r\n\r\n', 'ascii',
);

function warnUnclosedSocket() {
if (warnUnclosedSocket.emitted) {
return;
}

warnUnclosedSocket.emitted = true;
process.emitWarning(
'An error event has already been emitted on the request. ' +
'Please use the destroy method on the socket while handling a clientError event.',
);
}

function socketOnError(e) {
// Ignore further errors
this.removeListener('error', socketOnError);

if (this.listenerCount('error', noop) === 0) {
this.on('error', noop);
} else {
warnUnclosedSocket();
}

if (!this.server.emit('clientError', e, this)) {
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-http-socket-error-listeners.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Flags: --no-warnings

'use strict';

const common = require('../common');
Expand All @@ -15,8 +17,7 @@ const net = require('net');
{
let i = 0;
let socket;
const mustNotCall = common.mustNotCall.bind(null);
process.on('warning', mustNotCall);
process.on('warning', common.mustCall());

const server = http.createServer(common.mustNotCall());

Expand All @@ -41,7 +42,6 @@ const net = require('net');

socket.on('close', () => {
server.close();
process.removeListener('warning', mustNotCall);
});
});
}
Expand Down

0 comments on commit 77a2064

Please sign in to comment.