From b68cc5cdde704d964883d7e5f4575a6bead95203 Mon Sep 17 00:00:00 2001 From: Matt Sergeant Date: Sun, 25 Jun 2017 20:45:48 -0400 Subject: [PATCH] async-hooks,net: ensure asyncId=null if no handle If the .listen() hasn't been called on the server, there is no handle object. In this case use null as the triggerAsyncId. Fixes: https://github.com/nodejs/node/issues/13548 --- lib/net.js | 3 ++- test/async-hooks/test-get-connections.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/async-hooks/test-get-connections.js diff --git a/lib/net.js b/lib/net.js index 5f4eec89ec87b9..5129a596421e1c 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1558,7 +1558,8 @@ Server.prototype.getConnections = function(cb) { const self = this; function end(err, connections) { - nextTick(self[async_id_symbol], cb, err, connections); + const asyncId = self._handle ? self[async_id_symbol] : null; + nextTick(asyncId, cb, err, connections); } if (!this._usingSlaves) { diff --git a/test/async-hooks/test-get-connections.js b/test/async-hooks/test-get-connections.js new file mode 100644 index 00000000000000..16925536ef5c43 --- /dev/null +++ b/test/async-hooks/test-get-connections.js @@ -0,0 +1,14 @@ +'use strict'; + +const assert = require('assert'); +const net = require('net'); +const server = net.createServer(); + +try { + server.getConnections(() => { + assert.ok(true, "No error thrown") + }); +} +catch (e) { + assert.ok(false, "getConnections threw an error"); +}