Skip to content

Commit baad331

Browse files
authored
3.x: Fix Single.timeout race condition (#7515)
1 parent e1b6cb4 commit baad331

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleTimeout.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ public void onError(Throwable e) {
113113

114114
@Override
115115
public void run() {
116-
Disposable d = get();
117-
if (d != DisposableHelper.DISPOSED && compareAndSet(d, DisposableHelper.DISPOSED)) {
118-
if (d != null) {
119-
d.dispose();
120-
}
116+
if (DisposableHelper.dispose(this)) {
121117
SingleSource<? extends T> other = this.other;
122118
if (other == null) {
123119
downstream.onError(new TimeoutException(timeoutMessage(timeout, unit)));

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleTimeoutTest.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import io.reactivex.rxjava3.functions.Action;
2929
import io.reactivex.rxjava3.observers.TestObserver;
3030
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
31-
import io.reactivex.rxjava3.schedulers.TestScheduler;
31+
import io.reactivex.rxjava3.schedulers.*;
3232
import io.reactivex.rxjava3.subjects.*;
3333
import io.reactivex.rxjava3.testsupport.TestHelper;
3434

@@ -255,4 +255,24 @@ protected void subscribeActual(@NonNull SingleObserver<? super @NonNull Integer>
255255

256256
assertTrue(d.isDisposed());
257257
}
258+
259+
@Test
260+
public void timeoutWithZero() throws InterruptedException {
261+
int n = 10_000;
262+
Scheduler sch = Schedulers.single();
263+
for (int i = 0; i < n; i++) {
264+
final int y = i;
265+
final CountDownLatch latch = new CountDownLatch(1);
266+
Disposable d = Single.never()
267+
.timeout(0, TimeUnit.NANOSECONDS, sch)
268+
.subscribe(v -> {}, e -> {
269+
//System.out.println("timeout " + y);
270+
latch.countDown();
271+
});
272+
if (!latch.await(2, TimeUnit.SECONDS)) {
273+
System.out.println(d + " " + sch);
274+
throw new IllegalStateException("Timeout did not work at y = " + y);
275+
}
276+
}
277+
}
258278
}

0 commit comments

Comments
 (0)