-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix removing listeners in wrapped event emitters
The `.emitWithContext` function in the `ScopeManager`, which is called from the `.wrapEmitter` function in the `Tracer`, modifies a given `EventEmitter` so that the callbacks passed to it when registering a listener are bound to the active span of the asynchronous context in which the listener was registered. That is, calling `scopeManager.active()` or `tracer.currentSpan()` from the listener should return the span that was active at the point where the listener was registered. This is done by wrapping the callback in a function that closes over the current span, calling the original callback from within `scopeManager.withContext` with the closed over span, and registering this wrapper function as the callback for the event emitter's listener. Since the callback that is registered for an event emitter's listener is a wrapper function over the callback, and not the original callback, future attempts to remove the listener from the event emitter silently fail, as the callback function that is passed to `.removeListener` is not the callback function that was originally registered. This commit fixes this by keeping track of which wrapper functions were created for which events and callbacks, and modifying the given event emitter further, intercepting calls to methods that remove listeners and replacing the given callback with its previously created wrapper. This commit also adds tests for the .emitWithContext function.
- Loading branch information
Showing
3 changed files
with
157 additions
and
6 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
.../nodejs/.changesets/fix-removing-event-listeners-from-wrapped-event-emitters.md
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
bump: "patch" | ||
type: "fix" | ||
--- | ||
|
||
Fix removing event listeners from wrapped event emitters. When using | ||
`tracer.wrapEmitter` to wrap an event emitter, it was not possible to remove | ||
any listeners that were added after the event emitter was wrapped. |
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