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

Errors thrown in Subscriber's next handler should call unsubscribe logic #1186

Closed
benlesh opened this issue Jan 13, 2016 · 8 comments
Closed
Assignees
Labels
bug Confirmed bug
Milestone

Comments

@benlesh
Copy link
Member

benlesh commented Jan 13, 2016

It was supposed to be this way in Rx 4.0.6, according to @mattpodwysocki ... but when I tried it it didn't work out that way.

@benlesh benlesh added the bug Confirmed bug label Jan 13, 2016
@benlesh benlesh self-assigned this Jan 13, 2016
@benlesh benlesh removed the bug Confirmed bug label Jan 13, 2016
@benlesh
Copy link
Member Author

benlesh commented Jan 13, 2016

Digging into this, I'm not sure this can be implemented reliably. This is because any synchronous call to next will happen before the subscriber function returns the teardown/unsubscribe logic:

const source = new Observable(observer => {
  // start some expensive async thing
  var id = asyncThing((x => observer.next(x));

  // synchronously emit a value (that will throw ultimately)
  observer.next('wee');

  // return teardown logic for expensive async thing
  return () => {
    cancelAsyncThing(id);
  };
});

source.subscribe((x) => {
  if (x === 'wee') {
    throw new Error('stop having fun!');
  }
});

In the above, cancelAsyncThing will never be called. It doesn't matter if we wrap the next handler in a try/catch and try to call unsubscribe, because the Observable's setup logic hasn't even completed yet to even return the unsubscribe logic.

There's basically no way to get this to work consistently unless all Observable implementations had a try/finally block.

I'm not sure what we should do for expected behavior here. But I think it should be consistent so as not to surprise developers.

cc/ @zenparsing @jhusain @headinthebox

@benlesh
Copy link
Member Author

benlesh commented Jan 13, 2016

I suppose we could squelch all next, error and complete calls after the error is thrown, to allow the subscriber logic to complete and return the teardown, then unsubscribe.

@trxcllnt
Copy link
Member

@Blesh resolved in #1191. It also resolves some of @mattpodwysocki and @zenparsing's concerns about the difference between using callbacks and a duck-typed Observer object.

@benlesh
Copy link
Member Author

benlesh commented Jan 15, 2016

@trxcllnt did you happen to test perf before and after? I suspect that adding several conditionals and a few function calls to each emission is going to impact performance.

@trxcllnt
Copy link
Member

@Blesh I ran node perf/micro mergemap-observable and saw no regressions. I haven't run the full micro/macro suite though.

@benlesh
Copy link
Member Author

benlesh commented Jan 15, 2016

This is tricky, because the ideal behavior is to halt execution of the subscription logic and teardown only what's been subscribed to at that point.

In effect, it would almost be better to have teardown logic added to subscriber via subscriber.add than it is to have to get to the end and return the teardown logic.

@benlesh benlesh added bug Confirmed bug PR: WIP labels Jan 15, 2016
@benlesh benlesh added this to the 5.0.0-beta.2 milestone Jan 15, 2016
benlesh pushed a commit that referenced this issue Jan 26, 2016
…-land handlers.

- slightly improves perf by improving shape of SafeSubscriber so JIT can optimize call patterns.

related #1186
@benlesh
Copy link
Member Author

benlesh commented Feb 8, 2016

This was resolved by #1191.

@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

No branches or pull requests

2 participants