Skip to content

3.x: Fix ReplaySubject termination-subscription race emitting wrongly #7879

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

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
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
81 changes: 33 additions & 48 deletions src/main/java/io/reactivex/rxjava3/subjects/ReplaySubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,6 @@ static final class UnboundedReplayBuffer<T>

final List<Object> buffer;

volatile boolean done;

volatile int size;

UnboundedReplayBuffer(int capacityHint) {
Expand All @@ -671,7 +669,6 @@ public void addFinal(Object notificationLite) {
buffer.add(notificationLite);
trimHead();
size++;
done = true;
}

@Override
Expand Down Expand Up @@ -772,20 +769,17 @@ public void replay(ReplayDisposable<T> rs) {

Object o = b.get(index);

if (done) {
if (index + 1 == s) {
s = size;
if (index + 1 == s) {
if (NotificationLite.isComplete(o)) {
a.onComplete();
} else {
a.onError(NotificationLite.getError(o));
}
rs.index = null;
rs.cancelled = true;
return;
}
}
if (NotificationLite.isComplete(o)) {
a.onComplete();
rs.index = null;
rs.cancelled = true;
return;
} else
if (NotificationLite.isError(o)) {
a.onError(NotificationLite.getError(o));
rs.index = null;
rs.cancelled = true;
return;
}

a.onNext((T)o);
Expand Down Expand Up @@ -856,8 +850,6 @@ static final class SizeBoundReplayBuffer<T>

Node<Object> tail;

volatile boolean done;

SizeBoundReplayBuffer(int maxSize) {
this.maxSize = maxSize;
Node<Object> h = new Node<>(null);
Expand Down Expand Up @@ -895,7 +887,6 @@ public void addFinal(Object notificationLite) {
t.lazySet(n); // releases both the tail and size

trimHead();
done = true;
}

/**
Expand Down Expand Up @@ -1000,18 +991,17 @@ public void replay(ReplayDisposable<T> rs) {

Object o = n.value;

if (done) {
if (n.get() == null) {

if (NotificationLite.isComplete(o)) {
a.onComplete();
} else {
a.onError(NotificationLite.getError(o));
}
rs.index = null;
rs.cancelled = true;
return;
}
if (NotificationLite.isComplete(o)) {
a.onComplete();
rs.index = null;
rs.cancelled = true;
return;
} else
if (NotificationLite.isError(o)) {
a.onError(NotificationLite.getError(o));
rs.index = null;
rs.cancelled = true;
return;
}

a.onNext((T)o);
Expand Down Expand Up @@ -1069,8 +1059,6 @@ static final class SizeAndTimeBoundReplayBuffer<T>

TimedNode<Object> tail;

volatile boolean done;

SizeAndTimeBoundReplayBuffer(int maxSize, long maxAge, TimeUnit unit, Scheduler scheduler) {
this.maxSize = maxSize;
this.maxAge = maxAge;
Expand Down Expand Up @@ -1163,8 +1151,6 @@ public void addFinal(Object notificationLite) {
size++;
t.lazySet(n); // releases both the tail and size
trimFinal();

done = true;
}

/**
Expand Down Expand Up @@ -1290,18 +1276,17 @@ public void replay(ReplayDisposable<T> rs) {

Object o = n.value;

if (done) {
if (n.get() == null) {

if (NotificationLite.isComplete(o)) {
a.onComplete();
} else {
a.onError(NotificationLite.getError(o));
}
rs.index = null;
rs.cancelled = true;
return;
}
if (NotificationLite.isComplete(o)) {
a.onComplete();
rs.index = null;
rs.cancelled = true;
return;
} else
if (NotificationLite.isError(o)) {
a.onError(NotificationLite.getError(o));
rs.index = null;
rs.cancelled = true;
return;
}

a.onNext((T)o);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ public void disposed() {

@Test
public void manySources() {
Flowable<?>[] a = new Flowable[32];
@SuppressWarnings("unchecked")
Flowable<Object>[] a = new Flowable[32];
Arrays.fill(a, Flowable.never());
a[31] = Flowable.just(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ public void ambArraySingleElement() {

@Test
public void manySources() {
Observable<?>[] a = new Observable[32];
@SuppressWarnings("unchecked")
Observable<Object>[] a = new Observable[32];
Arrays.fill(a, Observable.never());
a[31] = Observable.just(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ public void run() {

@Test
public void manySources() {
Single<?>[] sources = new Single[32];
@SuppressWarnings("unchecked")
Single<Object>[] sources = new Single[32];
Arrays.fill(sources, Single.never());
sources[31] = Single.just(31);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1832,4 +1832,69 @@ public void timeAndSizeRemoveCorrectNumberOfOld() {

rp.test().assertValuesOnly(4, 5);
}
}

@Test
public void terminationSubscriptionRaceUnbounded() throws Throwable {
for (int i = 1; i <= 10000; i++) {
ReplayProcessor<String> source = ReplayProcessor.create();
PublishProcessor<String> sink = PublishProcessor.create();
TestSubscriber<String> subscriber = sink.test();
Schedulers.computation().scheduleDirect(() -> {
// issue signals to the source in adherence to the reactive streams specification
source.onSubscribe(new BooleanSubscription());
source.onNext("hello");
source.onNext("world");
source.onComplete();
});
Schedulers.computation().scheduleDirect(() -> {
// connect the source to the sink in parallel with the signals issued to the source
// note the cast() operator, which is here to detect non-String escapees
source.cast(String.class).subscribe(sink);
});
subscriber.await().assertValues("hello", "world").assertComplete();
}
}

@Test
public void terminationSubscriptionRaceSizeBound() throws Throwable {
for (int i = 1; i <= 10000; i++) {
ReplayProcessor<String> source = ReplayProcessor.createWithSize(20);
PublishProcessor<String> sink = PublishProcessor.create();
TestSubscriber<String> subscriber = sink.test();
Schedulers.computation().scheduleDirect(() -> {
// issue signals to the source in adherence to the reactive streams specification
source.onSubscribe(new BooleanSubscription());
source.onNext("hello");
source.onNext("world");
source.onComplete();
});
Schedulers.computation().scheduleDirect(() -> {
// connect the source to the sink in parallel with the signals issued to the source
// note the cast() operator, which is here to detect non-String escapees
source.cast(String.class).subscribe(sink);
});
subscriber.await().assertValues("hello", "world").assertComplete();
}
}

@Test
public void terminationSubscriptionRaceTimeBound() throws Throwable {
for (int i = 1; i <= 10000; i++) {
ReplayProcessor<String> source = ReplayProcessor.createWithTime(20, TimeUnit.MINUTES, Schedulers.computation());
PublishProcessor<String> sink = PublishProcessor.create();
TestSubscriber<String> subscriber = sink.test();
Schedulers.computation().scheduleDirect(() -> {
// issue signals to the source in adherence to the reactive streams specification
source.onSubscribe(new BooleanSubscription());
source.onNext("hello");
source.onNext("world");
source.onComplete();
});
Schedulers.computation().scheduleDirect(() -> {
// connect the source to the sink in parallel with the signals issued to the source
// note the cast() operator, which is here to detect non-String escapees
source.cast(String.class).subscribe(sink);
});
subscriber.await().assertValues("hello", "world").assertComplete();
}
}}
66 changes: 66 additions & 0 deletions src/test/java/io/reactivex/rxjava3/subjects/ReplaySubjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1378,4 +1378,70 @@ public void timeAndSizeRemoveCorrectNumberOfOld() {

rs.test().assertValuesOnly(4, 5);
}

@Test
public void terminationSubscriptionRaceUnbounded() throws Throwable {
for (int i = 1; i <= 10000; i++) {
Subject<String> source = ReplaySubject.create();
Subject<String> sink = PublishSubject.create();
TestObserver<String> observer = sink.test();
Schedulers.computation().scheduleDirect(() -> {
// issue signals to the source in adherence to the reactive streams specification
source.onSubscribe(Disposable.empty());
source.onNext("hello");
source.onNext("world");
source.onComplete();
});
Schedulers.computation().scheduleDirect(() -> {
// connect the source to the sink in parallel with the signals issued to the source
// note the cast() operator, which is here to detect non-String escapees
source.cast(String.class).subscribe(sink);
});
observer.await().assertValues("hello", "world").assertComplete();
}
}

@Test
public void terminationSubscriptionRaceSizeBound() throws Throwable {
for (int i = 1; i <= 10000; i++) {
Subject<String> source = ReplaySubject.createWithSize(20);
Subject<String> sink = PublishSubject.create();
TestObserver<String> observer = sink.test();
Schedulers.computation().scheduleDirect(() -> {
// issue signals to the source in adherence to the reactive streams specification
source.onSubscribe(Disposable.empty());
source.onNext("hello");
source.onNext("world");
source.onComplete();
});
Schedulers.computation().scheduleDirect(() -> {
// connect the source to the sink in parallel with the signals issued to the source
// note the cast() operator, which is here to detect non-String escapees
source.cast(String.class).subscribe(sink);
});
observer.await().assertValues("hello", "world").assertComplete();
}
}

@Test
public void terminationSubscriptionRaceTimeBound() throws Throwable {
for (int i = 1; i <= 10000; i++) {
Subject<String> source = ReplaySubject.createWithTime(20, TimeUnit.MINUTES, Schedulers.computation());
Subject<String> sink = PublishSubject.create();
TestObserver<String> observer = sink.test();
Schedulers.computation().scheduleDirect(() -> {
// issue signals to the source in adherence to the reactive streams specification
source.onSubscribe(Disposable.empty());
source.onNext("hello");
source.onNext("world");
source.onComplete();
});
Schedulers.computation().scheduleDirect(() -> {
// connect the source to the sink in parallel with the signals issued to the source
// note the cast() operator, which is here to detect non-String escapees
source.cast(String.class).subscribe(sink);
});
observer.await().assertValues("hello", "world").assertComplete();
}
}
}
Loading