Skip to content

3.x: Fix delay-cancellation race producing random subsequent emissions #7855

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
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ final class OnNext implements Runnable {

@Override
public void run() {
downstream.onNext(t);
if (!w.isDisposed()) {
downstream.onNext(t);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ final class OnNext implements Runnable {

@Override
public void run() {
downstream.onNext(t);
if (!w.isDisposed()) {
downstream.onNext(t);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.LockSupport;

import org.junit.*;
import org.mockito.InOrder;
Expand All @@ -28,6 +29,7 @@
import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.exceptions.TestException;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.disposables.SequentialDisposable;
import io.reactivex.rxjava3.internal.functions.Functions;
import io.reactivex.rxjava3.processors.PublishProcessor;
import io.reactivex.rxjava3.schedulers.*;
Expand Down Expand Up @@ -1030,4 +1032,38 @@ public Publisher<Object> apply(Integer t) throws Exception {
.to(TestHelper.<Integer>testConsumer())
.assertFailureAndMessage(NullPointerException.class, "The itemDelay returned a null Publisher");
}

@Test
public void cancelShouldPreventRandomSubsequentEmissions() {
for (int attempt = 1; attempt < 100; attempt ++) {

SequentialDisposable disposable = new SequentialDisposable();
ConcurrentLinkedQueue<Integer> sink = new ConcurrentLinkedQueue<>();

disposable.replace(
Flowable.range(1, 10)
.delay(1, TimeUnit.MICROSECONDS, Schedulers.computation(), true)
.doOnNext(v -> {
if (v == 1) {
Schedulers.computation().scheduleDirect(disposable::dispose);
}
sink.offer(v);
})
.subscribe());

LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(1));

Integer last = null;

while (!sink.isEmpty()) {
Integer current = sink.poll();

if (last != null && last + 1 != current) {
fail("Emission hole: " + last + " -> " + current);
}

last = current;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;

import org.junit.*;
import org.mockito.InOrder;
Expand All @@ -29,6 +30,7 @@
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.exceptions.TestException;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.disposables.SequentialDisposable;
import io.reactivex.rxjava3.internal.functions.Functions;
import io.reactivex.rxjava3.observers.*;
import io.reactivex.rxjava3.schedulers.*;
Expand Down Expand Up @@ -978,4 +980,37 @@ public Observable<Object> apply(Integer t) throws Exception {
.to(TestHelper.<Integer>testConsumer())
.assertFailureAndMessage(NullPointerException.class, "The itemDelay returned a null ObservableSource");
}
}

@Test
public void cancelShouldPreventRandomSubsequentEmissions() {
for (int attempt = 1; attempt < 100; attempt ++) {

SequentialDisposable disposable = new SequentialDisposable();
ConcurrentLinkedQueue<Integer> sink = new ConcurrentLinkedQueue<>();

disposable.replace(
Observable.range(1, 10)
.delay(1, TimeUnit.MICROSECONDS, Schedulers.computation(), true)
.doOnNext(v -> {
if (v == 1) {
Schedulers.computation().scheduleDirect(disposable::dispose);
}
sink.offer(v);
})
.subscribe());

LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(1));

Integer last = null;

while (!sink.isEmpty()) {
Integer current = sink.poll();

if (last != null && last + 1 != current) {
fail("Emission hole: " + last + " -> " + current);
}

last = current;
}
}
}}
Loading