joyent/node — pull request #6555 by vkurchatkin (2013-11-21 10:48:27+00:00)
Title: node: fix removing `AsyncListener` in callback

Currently removing `AsyncListener` inside `before` callback (or original event callback itself) prevents `after` callback from firing. Re-adding listener makes it fire, but `storageValue` is `undefined`.

This also makes example from docs incorrect:
```javascript
{
before: function onBefore(context, storage) {
// Need to remove the listener while logging or will end up
// with an infinite call loop.
process.removeAsyncListener(key);
console.log('uid: %s is about to run', storage.uid);
process.addAsyncListener(key);
},
after: function onAfter(context, storage) {
process.removeAsyncListener(key);
console.log('uid: %s is about to run', storage.uid);
process.addAsyncListener(key);
}
}
```
In this example `after` is never called because of an error in callback, but if you remove it the code will crash with `TypeError: Cannot read property 'uid' of undefined`.