question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

2.x: CreateEmitter throws when onError called after dispose unlike 1.x

See original GitHub issue

I have these two tests which behave differently in 1.x and 2.x.

@org.junit.Test
public void testRxJava1() throws Exception {
	Subscription subscription = rx.Observable.fromEmitter(emitter -> {
		emitter.onNext(1);
		emitter.setCancellation(() -> emitter.onError(new IllegalArgumentException()));
	}, Emitter.BackpressureMode.NONE)
			.subscribe(
				integer -> System.out.println(integer),
				throwable -> throwable.printStackTrace()
			);

	subscription.unsubscribe();
}

@org.junit.Test
public void testRxJava2() throws Exception {
	Disposable disposable = Observable.create(emitter -> {
		emitter.onNext(1);
		emitter.setCancellable(() -> emitter.onError(new IllegalArgumentException()));
	}).subscribe(
		integer -> System.out.println(integer),
		throwable -> throwable.printStackTrace()
	);

	disposable.dispose();
}

The 2.x CreateEmitter ends up throwing the exception as an uncaught exception, the 1.x Emitter just returns if the Subscription has already been unsubscribed.

The Wiki at Entering the Reactive World makes it sounds like both should behave the same.

What would be the correct usage in 2.x to get the same behaviour as in 1.x and not crashing the Application with the emitter.onError(...) call?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19 (13 by maintainers)

github_iconTop GitHub Comments

12reactions
akarnokdcommented, Aug 11, 2017

See FlowableEmitter.tryOnError and similar methods since 2.1.1 .

8reactions
Mauincommented, Nov 23, 2016

Working solution is to manually check if the emitter is not disposed before calling emitter.onError() to prevent the throw from happening. Mainly just wanted to point out the difference in Emitters between 1.x and 2.x even though the Wiki states they should be the same

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle dispose in RxJava without InterruptedException
One important design requirement for 2.x is that no Throwable errors should be swallowed. This means errors that can't be emitted because the ......
Read more >
RxJava2 UndeliverableException - ProAndroidDev
So in tryOnError() function try to call observer's onError() function if it is not disposed. On the other case it calls RxJavaPlugins.onError ......
Read more >
Observable (RxJava Javadoc 2.2.21) - ReactiveX
Unlike the Observable of version 1.x, subscribe(Observer) does not allow external disposal of a subscription and the Observer instance is expected to expose ......
Read more >
Using RxJava 2 - Tutorial - Vogella.com
Using RxJava 2 - Tutorial ... 1. Using reactive programming with RxJava 2.0 ... the onError() method is called on each subscriber.
Read more >
Master error handling in RxJava. Crush em! - Medium
The chain is disposed of before the Rx calls onError(). ... We see there are 2 ways in which we are throwing an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found