-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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: Cleanup hooks when they are not used anymore #12852
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -459,27 +459,19 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> { | |
|
||
/** @inheritdoc */ | ||
public on(hook: string, callback: unknown): () => void { | ||
// Note that the code below, with nullish coalescing assignment, | ||
// may reduce the code, so it may be switched to when Node 14 support | ||
// is dropped (the `??=` operator is supported since Node 15). | ||
// (this._hooks[hook] ??= []).push(callback); | ||
if (!this._hooks[hook]) { | ||
this._hooks[hook] = []; | ||
} | ||
const hooks = (this._hooks[hook] = this._hooks[hook] || []); | ||
|
||
// @ts-expect-error We assue the types are correct | ||
this._hooks[hook].push(callback); | ||
hooks.push(callback); | ||
|
||
// This function returns a callback execution handler that, when invoked, | ||
// deregisters a callback. This is crucial for managing instances where callbacks | ||
// need to be unregistered to prevent self-referencing in callback closures, | ||
// ensuring proper garbage collection. | ||
return () => { | ||
const hooks = this._hooks[hook]; | ||
|
||
if (hooks) { | ||
// @ts-expect-error We assue the types are correct | ||
const cbIndex = hooks.indexOf(callback); | ||
// @ts-expect-error We assue the types are correct | ||
const cbIndex = hooks.indexOf(callback); | ||
if (cbIndex > -1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also adding this safeguard that was missing before - if trying to remove a hook twice, this should not fail and remove the first entry or similar. |
||
hooks.splice(cbIndex, 1); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While debugging a test failure, I noticed that this surfaced another problem - we were potentially double-ending the span here. If the idle span already ended on it's own, we do not want to/need to end it anymore here! Calling this twice lead to some issues as it tried to remove the hooks twice, ...