Skip to content

Commit 77a2064

Browse files
committed
http: also warn the user when preventing the leak
1 parent 9717e90 commit 77a2064

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/_http_server.js

+15
Original file line numberDiff line numberDiff line change
@@ -822,12 +822,27 @@ const requestHeaderFieldsTooLargeResponse = Buffer.from(
822822
`HTTP/1.1 431 ${STATUS_CODES[431]}\r\n` +
823823
'Connection: close\r\n\r\n', 'ascii',
824824
);
825+
826+
function warnUnclosedSocket() {
827+
if (warnUnclosedSocket.emitted) {
828+
return;
829+
}
830+
831+
warnUnclosedSocket.emitted = true;
832+
process.emitWarning(
833+
'An error event has already been emitted on the request. ' +
834+
'Please use the destroy method on the socket while handling a clientError event.',
835+
);
836+
}
837+
825838
function socketOnError(e) {
826839
// Ignore further errors
827840
this.removeListener('error', socketOnError);
828841

829842
if (this.listenerCount('error', noop) === 0) {
830843
this.on('error', noop);
844+
} else {
845+
warnUnclosedSocket();
831846
}
832847

833848
if (!this.server.emit('clientError', e, this)) {

test/parallel/test-http-socket-error-listeners.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Flags: --no-warnings
2+
13
'use strict';
24

35
const common = require('../common');
@@ -15,8 +17,7 @@ const net = require('net');
1517
{
1618
let i = 0;
1719
let socket;
18-
const mustNotCall = common.mustNotCall.bind(null);
19-
process.on('warning', mustNotCall);
20+
process.on('warning', common.mustCall());
2021

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

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

4243
socket.on('close', () => {
4344
server.close();
44-
process.removeListener('warning', mustNotCall);
4545
});
4646
});
4747
}

0 commit comments

Comments
 (0)