From 15cf40d4a45ea5997cb540ee05b980b10360708f Mon Sep 17 00:00:00 2001 From: Yilun Date: Tue, 30 Jun 2020 23:53:55 -0700 Subject: [PATCH] Fix temp client websocket leaking Signed-off-by: Yilun --- lib/index.js | 14 +++++++++++++- package.json | 2 +- src/index.js | 12 +++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 24e6dcc..0b362c9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -323,14 +323,21 @@ client.onConnect(() => { } }, pingInterval); let lastForceUpdateTime = new Date(); + let tempClient; setInterval(async () => { + if (tempClient) { + tempClient.close().catch(e => { + console.error('Close temp client error:', e); + }); + } + let tempIdentifier = _nknSdk.default.util.randomBytesHex(4); if (identifier) { tempIdentifier += '.' + identifier; } - let tempClient = new _nknSdk.default.MultiClient({ + tempClient = new _nknSdk.default.MultiClient({ originalClient: true, seed: wallet.getSeed(), identifier: tempIdentifier @@ -356,6 +363,11 @@ client.onConnect(() => { } } } + + tempClient.close().catch(e => { + console.error('Close temp client error:', e); + }); + tempClient = null; }, 3000); }); }, forcePingInterval); diff --git a/package.json b/package.json index a9da361..50f2948 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nkn-shell-daemon", - "version": "1.0.7", + "version": "1.0.9", "description": "NKN Shell Daemon", "main": "nshd.js", "bin": { diff --git a/src/index.js b/src/index.js index 8891d02..72ab327 100644 --- a/src/index.js +++ b/src/index.js @@ -303,12 +303,18 @@ client.onConnect(() => { }, pingInterval) let lastForceUpdateTime = new Date() + let tempClient setInterval(async () => { + if (tempClient) { + tempClient.close().catch((e) => { + console.error('Close temp client error:', e); + }) + } let tempIdentifier = nkn.util.randomBytesHex(4) if (identifier) { tempIdentifier += '.' + identifier } - let tempClient = new nkn.MultiClient({ + tempClient = new nkn.MultiClient({ originalClient: true, seed: wallet.getSeed(), identifier: tempIdentifier, @@ -331,6 +337,10 @@ client.onConnect(() => { } } } + tempClient.close().catch((e) => { + console.error('Close temp client error:', e); + }) + tempClient = null }, 3000); }) }, forcePingInterval)