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

Prevent double wrapping event emitter listeners #3133

Merged
merged 7 commits into from
Aug 3, 2022
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ All notable changes to this project will be documented in this file.

### :bug: (Bug Fix)

* fix(context-async-hooks): Ensure listeners added using `once` can be removed using `removeListener`
[#3133](https://github.com/open-telemetry/opentelemetry-js/pull/3133)

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ implements ContextManager {
) {
const contextManager = this;
return function (this: never, event: string, listener: Func<void>) {
/**
* This check is required to prevent double-wrapping the listener.
* The implementation for ee.once wraps the listener and calls ee.on.
* Without this check, we would wrap that wrapped listener.
* This causes an issue because ee.removeListener depends on the onceWrapper
* to properly remove the listener. If we wrap their wrapper, we break
* that detection.
*/
if (contextManager._wrapped) {
vmarchaud marked this conversation as resolved.
Show resolved Hide resolved
return original.call(this, event, listener);
}
let map = contextManager._getPatchMap(ee);
if (map === undefined) {
map = contextManager._createPatchMap(ee);
Expand All @@ -189,7 +200,16 @@ implements ContextManager {
const patchedListener = contextManager.bind(context, listener);
// store a weak reference of the user listener to ours
listeners.set(listener, patchedListener);
return original.call(this, event, patchedListener);

/**
* See comment at the start of this function for the explanation of this property.
*/
contextManager._wrapped = true;
try {
return original.call(this, event, patchedListener);
} finally {
contextManager._wrapped = false;
}
};
}

Expand All @@ -204,4 +224,5 @@ implements ContextManager {
}

private readonly _kOtListeners = Symbol('OtListeners');
private _wrapped = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,28 @@ for (const contextManagerClass of [
patchedEE.emit('test');
});

it('should remove event handler enabled by .once using removeListener (when enabled)', () => {
const ee = new EventEmitter();
const context = ROOT_CONTEXT.setValue(key1, 1);
const patchedEE = contextManager.bind(context, ee);
function handler() {}
patchedEE.once('test', handler);
assert.strictEqual(patchedEE.listeners('test').length, 1);
patchedEE.removeListener('test', handler);
assert.strictEqual(patchedEE.listeners('test').length, 0);
});

it('should remove event handler enabled by .once using off (when enabled)', () => {
const ee = new EventEmitter();
const context = ROOT_CONTEXT.setValue(key1, 1);
const patchedEE = contextManager.bind(context, ee);
const handler = () => { };
patchedEE.once('test', handler);
assert.strictEqual(patchedEE.listeners('test').length, 1);
patchedEE.off('test', handler);
assert.strictEqual(patchedEE.listeners('test').length, 0);
});

it('should return current context and removeAllListeners (when enabled)', done => {
const ee = new EventEmitter();
const context = ROOT_CONTEXT.setValue(key1, 1);
Expand Down