RxJava icon indicating copy to clipboard operation
RxJava copied to clipboard

Flowable GroupBy operator never completes

Open daniil-dubin opened this issue 9 years ago • 5 comments

On RxJava version 2.1.6 JRE Oracle 1.8.111 This sample dummy code never completes.

Flowable.range(1, 132).groupBy(z -> z % 3).subscribe(z -> {}, err -> {}, () -> System.out.println("completed"));

Numbers 132 and divisor 3 are essential, although issue is reproducible with other combinations like f.e. 139 and 10.

Conversely, with Observable everything works fine Observable.range(1, 132) .groupBy(z -> z % 3) .subscribe(z -> {}, err -> {}, () -> System.out.println("completed"));

daniil-dubin avatar Nov 27 '17 10:11 daniil-dubin

You have to consume the GroupedFlowable groups in order to have more items delivered due to backpressure coordination by groupBy.

akarnokd avatar Nov 27 '17 10:11 akarnokd

Thanks for your quick response. With addition of consumption of GroupedFlowable this now completes fine:

            Flowable.range(1, 394)
                    .groupBy(z -> z % 3)
                    .flatMap(groups -> groups)
                    .blockingSubscribe();

But simply adding useless but quite legitimate .buffer(2).flatMap(batch -> Flowable.fromIterable(batch)) still causes hang.

            Flowable.range(1, 394)
                    .groupBy(z -> z % 3)
                    .buffer(2).flatMap(batch -> Flowable.fromIterable(batch))
                    .flatMap(groups -> groups)
                    .blockingSubscribe();

That bizarre code while looking useless is a simplified version of one that processes events in batches in real application.

daniil-dubin avatar Nov 27 '17 11:11 daniil-dubin

Adding buffer after groupBy prevents the immediate consumption of the groups by flatMap.

akarnokd avatar Nov 27 '17 11:11 akarnokd

So should it be considered like a bug? For me it's obviously a problem when via quite legitimate usage of API (I believe that in the code above there was nothing prohibited done, correct me if I am wrong) it's possible to break the system under some circumstances. There should be at least some red flags in documentation discouraging misuse. In my particular case everything has been working fine for weeks until the number of emitted buffered events reached some magic value. That's upsetting. If it's a bug should I dive into the rx-java internals to try to somehow fix it?

daniil-dubin avatar Nov 27 '17 12:11 daniil-dubin

So should it be considered like a bug?

It is a property of how the fan-out logic interacts with backpressure where the number of groups can't be anticipated by the operator. An emission from upstream could go into an existing group or create a new group with that single initial value. Since group creation has to honor backpressure any delay to bringing that group to a consumer may result in the sequence to hang.

I believe that in the code above there was nothing prohibited done, correct me if I am wrong

Why would you buffer groups in the first place?

There should be at least some red flags in documentation discouraging misuse.

You can formulate this warning and add them to the Flowable.groupBy overloads' JavaDoc via PR if you want.

If it's a bug should I dive into the rx-java internals to try to somehow fix it?

This property of groupBy has been there for a long time now and nobody so far could come up with a reasonable solution that also doesn't break the Reactive-Streams spec. You can have a stab at it if you want though.

One option would be to have groupBy use the on-backpressure-error policy which then will prompt the developer about the need to consume groups. However, this is a drastic behavior change and as such can only happen in a subsequent major release such as 3.x.

akarnokd avatar Nov 27 '17 13:11 akarnokd