Exceptions thrown in several threads simultaneously cause the application to crash when using ParallelFlowable
See original GitHub issueIn 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:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@akarnokd Nice explanation, thanks! I tried using DisposableSubscriber for cancelling the “whole setup” and it seems to be working well.
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” becauseInteger.parseInt
crashes the function of theflatMap
before it could create an async source for the rest of the inputnumber
s.