Skip to content

Commit

Permalink
Merge pull request caolan#521 from svozza/518-null-pointer
Browse files Browse the repository at this point in the history
Add fix for null pointer exception in _next_fn.
  • Loading branch information
vqvu authored Aug 9, 2016
2 parents 0cd59e9 + 0c4d5c6 commit 5b6cd99
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,14 +770,16 @@ function Stream(generator) {
};

this._next_fn = function (xs) {
// console.log(self.id, '_next', xs, self.paused);
if (self._explicitly_destroyed) {
return;
}

// It's possible to get into a situation where a call to next() is
// scheduled asynchonously, but before it is run, destroy() is called,
// usually by a downstream consumer like take(1). The call to next()
// still completes, and there is nothing the original caller can do can
// do. We do not want to throw in that situation.
if (self._nil_pushed && !self._explicitly_destroyed) {
if (self._nil_pushed) {
throw new Error('Cannot call next after nil');
}

Expand Down Expand Up @@ -1227,7 +1229,11 @@ addMethod('destroy', function () {
});

Stream.prototype._writeOutgoing = function _writeOutgoing(token) {
if (this._nil_pushed && !this._explicitly_destroyed) {
if (this._explicitly_destroyed) {
return;
}

if (this._nil_pushed) {
throw new Error('Cannot write to stream after nil');
}

Expand Down
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,30 @@ exports.GeneratorStream = {
test.ok(push.called, 'The push function should have been called.');
test.ok(!push.threw(), 'The push function call should not have thrown.');
test.done();
},
'Cannot read property __ConsumeGenerator__ of null (#518)': function (test) {
var clock = sinon.useFakeTimers();

var s = _(function (push, next) {
setTimeout(function () {
push(new Error('error'));
_.setImmediate(next); // next2
next(); // next1
}, 0);
});

s.pull(function (err, x) {
s.pull(function () {
});

_.setImmediate(function () {
s.destroy();
});
});

clock.tick(100);
clock.restore();
test.done();
}
};

Expand Down

0 comments on commit 5b6cd99

Please sign in to comment.