From cf68c1ed021be590c3a9693723bbfe366329830a Mon Sep 17 00:00:00 2001 From: Vladimir Zhilin Date: Wed, 25 Nov 2026 15:50:27 +0100 Subject: [PATCH] 4.x: Fix SubscriptionArbiter installing a subscription after cancel() setSubscription checked `cancelled` only before taking the WIP slot. A cancel() whose whole drain ran in between cancelled the previous subscription only; the new one was then installed, requested from and never cancelled. Re-check `cancelled` after winning the slot. Resolves #8404 Co-Authored-By: Claude Opus 5.5 --- .../subscriptions/SubscriptionArbiter.java | 9 ++++ .../SubscriptionArbiterIsolatedTest.java | 42 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/test/java/io/reactivex/rxjava4/internal/subscriptions/SubscriptionArbiterIsolatedTest.java diff --git a/src/main/java/io/reactivex/rxjava4/internal/subscriptions/SubscriptionArbiter.java b/src/main/java/io/reactivex/rxjava4/internal/subscriptions/SubscriptionArbiter.java index aae98ba294a..5b3938c0d32 100644 --- a/src/main/java/io/reactivex/rxjava4/internal/subscriptions/SubscriptionArbiter.java +++ b/src/main/java/io/reactivex/rxjava4/internal/subscriptions/SubscriptionArbiter.java @@ -71,6 +71,15 @@ public final void setSubscription(Subscription s) { Objects.requireNonNull(s, "s is null"); if (get() == 0 && compareAndSet(0, 1)) { + if (cancelled) { + // a cancel() that ran entirely between the check above and the CAS + // has already drained and will not see this subscription + s.cancel(); + if (decrementAndGet() != 0) { + drainLoop(); + } + return; + } Subscription a = actual; if (a != null && cancelOnReplace) { diff --git a/src/test/java/io/reactivex/rxjava4/internal/subscriptions/SubscriptionArbiterIsolatedTest.java b/src/test/java/io/reactivex/rxjava4/internal/subscriptions/SubscriptionArbiterIsolatedTest.java new file mode 100644 index 00000000000..a05ae7d74ee --- /dev/null +++ b/src/test/java/io/reactivex/rxjava4/internal/subscriptions/SubscriptionArbiterIsolatedTest.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2016-present, RxJava Contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See + * the License for the specific language governing permissions and limitations under the License. + */ + +package io.reactivex.rxjava4.internal.subscriptions; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Isolated; + +import io.reactivex.rxjava4.core.RxJavaTest; +import io.reactivex.rxjava4.testsupport.TestHelper; + +@Isolated +public class SubscriptionArbiterIsolatedTest extends RxJavaTest { + + @Test + public void setSubscriptionRaceWithCancel() { + for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { + SubscriptionArbiter sa = new SubscriptionArbiter(false); + + sa.setSubscription(new BooleanSubscription()); + sa.request(1); + + BooleanSubscription bs = new BooleanSubscription(); + + TestHelper.race(() -> sa.setSubscription(bs), sa::cancel); + + assertTrue(bs.isCancelled(), "Round " + i); + } + } +}