diff --git a/lib/_http_server.js b/lib/_http_server.js index 5d25e459d84a48..d8b43ad5cbc459 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -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)) { diff --git a/test/parallel/test-http-socket-error-listeners.js b/test/parallel/test-http-socket-error-listeners.js index 5e712aa98ffc19..1108ac7ee27f1d 100644 --- a/test/parallel/test-http-socket-error-listeners.js +++ b/test/parallel/test-http-socket-error-listeners.js @@ -1,3 +1,5 @@ +// Flags: --no-warnings + 'use strict'; const common = require('../common'); @@ -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()); @@ -41,7 +42,6 @@ const net = require('net'); socket.on('close', () => { server.close(); - process.removeListener('warning', mustNotCall); }); }); }