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: correct removeAllListeners in case no event is passed #2088

Merged
merged 4 commits into from
Apr 9, 2021

Conversation

Flarna
Copy link
Member

@Flarna Flarna commented Apr 8, 2021

Which problem is this PR solving?

fixes #2068

Short description of the changes

Correct handling of patched removeAllListeners() in AbstractAsyncHooksContextManager in case it's called without argument.

In this case all listener types should be removed.
Additionally it's important to forward arguments instead of passing undefined as node.js checks arguments.length.

Correct handling of patched removeAllListeners() in AbstractAsyncHooksContextManager in case
it's called without argument.

In this case all listener types should be removed.
Additionally it's imporant to forward arguments instead of passing undefined as node.js checks arguments.length.
@codecov
Copy link

codecov bot commented Apr 8, 2021

Codecov Report

Merging #2088 (15f5dc1) into main (4a3fd1f) will increase coverage by 0.02%.
The diff coverage is 100.00%.

❗ Current head 15f5dc1 differs from pull request most recent head dd9e1bf. Consider uploading reports for the commit dd9e1bf to get more accurate results

@@            Coverage Diff             @@
##             main    #2088      +/-   ##
==========================================
+ Coverage   92.53%   92.55%   +0.02%     
==========================================
  Files         133      133              
  Lines        4848     4851       +3     
  Branches     1000     1002       +2     
==========================================
+ Hits         4486     4490       +4     
+ Misses        362      361       -1     
Impacted Files Coverage Δ
...sync-hooks/src/AbstractAsyncHooksContextManager.ts 97.01% <100.00%> (+0.13%) ⬆️
...emetry-core/src/platform/node/RandomIdGenerator.ts 93.75% <0.00%> (+6.25%) ⬆️

@@ -146,10 +146,14 @@ export abstract class AbstractAsyncHooksContextManager
const contextManager = this;
return function (this: never, event: string) {
const map = contextManager._getPatchMap(ee);
if (map?.[event] !== undefined) {
delete map[event];
if (map !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const map = null; 
map !== undefined; 

map['aa'] !== undefined; 
// error
Suggested change
if (map !== undefined) {
if (map) {

Copy link
Member Author

@Flarna Flarna Apr 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used !== undefined here because it's used at a lot places in this file. Usually I prefer != null.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but if the map is null then the condition will pass and you will have undefined object exception

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!= null would catch both null and undefined

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the map will never be null in this case, only undefined. If a dev made a change which allowed this to be null, the test would fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that map is never null here, i don't see the benefit of checking for both undefined and null

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As said I usually use != null. But there are a lot places in this file where !== undefined is already used so the no 1 rule 'When in Rome, Do as the Romans Do' wins.
I would prefer to do cleanups like this in a non bugfix PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well if (map) { would simply do the trick and will be a bullet proof for any future changes, you don't need to use != null

@Flarna Flarna added the bug Something isn't working label Apr 9, 2021
@@ -146,10 +146,14 @@ export abstract class AbstractAsyncHooksContextManager
const contextManager = this;
return function (this: never, event: string) {
const map = contextManager._getPatchMap(ee);
if (map?.[event] !== undefined) {
delete map[event];
if (map !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well if (map) { would simply do the trick and will be a bullet proof for any future changes, you don't need to use != null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

context-async-hooks throws unhandled exception with follow-redirects 1.13.3 dependency
4 participants