Skip to content

Commit

Permalink
net: implement capture rejections for 'connection' event
Browse files Browse the repository at this point in the history
PR-URL: #27867
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
mcollina authored and targos committed Dec 9, 2019
1 parent 334d4f6 commit 4eecee0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,11 @@ Server.prototype.setTimeout = function setTimeout(msecs, callback) {
};

Server.prototype[EE.captureRejectionSymbol] = function(
err, event, req, res) {
err, event, ...args) {

switch (event) {
case 'request':
const [ , res] = args;
if (!res.headersSent && !res.writableEnded) {
// Don't leak headers.
for (const name of res.getHeaderNames()) {
Expand All @@ -375,7 +376,8 @@ Server.prototype[EE.captureRejectionSymbol] = function(
}
break;
default:
this.emit('error', err);
net.Server.prototype[Symbol.for('nodejs.rejection')]
.call(this, err, event, ...args);
}
};

Expand Down
13 changes: 13 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,19 @@ function emitCloseNT(self) {
}


Server.prototype[EventEmitter.captureRejectionSymbol] = function(
err, event, sock) {

switch (event) {
case 'connection':
sock.destroy(err);
break;
default:
this.emit('error', err);
}
};


// Legacy alias on the C++ wrapper object. This is not public API, so we may
// want to runtime-deprecate it at some point. There's no hurry, though.
ObjectDefineProperty(TCP.prototype, 'owner', {
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-net-server-capture-rejection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const events = require('events');
const { createServer, connect } = require('net');

events.captureRejections = true;

const server = createServer(common.mustCall(async (sock) => {
server.close();

const _err = new Error('kaboom');
sock.on('error', common.mustCall((err) => {
assert.strictEqual(err, _err);
}));
throw _err;
}));

server.listen(0, common.mustCall(() => {
const sock = connect(
server.address().port,
server.address().host
);

sock.on('close', common.mustCall());
}));

0 comments on commit 4eecee0

Please sign in to comment.