Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.x: Fix Observable.flatMap with maxConcurrency hangs (#6947) #6960

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -334,6 +334,7 @@ void drainLoop() {
if (checkTerminate()) {
return;
}
int innerCompleted = 0;
SimplePlainQueue<U> svq = queue;

if (svq != null) {
Expand All @@ -349,9 +350,18 @@ void drainLoop() {
}

child.onNext(o);
innerCompleted++;
}
}

if (innerCompleted != 0) {
if (maxConcurrency != Integer.MAX_VALUE) {
subscribeMore(innerCompleted);
innerCompleted = 0;
}
continue;
}

boolean d = done;
svq = queue;
InnerObserver<?, ?>[] inner = observers.get();
Expand All @@ -376,7 +386,6 @@ void drainLoop() {
return;
}

int innerCompleted = 0;
if (n != 0) {
long startId = lastId;
int index = lastIndex;
Expand Down Expand Up @@ -463,27 +472,33 @@ void drainLoop() {

if (innerCompleted != 0) {
if (maxConcurrency != Integer.MAX_VALUE) {
while (innerCompleted-- != 0) {
ObservableSource<? extends U> p;
synchronized (this) {
p = sources.poll();
if (p == null) {
wip--;
continue;
}
}
subscribeInner(p);
}
subscribeMore(innerCompleted);
innerCompleted = 0;
}
continue;
}

missed = addAndGet(-missed);
if (missed == 0) {
break;
}
}
}

void subscribeMore(int innerCompleted) {
while (innerCompleted-- != 0) {
ObservableSource<? extends U> p;
synchronized (this) {
p = sources.poll();
if (p == null) {
wip--;
continue;
}
}
subscribeInner(p);
}
}

boolean checkTerminate() {
if (cancelled) {
return true;
Expand Down
Expand Up @@ -1157,4 +1157,27 @@ public void innerErrorsMainCancelled() {

assertFalse("Has subscribers?", pp1.hasSubscribers());
}

@Test(timeout = 5000)
public void mixedScalarAsync() {
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
Flowable
.range(0, 20)
.flatMap(new Function<Integer, Publisher<?>>() {
@Override
public Publisher<?> apply(Integer integer) throws Exception {
if (integer % 5 != 0) {
return Flowable
.just(integer);
}

return Flowable
.just(-integer)
.observeOn(Schedulers.computation());
}
}, false, 1)
.ignoreElements()
.blockingAwait();
}
}
}
Expand Up @@ -1118,4 +1118,27 @@ public void innerErrorsMainCancelled() {

assertFalse("Has subscribers?", ps1.hasObservers());
}

@Test(timeout = 5000)
public void mixedScalarAsync() {
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
Observable
.range(0, 20)
.flatMap(new Function<Integer, ObservableSource<?>>() {
@Override
public ObservableSource<?> apply(Integer integer) throws Exception {
if (integer % 5 != 0) {
return Observable
.just(integer);
}

return Observable
.just(-integer)
.observeOn(Schedulers.computation());
}
}, false, 1)
.ignoreElements()
.blockingAwait();
}
}
}