Skip to content

Difference in behaviour for onError between 1.x AsyncEmitter and Observable.create() 2.x #5009

@hvisser

Description

@hvisser

RxJava version 2.0.4

While migrating to RxJava 2, I ran into some behavioural differences between 1.x fromAsync() and Observable.create(). In particular, the emitter passed into fromAsync handles onError() emissions after unsubscription differently from the 2.x one.

In 2.x, if onError() is invoked after the observable is disposed (e.g. emitter.isDisposed() returns true), the error is routed to the default onError() handler, resulting in an unhandled exception. This means that when emitting an error event, this should be guarded with a check to emitter.isDisposed(). onNext() and onCompleted events after disposal are discarded like before in 1.x

In 1.x this is not the case, and the emitter will just ignore the error emission if the observable has been unsubscribed.

2.x: https://github.com/ReactiveX/RxJava/blob/2.x/src/main/java/io/reactivex/internal/operators/observable/ObservableCreate.java#L72

1.x https://github.com/ReactiveX/RxJava/blob/1.x/src/main/java/rx/internal/operators/OnSubscribeFromEmitter.java#L105

Personally I prefer the 1.x behaviour because it's less error prone and doesn't require a guard using isDisposed(). Also because of this, migrating existing 1.x code to 2.x can be problematic. It took me a while to figure out why an error was thrown even though all of my subscriptions have explicit error handling.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions