async-listener
async-listener copied to clipboard
Bug fix - listener leaks in case of nested wrapped methods and an error in the method
The bug is subtle but still there -
If we have two nested methods that are both wrapped by glue.js, the result is that create will be called twice for the same tick. This by itself is not a big problem. The important thing is that the list of listeners is pushed twice into listenersStack
However, if the callback passed to those two have an exception, the asyncCatcher method in glue.js
only pops listenersStack once.
Once example where this may happen is if we import domains after async-listener, the domain import causes process.nextTick to call async listener twice. The reason is because domain replaces the underlying _currentTickHandler method. If domain is loaded after async listener, the _currentTickHandler method is replaced with a wrapped method (wrapped by async listener).
The net result is that
if we load async-listener, then domain, we will have both process.nextTick and _currentTickHandler wrapped.
if we load domain, then async-listener, we will have only process.nextTick wrapped.
I have included two new test files to demonstrate the two cases as well as a fix to the problem. Just run the test cases without the fix and you can see the problem.
See the files
`````` double-calls-to-create-if-importing-domain-after-async-listener.tap.js double-calls-to-create-not-happening-if-importing-domain-before-async-listener.tap.js```
Also note that the problem is not local to just domains. I have noticed that using fs.readFile calls fs.open directly passing the same callback, resulting in nested wrapped methods and the same issue.
Thanks for this patch! Unfortunately, some of your new tests aren't passing:
not ok test/double-calls-to-create-if-importing-domain-after-async-listener.tap.js 0/4
Command: "/usr/local/bin/iojs double-calls-to-create-if-importing-domain-after-async-listener.tap.js"
TAP version 13
not ok 1 number of calls to create
---
file: /Users/ogd/Documents/projects/async-listener/test/double-calls-to-create-if-importing-domain-after-async-listener.tap.js
line: 36
column: 15
stack:
- |
getCaller (/Users/ogd/Documents/projects/async-listener/node_modules/tap/lib/tap-assert.js:439:17)
- |
assert (/Users/ogd/Documents/projects/async-listener/node_modules/tap/lib/tap-assert.js:21:16)
- |
Function.equal (/Users/ogd/Documents/projects/async-listener/node_modules/tap/lib/tap-assert.js:163:10)
- |
Test._testAssert [as equal] (/Users/ogd/Documents/projects/async-listener/node_modules/tap/lib/tap-test.js:87:16)
- |
Func (/Users/ogd/Documents/projects/async-listener/test/double-calls-to-create-if-importing-domain-after-async-listener.tap.js:36:15)
- |
/Users/ogd/Documents/projects/async-listener/glue.js:198:31
- |
doNTCallback0 (node.js:408:9)
- |
process._tickDomainCallback (node.js:378:13)
- |
process.<anonymous> (/Users/ogd/Documents/projects/async-listener/index.js:19:15)
- |
Function.Module.runMain (module.js:474:11)
found: 1
wanted: 2
...
not ok 2 number of calls to create - before the async method
---
file: /Users/ogd/Documents/projects/async-listener/test/double-calls-to-create-if-importing-domain-after-async-listener.tap.js
line: 66
column: 19
stack:
- |
getCaller (/Users/ogd/Documents/projects/async-listener/node_modules/tap/lib/tap-assert.js:439:17)
- |
assert (/Users/ogd/Documents/projects/async-listener/node_modules/tap/lib/tap-assert.js:21:16)
- |
Function.equal (/Users/ogd/Documents/projects/async-listener/node_modules/tap/lib/tap-assert.js:163:10)
- |
Test._testAssert [as equal] (/Users/ogd/Documents/projects/async-listener/node_modules/tap/lib/tap-test.js:87:16)
- |
null.<anonymous> (/Users/ogd/Documents/projects/async-listener/test/double-calls-to-create-if-importing-domain-after-async-listener.tap.js:66:19)
- |
null._repeat (/Users/ogd/Documents/projects/async-listener/glue.js:198:31)
- |
wrapper [as _onTimeout] (timers.js:267:19)
- |
Timer.listOnTimeout (timers.js:89:15)
found: 1
wanted: 2
...
not ok 3 number of calls to create - in the async method (who should not be listened on by async the registered listener
---
file: node.js
line: 408
column: 9
stack:
- |
getCaller (/Users/ogd/Documents/projects/async-listener/node_modules/tap/lib/tap-assert.js:439:17)
- |
assert (/Users/ogd/Documents/projects/async-listener/node_modules/tap/lib/tap-assert.js:21:16)
- |
Function.equal (/Users/ogd/Documents/projects/async-listener/node_modules/tap/lib/tap-assert.js:163:10)
- |
Test._testAssert [as equal] (/Users/ogd/Documents/projects/async-listener/node_modules/tap/lib/tap-test.js:87:16)
- |
/Users/ogd/Documents/projects/async-listener/test/double-calls-to-create-if-importing-domain-after-async-listener.tap.js:69:23
- |
/Users/ogd/Documents/projects/async-listener/glue.js:198:31
- |
doNTCallback0 (node.js:408:9)
- |
process._tickDomainCallback (node.js:378:13)
- |
process.<anonymous> (/Users/ogd/Documents/projects/async-listener/index.js:19:15)
found: 1
wanted: 2
...
not ok 4 test/double-calls-to-create-if-importing-domain-after-async-listener.tap.js
---
exit: 1
command: "/usr/local/bin/iojs double-calls-to-create-if-importing-domain-after-async-listener.tap.js"
...
1..4
# tests 4
# fail 4
A few things have changed on master since you wrote this (I rebased your patch against current master), so it's possible things need a little adjustment. It's also possible that something's changed in iojs that's affected this. If you can get the tests passing or tweaked to your satisfaction, I'd be happy to merge this.
I have removed unneeded test - apparently something different has fixed the trigger for this fix - that if you imported domains after async listener you got some strange things happening.
removed the obsolete test.
But still, the PR is still valid - it fixes cases of nested callback wrapping
I believe this needs merged otherwise results in max listeners error per #37 #48