Skip to content

3.x: Fix concurrent clear() calls when fused chains are canceled #6676

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

Merged
merged 1 commit into from
Oct 17, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void cancel(K key) {
if (groupCount.decrementAndGet() == 0) {
upstream.cancel();

if (getAndIncrement() == 0) {
if (!outputFused && getAndIncrement() == 0) {
queue.clear();
}
}
Expand Down Expand Up @@ -601,7 +601,6 @@ void drainFused() {
for (;;) {
if (a != null) {
if (cancelled.get()) {
q.clear();
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void cancel() {
cancelled = true;
upstream.cancel();

if (getAndIncrement() == 0) {
if (!outputFused && getAndIncrement() == 0) {
queue.clear();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ void drainFused(Subscriber<? super T> a) {
for (;;) {

if (cancelled) {
q.clear();
downstream.lazySet(null);
return;
}
Expand Down Expand Up @@ -548,10 +547,11 @@ public void cancel() {

doTerminate();

if (!enableOperatorFusion) {
if (wip.getAndIncrement() == 0) {
downstream.lazySet(null);
if (wip.getAndIncrement() == 0) {
downstream.lazySet(null);
if (!enableOperatorFusion) {
queue.clear();
downstream.lazySet(null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ void drainFused(Observer<? super T> a) {

if (disposed) {
downstream.lazySet(null);
q.clear();
return;
}
boolean d = done;
Expand Down Expand Up @@ -556,7 +555,9 @@ public void dispose() {
downstream.lazySet(null);
if (wip.getAndIncrement() == 0) {
downstream.lazySet(null);
queue.clear();
if (!enableOperatorFusion) {
queue.clear();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
import com.google.common.cache.*;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.exceptions.TestException;
import io.reactivex.rxjava3.exceptions.*;
import io.reactivex.rxjava3.flowables.GroupedFlowable;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.functions.Functions;
import io.reactivex.rxjava3.internal.fuseable.QueueFuseable;
import io.reactivex.rxjava3.internal.fuseable.*;
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.processors.PublishProcessor;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.subjects.PublishSubject;
Expand Down Expand Up @@ -2332,4 +2333,83 @@ public void accept(GroupedFlowable<Integer, Object> g) throws Throwable {

ts2.assertFailure(TestException.class, 1);
}

@Test
public void fusedNoConcurrentCleanDueToCancel() {
for (int j = 0; j < TestHelper.RACE_LONG_LOOPS; j++) {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final PublishProcessor<Integer> pp = PublishProcessor.create();

final AtomicReference<QueueSubscription<GroupedFlowable<Object, Integer>>> qs = new AtomicReference<QueueSubscription<GroupedFlowable<Object, Integer>>>();

final TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>();

pp.groupBy(Functions.identity(), Functions.<Integer>identity(), false, 4)
.subscribe(new FlowableSubscriber<GroupedFlowable<Object, Integer>>() {

boolean once;

@Override
public void onNext(GroupedFlowable<Object, Integer> g) {
if (!once) {
try {
GroupedFlowable<Object, Integer> t = qs.get().poll();
if (t != null) {
once = true;
t.subscribe(ts2);
}
} catch (Throwable ignored) {
// not relevant here
}
}
}

@Override
public void onError(Throwable t) {
}

@Override
public void onComplete() {
}

@Override
public void onSubscribe(Subscription s) {
@SuppressWarnings("unchecked")
QueueSubscription<GroupedFlowable<Object, Integer>> q = (QueueSubscription<GroupedFlowable<Object, Integer>>)s;
qs.set(q);
q.requestFusion(QueueFuseable.ANY);
q.request(1);
}
})
;

Runnable r1 = new Runnable() {
@Override
public void run() {
qs.get().cancel();
qs.get().clear();
}
};
Runnable r2 = new Runnable() {
@Override
public void run() {
ts2.cancel();
}
};

for (int i = 0; i < 100; i++) {
pp.onNext(i);
}

TestHelper.race(r1, r2);

if (!errors.isEmpty()) {
throw new CompositeException(errors);
}
} finally {
RxJavaPlugins.reset();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static org.junit.Assert.*;

import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -24,11 +25,15 @@
import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.exceptions.*;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.functions.Functions;
import io.reactivex.rxjava3.internal.fuseable.QueueFuseable;
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
import io.reactivex.rxjava3.observers.TestObserver;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.processors.PublishProcessor;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.subscribers.*;
import io.reactivex.rxjava3.testsupport.TestSubscriberEx;
import io.reactivex.rxjava3.testsupport.*;

public class FlowableOnBackpressureBufferTest extends RxJavaTest {

Expand Down Expand Up @@ -308,4 +313,37 @@ public void fusionRejected() {
ts.assertFusionMode(QueueFuseable.NONE)
.assertEmpty();
}

@Test
public void fusedNoConcurrentCleanDueToCancel() {
for (int j = 0; j < TestHelper.RACE_LONG_LOOPS; j++) {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final PublishProcessor<Integer> pp = PublishProcessor.create();

TestObserver<Integer> to = pp.onBackpressureBuffer(4, false, true)
.observeOn(Schedulers.io())
.map(Functions.<Integer>identity())
.observeOn(Schedulers.single())
.firstOrError()
.test();

for (int i = 0; pp.hasSubscribers(); i++) {
pp.onNext(i);
}

to
.awaitDone(5, TimeUnit.SECONDS)
;

if (!errors.isEmpty()) {
throw new CompositeException(errors);
}

to.assertResult(0);
} finally {
RxJavaPlugins.reset();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@
import static org.junit.Assert.*;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.Test;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.TestException;
import io.reactivex.rxjava3.exceptions.*;
import io.reactivex.rxjava3.internal.functions.Functions;
import io.reactivex.rxjava3.internal.fuseable.QueueFuseable;
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
import io.reactivex.rxjava3.observers.TestObserver;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.subscribers.TestSubscriber;
import io.reactivex.rxjava3.testsupport.*;

Expand Down Expand Up @@ -439,4 +443,37 @@ public void unicastSubscriptionBadRequest() {
RxJavaPlugins.reset();
}
}

@Test
public void fusedNoConcurrentCleanDueToCancel() {
for (int j = 0; j < TestHelper.RACE_LONG_LOOPS; j++) {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final UnicastProcessor<Integer> us = UnicastProcessor.create();

TestObserver<Integer> to = us
.observeOn(Schedulers.io())
.map(Functions.<Integer>identity())
.observeOn(Schedulers.single())
.firstOrError()
.test();

for (int i = 0; us.hasSubscribers(); i++) {
us.onNext(i);
}

to
.awaitDone(5, TimeUnit.SECONDS)
;

if (!errors.isEmpty()) {
throw new CompositeException(errors);
}

to.assertResult(0);
} finally {
RxJavaPlugins.reset();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
import static org.mockito.Mockito.mock;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.Test;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.*;
import io.reactivex.rxjava3.exceptions.TestException;
import io.reactivex.rxjava3.exceptions.*;
import io.reactivex.rxjava3.internal.functions.Functions;
import io.reactivex.rxjava3.internal.fuseable.QueueFuseable;
import io.reactivex.rxjava3.observers.TestObserver;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.testsupport.*;

public class UnicastSubjectTest extends SubjectTest<Integer> {
Expand Down Expand Up @@ -457,4 +460,37 @@ public void drainFusedFailFastEmpty() {

to.assertEmpty();
}

@Test
public void fusedNoConcurrentCleanDueToCancel() {
for (int j = 0; j < TestHelper.RACE_LONG_LOOPS; j++) {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final UnicastSubject<Integer> us = UnicastSubject.create();

TestObserver<Integer> to = us
.observeOn(Schedulers.io())
.map(Functions.<Integer>identity())
.observeOn(Schedulers.single())
.firstOrError()
.test();

for (int i = 0; us.hasObservers(); i++) {
us.onNext(i);
}

to
.awaitDone(5, TimeUnit.SECONDS)
;

if (!errors.isEmpty()) {
throw new CompositeException(errors);
}

to.assertResult(0);
} finally {
RxJavaPlugins.reset();
}
}
}
}