-
Notifications
You must be signed in to change notification settings - Fork 1k
Compliance with Reactive Streams specs #7191
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,8 +95,23 @@ public final class SimplePublisher<T> implements Publisher<T> { | |
| */ | ||
| private final FailureMessage failureMessage = new FailureMessage(); | ||
|
|
||
| /** | ||
| * Whether {@link #subscribe(Subscriber)} has already been called, used to reject a second subscription. Tracked | ||
| * separately from {@link #subscriber} because that field is cleared on termination and must not re-open subscription. | ||
| */ | ||
| private final AtomicBoolean subscribed = new AtomicBoolean(false); | ||
|
|
||
| /** | ||
| * True while the subscriber's {@code onSubscribe} is executing. Used to prevent {@link #processEventQueue()} from | ||
| * delivering {@code onNext} signals before {@code onSubscribe} has returned, as required by Reactive Streams rule 1.03. | ||
| */ | ||
| private volatile boolean onSubscribeInProgress = false; | ||
|
|
||
| /** | ||
| * The subscriber provided via {@link #subscribe(Subscriber)}. This publisher only supports a single subscriber. | ||
| * | ||
| * <p>Cleared when the stream terminates (complete, error, or cancel) so the publisher does not retain the subscriber | ||
| * reference, per Reactive Streams rule 3.13. | ||
| */ | ||
| private Subscriber<? super T> subscriber; | ||
|
|
||
|
|
@@ -196,14 +211,19 @@ public CompletableFuture<Void> error(Throwable error) { | |
| */ | ||
| @Override | ||
| public void subscribe(Subscriber<? super T> s) { | ||
| if (subscriber != null) { | ||
| if (!subscribed.compareAndSet(false, true)) { | ||
| s.onSubscribe(new NoOpSubscription()); | ||
| s.onError(new IllegalStateException("Only one subscription may be active at a time.")); | ||
| return; | ||
| } | ||
|
|
||
| this.subscriber = s; | ||
| s.onSubscribe(new SubscriptionImpl()); | ||
| onSubscribeInProgress = true; | ||
| try { | ||
| s.onSubscribe(new SubscriptionImpl()); | ||
| } finally { | ||
| onSubscribeInProgress = false; | ||
| } | ||
| processEventQueue(); | ||
| } | ||
|
|
||
|
|
@@ -275,6 +295,7 @@ private void doProcessQueue() { | |
|
|
||
| log.trace(() -> "Calling onComplete()"); | ||
| subscriber.onComplete(); | ||
| subscriber = null; | ||
| break; | ||
| case ON_ERROR: | ||
|
|
||
|
|
@@ -283,9 +304,11 @@ private void doProcessQueue() { | |
| onErrorEntry.failure)); | ||
| log.trace(() -> "Calling onError() with " + onErrorEntry.failure, onErrorEntry.failure); | ||
| subscriber.onError(onErrorEntry.failure); | ||
| subscriber = null; | ||
| break; | ||
| case CANCEL: | ||
| failureMessage.trySet(() -> new CancellationException("subscription has been cancelled.")); | ||
| subscriber = null; | ||
| break; | ||
| default: | ||
| // Should never happen. Famous last words? | ||
|
|
@@ -317,6 +340,11 @@ private boolean shouldProcessQueueEntry(QueueEntry<T> entry) { | |
| return false; | ||
| } | ||
|
|
||
| if (onSubscribeInProgress) { | ||
| // Do not deliver signals until onSubscribe has returned (Reactive Streams rule 1.03). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trying to understand this: how could processEventQueue be invoked if onSubscribeInProgress?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The subscriber calls |
||
| return false; | ||
| } | ||
|
|
||
| if (entry.type() != ON_NEXT) { | ||
| // This event isn't an on-next event, so we don't need subscriber demand in order to process it. | ||
| return true; | ||
|
|
@@ -338,7 +366,10 @@ private void panicAndDie(Throwable cause) { | |
| // Create exception here instead of in supplier to preserve a more-useful stack trace. | ||
| RuntimeException failure = new IllegalStateException("Encountered fatal error in publisher", cause); | ||
| failureMessage.trySet(() -> failure); | ||
| subscriber.onError(cause instanceof Error ? cause : failure); | ||
| if (subscriber != null) { | ||
| subscriber.onError(cause instanceof Error ? cause : failure); | ||
| subscriber = null; | ||
| } | ||
|
|
||
| while (true) { | ||
| QueueEntry<T> entry = standardPriorityQueue.poll(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could there be a race condition where onErrorInvoked turns to true (onError invoked) right after the check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, Rule 2.3 only prohibits calling
cancel()from within theonErrorcall stack (same thread). That's the case this flag guards:onError→ sets flag → callscompleteExceptionally()→ handler runs inline on the same thread → sees flag → skips cancel. In the scenario you're describing (different thread sets the flag after the check), the handler is running because of an external abort, not because ofonError— so callingcancel()there is correct and not a rule 2.3 violation.