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

forEach should unsubscribe if callback function throws #1411

Closed
zenparsing opened this issue Mar 1, 2016 · 5 comments · Fixed by #1434
Closed

forEach should unsubscribe if callback function throws #1411

zenparsing opened this issue Mar 1, 2016 · 5 comments · Fixed by #1434
Assignees
Labels
bug Confirmed bug

Comments

@zenparsing
Copy link

Test case:

new Rx.Observable(sink => {
    sink.next(1); 
    sink.next(2); 
    sink.complete();
}).forEach(x => {
    console.log(x);
    throw new Error("boom");
}).catch(err => console.log(err));

Expected result: Only "1" is logged.
Actual result: "1" and "2" are logged.

I recently fixed this issue over on es-observable:

@benlesh
Copy link
Member

benlesh commented Mar 7, 2016

Ah... good catch, thank you @zenparsing!

@benlesh benlesh added the bug Confirmed bug label Mar 7, 2016
@benlesh benlesh self-assigned this Mar 7, 2016
@benlesh
Copy link
Member

benlesh commented Mar 8, 2016

Actually, @zenparsing, looking at your reference implementation, there's a tricky bit your not covering... consider this:

const syncObservable = new Observable((observer) => {
   const expensiveThing = new ExpensiveThing();
   observer.next('I am a little teapot!');
   return () => expensiveThing.destroy();
});

syncObservable.forEach(x => {
  throw new Error('dammit, I am a sugar bowl');
});

In the case above, the error will be thrown synchronously before subscription can be returned. So your reference impl will throw on this line

I haven't tested this, but I think that's what will happen.

I'm working up a solution for RxJS 5 now.

@zenparsing
Copy link
Author

Yeah, I was trying to simplify the algorithm by letting the error propagate in synchronous case, but clearly I messed it up : )

@trxcllnt
Copy link
Member

trxcllnt commented Mar 8, 2016

@zenparsing we had to go to quite a bit of trouble to ensure that both a. the source unsubscription logic is run and b. the error is thrown to outside consumers when a next/error/complete handler throws on synchronous dispatch.

@lock
Copy link

lock bot commented Jun 7, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Confirmed bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants