diff --git a/lib/client.js b/lib/client.js index b230c368dab..71a1106c22c 100644 --- a/lib/client.js +++ b/lib/client.js @@ -320,7 +320,7 @@ class Client extends DispatcherBase { async [kClose] () { return new Promise((resolve) => { if (!this[kSize]) { - this.destroy(resolve) + resolve(null) } else { this[kClosedResolve] = resolve } @@ -337,6 +337,7 @@ class Client extends DispatcherBase { const callback = () => { if (this[kClosedResolve]) { + // TODO (fix): Should we error here with ClientDestroyedError? this[kClosedResolve]() this[kClosedResolve] = null } @@ -1186,8 +1187,9 @@ function _resume (client, sync) { return } - if (client.closed && !client[kSize]) { - client.destroy() + if (client[kClosedResolve] && !client[kSize]) { + client[kClosedResolve]() + client[kClosedResolve] = null return } diff --git a/lib/dispatcher-base.js b/lib/dispatcher-base.js index 14a5c0acd70..5c0220b5b33 100644 --- a/lib/dispatcher-base.js +++ b/lib/dispatcher-base.js @@ -19,7 +19,7 @@ class DispatcherBase extends Dispatcher { super() this[kDestroyed] = false - this[kOnDestroyed] = [] + this[kOnDestroyed] = null this[kClosed] = false this[kOnClosed] = [] } @@ -127,6 +127,7 @@ class DispatcherBase extends Dispatcher { } this[kDestroyed] = true + this[kOnDestroyed] = this[kOnDestroyed] || [] this[kOnDestroyed].push(callback) const onDestroyed = () => { @@ -167,7 +168,7 @@ class DispatcherBase extends Dispatcher { throw new InvalidArgumentError('opts must be an object.') } - if (this[kDestroyed]) { + if (this[kDestroyed] || this[kOnDestroyed]) { throw new ClientDestroyedError() }