Skip to content

Commit

Permalink
fix: close and destroy overlap (nodejs#2068)
Browse files Browse the repository at this point in the history
* fix: close and destroy overlap

* fixup
  • Loading branch information
ronag authored and metcoder95 committed Jul 21, 2023
1 parent da6898c commit 2a689ec
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 3 additions & 2 deletions lib/dispatcher-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DispatcherBase extends Dispatcher {
super()

this[kDestroyed] = false
this[kOnDestroyed] = []
this[kOnDestroyed] = null
this[kClosed] = false
this[kOnClosed] = []
}
Expand Down Expand Up @@ -127,6 +127,7 @@ class DispatcherBase extends Dispatcher {
}

this[kDestroyed] = true
this[kOnDestroyed] = this[kOnDestroyed] || []
this[kOnDestroyed].push(callback)

const onDestroyed = () => {
Expand Down Expand Up @@ -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()
}

Expand Down

0 comments on commit 2a689ec

Please sign in to comment.