Skip to content
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

fix: race condition on cancellation in UniCallbackSubscriber #1495

Merged
merged 1 commit into from
Jan 23, 2024
Merged
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 @@ -79,7 +79,7 @@ public final void onItem(T x) {
@Override
public void cancel() {
UniSubscription sub = SUBSCRIPTION_UPDATER.getAndSet(this, CANCELLED);
if (sub != CANCELLED) {
if (sub != null && sub != CANCELLED) {
sub.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
Expand All @@ -15,7 +18,10 @@
import org.junit.jupiter.api.parallel.ResourceLock;

import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.operators.AbstractUni;
import io.smallrye.mutiny.subscription.Cancellable;
import io.smallrye.mutiny.subscription.UniSubscriber;
import io.smallrye.mutiny.subscription.UniSubscription;
import junit5.support.InfrastructureResource;

@ResourceLock(value = InfrastructureResource.NAME, mode = ResourceAccessMode.READ)
Expand Down Expand Up @@ -105,4 +111,30 @@ public void testTwoCallbacksVariant() {
cancellable.cancel();
}

@Test
public void cancellationBeforeSubscriptionArrivesDoesntThrowNpe() {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
try {
AbstractUni<String> uni = new AbstractUni<>() {
@Override
public void subscribe(UniSubscriber<? super String> subscriber) {
scheduler.schedule(() -> {
subscriber.onSubscribe(new UniSubscription() {
@Override
public void cancel() {

}
});
subscriber.onItem("Yolo");
}, 10, TimeUnit.SECONDS);
}
};

AtomicReference<String> result = new AtomicReference<>();
uni.subscribe().with(result::set).cancel();
assertThat(result).hasNullValue();
} finally {
scheduler.shutdown();
}
}
}
Loading