From aeb033549a7e6c69cd36fe3f086879ff456ff359 Mon Sep 17 00:00:00 2001 From: Zain Qureshi <43629888+ZainnQureshii@users.noreply.github.com> Date: Sat, 28 Nov 2026 19:22:05 +0100 Subject: [PATCH] 4.x: Fix Streamable.takeUntil missing stop signals with synchronous sources Co-Authored-By: Claude Opus 5.5 --- .../streamable/StreamableTakeUntil.java | 19 +++++---- .../streamable/StreamableTakeUntilTest.java | 42 +++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableTakeUntil.java b/src/main/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableTakeUntil.java index f61ee9bf4f..435d1df169 100644 --- a/src/main/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableTakeUntil.java +++ b/src/main/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableTakeUntil.java @@ -18,7 +18,7 @@ import static io.reactivex.rxjava4.internal.operators.streamable.StreamableHelper.suppressValueAndCancel; import static io.reactivex.rxjava4.internal.operators.streamable.StreamableHelper.whenBoth; -import java.util.concurrent.CompletionStage; +import java.util.concurrent.*; import io.reactivex.rxjava4.annotations.NonNull; import io.reactivex.rxjava4.core.Streamable; @@ -57,16 +57,16 @@ static final class TakeUntilMainStreamer implements Streamer { final DisposableStreamerCancellation otherCancellation; - final CompletionStage otherNext; + final CompletableFuture otherNext; - CompletionStage mainNext; + CompletionStage mainNext = NEXT_FALSE; public TakeUntilMainStreamer( Streamer upstream, Streamer otherStreamer, DisposableStreamerCancellation mainCancellation, DisposableStreamerCancellation otherCancellation, - CompletionStage otherNext + CompletableFuture otherNext ) { this.upstream = upstream; this.otherStreamer = otherStreamer; @@ -77,12 +77,17 @@ public TakeUntilMainStreamer( @Override public @NonNull CompletionStage next() { - mainNext = suppressCancel(upstream.next(), mainCancellation, false); - return StreamableHelper.race(mainNext, otherNext, w -> { - if (w == 2) { + var mainResult = new CompletableFuture(); + var result = StreamableHelper.race(otherNext, mainResult, w -> { + if (w == 1) { mainCancellation.dispose(); } }); + if (!otherNext.isDone()) { + mainNext = suppressCancel(upstream.next(), mainCancellation, false); + StreamableHelper.forward(mainNext, mainResult); + } + return result; } @Override diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableTakeUntilTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableTakeUntilTest.java index bf97c8aa80..6fcc35c134 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableTakeUntilTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableTakeUntilTest.java @@ -13,13 +13,18 @@ package io.reactivex.rxjava4.internal.operators.streamable; +import static org.junit.jupiter.api.Assertions.*; + import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.*; import org.junit.jupiter.api.Test; import io.reactivex.rxjava4.core.Streamable; +import io.reactivex.rxjava4.disposables.Disposable; import io.reactivex.rxjava4.exceptions.TestException; import io.reactivex.rxjava4.schedulers.Schedulers; +import io.reactivex.rxjava4.schedulers.TestScheduler; public class StreamableTakeUntilTest extends StreamableBaseTest { @@ -59,6 +64,43 @@ public void empty() throws Throwable { .assertResult(); } + @Test + public void emptyOtherBeforeSynchronousMain() throws Throwable { + var reads = new AtomicInteger(); + + Streamable.range(1, 5) + .doOnNext(_ -> reads.incrementAndGet()) + .takeUntil(Streamable.empty()) + .test() + .awaitDone(5, TimeUnit.SECONDS) + .assertResult(); + + assertEquals(0, reads.get()); + } + + @Test + public void timerFiresDuringSynchronousPull() throws Throwable { + var scheduler = new TestScheduler(); + var cancelled = Disposable.empty(); + var cancelledDuringPull = new AtomicBoolean(); + + Streamable.range(1, 5) + .withCancellation(cancellation -> { + cancellation.add(cancelled); + return cancellation; + }) + .doOnNext(_ -> { + scheduler.advanceTimeBy(1, TimeUnit.SECONDS); + cancelledDuringPull.set(cancelled.isDisposed()); + }) + .takeUntil(Streamable.timer(1, TimeUnit.SECONDS, scheduler)) + .test() + .awaitDone(5, TimeUnit.SECONDS) + .assertResult(); + + assertTrue(cancelledDuringPull.get()); + } + @Test public void otherError() throws Throwable { Streamable.never()