Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: close and destroy overlap #2068

Merged
merged 2 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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