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.

Exceptions thrown in several threads simultaneously cause the application to crash when using ParallelFlowable

See original GitHub issue

In Android application I’m trying to perform a simple parallel task using ParallelFlowable from RxJava 2.0.6.

public void doParallelAsyncJob() {
    subscription = Flowable
            .fromArray("1", "2", "3", "4", "5", "6", "7", "8")
            .parallel()
            .runOn(Schedulers.computation())
            .map(Integer::parseInt)
            .sequential()
            .toList()
            .subscribeOn(Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    result -> showSuccessDialog(),
                    error -> showErrorDialog()
            );
}

As you can see, the code takes several strings and tries to parse them as integers. In this case all numbers are processed successfully, so the success dialog is shown. If I use the following sequence:

.fromArray("wrong", "2", "3", "4", "5", "6", "7", "8")

Then the error dialog appears, because one of the items cannot be parsed as number. This is a correct behavior. However, if I add two incorrect items instead of one:

.fromArray("wrong", "wrong", "3", "4", "5", "6", "7", "8")

Then the application just crashes. It seems to me that the reason is because the exception happened in two threads at once, but I may be wrong in the conclusion.

When I implement the parallelization the old way, the application doesn’t crash:

public void doParallelAsyncJobOldWay() {
    subscription = Flowable
            .fromArray("wrong", "wrong", "3", "4", "5", "6", "7", "8")
            .flatMap(number ->
                    Flowable
                            .just(Integer.parseInt(number))
                            .subscribeOn(Schedulers.computation())
            )
            .toList()
            .subscribeOn(Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    result -> showSuccessDialog(),
                    error -> showErrorDialog()
            );
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
romanzescommented, Feb 21, 2017

@akarnokd Nice explanation, thanks! I tried using DisposableSubscriber for cancelling the “whole setup” and it seems to be working well.

1reaction
akarnokdcommented, Feb 21, 2017

Thanks for reporting. There is a fix waiting for review/feedback about the same issue: #5117 (reported in #5108).

As for the doParallelAsyncJobOldWay, it doesn’t crash the app but also doesn’t process “3” … “8” because Integer.parseInt crashes the function of the flatMap before it could create an async source for the rest of the input numbers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does a single thread exception crash entire program ...
The exception handler specified by lpTopLevelExceptionFilter is executed in the context of the thread that caused the fault.
Read more >
How to: Handle Exceptions in Parallel Loops | Microsoft Learn
See an example of how to wrap all exceptions from the loop in a System. ... exceptions might be thrown on multiple threads...
Read more >
Merging RxJava Observables considered harmful — Part 2
This error handler will catch every exception that is thrown by a stream ... crash if more than one streams emit an error...
Read more >
Flowable (RxJava Javadoc 3.1.5) - ReactiveX
The Flowable class that implements the Reactive Streams Publisher Pattern and offers factory methods, intermediate operators and the ability to consume ...
Read more >
How to Handle Died Threads due to Uncaught Exceptions in ...
In concurrent applications a thread might fail and die due to uncaught runtime ... submitted with submit() to the executor service, any thrown...
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