addyosmani/aura — pull request #117 by bobholt (2012-09-14 19:44:43+00:00)
Title: fix potential race condition in core.publish

There is a potential race condition between `core.publish` and `core.stop`.

If `core.stop` is called on a current subscriber to `core.publish` as a callback in 'core.publish', it removes the subscriber in the current queue `core.publish` is iterating over, which reduces the length of the array.

Since `core.publish` has already cached the length of this array, it can exceed the modified number of elements, causing an error.

Example:
```javascript
// within mySubscriber module
sandbox.subscribe('stopEvent', 'mySubscriber', function(){

sandbox.stop('mySubscriber', element);

});

// within anotherSubscriber module
sandbox.subscribe('stopEvent', 'anotherSubscriber', function(){

sandbox.stop('anotherSubscriber', element);

});

// within a third module
sandbox.publish('stopEvent');
```

This fix nulls the subscriber's callback in `core.stop` instead of removing the entire subscriber. `core.publish` now checks for this nulled callback. If it finds it, it removes the subscriber from the array and postpones its iteration (as the previous `i+1` is the new `i`).