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 concurrent clear() calls when fused chains are canceled #6677

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 @@ -262,7 +262,7 @@ public void cancel(K key) {
if (groupCount.decrementAndGet() == 0) {
upstream.cancel();

if (getAndIncrement() == 0) {
if (!outputFused && getAndIncrement() == 0) {
queue.clear();
}
}
Expand All @@ -288,7 +288,6 @@ void drainFused() {

for (;;) {
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
8 changes: 4 additions & 4 deletions src/main/java/io/reactivex/processors/UnicastProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ void drainFused(Subscriber<? super T> a) {
for (;;) {

if (cancelled) {
q.clear();
downstream.lazySet(null);
return;
}
Expand Down Expand Up @@ -550,10 +549,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
5 changes: 3 additions & 2 deletions src/main/java/io/reactivex/subjects/UnicastSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ void drainFused(Observer<? super T> a) {

if (disposed) {
downstream.lazySet(null);
q.clear();
return;
}
boolean d = done;
Expand Down Expand Up @@ -558,7 +557,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 @@ -14,6 +14,7 @@
package io.reactivex.internal.operators.flowable;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import java.io.IOException;
Expand All @@ -29,12 +30,13 @@
import com.google.common.cache.*;

import io.reactivex.*;
import io.reactivex.exceptions.TestException;
import io.reactivex.exceptions.*;
import io.reactivex.flowables.GroupedFlowable;
import io.reactivex.functions.*;
import io.reactivex.internal.functions.Functions;
import io.reactivex.internal.fuseable.QueueFuseable;
import io.reactivex.internal.fuseable.*;
import io.reactivex.internal.subscriptions.BooleanSubscription;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.processors.PublishProcessor;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
Expand Down Expand Up @@ -2205,4 +2207,83 @@ public void accept(Object object) {
}};
return evictingMapFactory;
}

@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,17 +15,22 @@

import static org.junit.Assert.*;

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

import org.junit.Test;
import org.reactivestreams.*;

import io.reactivex.Flowable;
import io.reactivex.*;
import io.reactivex.exceptions.*;
import io.reactivex.functions.*;
import io.reactivex.internal.functions.Functions;
import io.reactivex.internal.fuseable.QueueFuseable;
import io.reactivex.internal.subscriptions.BooleanSubscription;
import io.reactivex.observers.TestObserver;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.processors.PublishProcessor;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.*;

Expand Down Expand Up @@ -307,4 +312,37 @@ public void fusionRejected() {
SubscriberFusion.assertFusion(ts, 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();
}
}
}
}
41 changes: 39 additions & 2 deletions src/test/java/io/reactivex/processors/UnicastProcessorTest.java
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.*;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.TestException;
import io.reactivex.internal.fuseable.*;
import io.reactivex.exceptions.*;
import io.reactivex.internal.functions.Functions;
import io.reactivex.internal.fuseable.QueueFuseable;
import io.reactivex.internal.subscriptions.BooleanSubscription;
import io.reactivex.observers.TestObserver;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.*;

public class UnicastProcessorTest extends FlowableProcessorTest<Object> {
Expand Down Expand Up @@ -438,4 +442,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();
}
}
}
}
40 changes: 38 additions & 2 deletions src/test/java/io/reactivex/subjects/UnicastSubjectTest.java
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.*;
import io.reactivex.disposables.*;
import io.reactivex.exceptions.TestException;
import io.reactivex.internal.fuseable.*;
import io.reactivex.exceptions.*;
import io.reactivex.internal.functions.Functions;
import io.reactivex.internal.fuseable.QueueFuseable;
import io.reactivex.observers.*;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Schedulers;

public class UnicastSubjectTest extends SubjectTest<Integer> {

Expand Down Expand Up @@ -456,4 +459,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();
}
}
}
}