Skip to content

Subscriber.onStart is called twice on nested unsafeSubscription #2979

Closed
@ibaca

Description

@ibaca

This test shows the onStart called twice problem. It is a clone of SubscriberTest.testOnStartCalledOnceViaUnsafeSubscribe but adding the defer operator which calls unsafeSubscribe resulting in a nested call to unsafeSubscribe, the one from the test and the second one from OnSubscribeDefer.

@Test
public void testOnStartCalledOnceViaNestedUnsafeSubscribe() {
    final AtomicInteger c = new AtomicInteger();
    Observable.defer(new Func0<Observable<Integer>>() {
        @Override public Observable<Integer> call() {
            return Observable.just(1, 2, 3, 4).take(2);
        }
    }).unsafeSubscribe(new Subscriber<Integer>() {
        @Override public void onStart() {
            c.incrementAndGet();
            request(1);
        }

        @Override public void onCompleted() { }

        @Override public void onError(Throwable e) { }

        @Override public void onNext(Integer t) {
            request(1);
        }
    });

    assertEquals(1, c.get());
}

Two proposed solutions (first one looks ugly, second one adds more code to Subscriber)

  1. Add a parameter to subscription calls to transfer the 'onStart called' state.
  2. Modify Subscriber so .subscribe() .unsafeSubscribe() calls Subscriber.start() which delegates to the actual .onStart() protecting for duplicate calls.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions