Skip to content

Commit 4d5a585

Browse files
committed
Compliance with Reactive Streams specs
1 parent db82b95 commit 4d5a585

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

utils/src/main/java/software/amazon/awssdk/utils/async/SimplePublisher.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ public final class SimplePublisher<T> implements Publisher<T> {
101101
*/
102102
private final AtomicBoolean subscribed = new AtomicBoolean(false);
103103

104+
/**
105+
* True while the subscriber's {@code onSubscribe} is executing. Used to prevent {@link #processEventQueue()} from
106+
* delivering {@code onNext} signals before {@code onSubscribe} has returned, as required by Reactive Streams rule 1.03.
107+
*/
108+
private volatile boolean onSubscribeInProgress = false;
109+
104110
/**
105111
* The subscriber provided via {@link #subscribe(Subscriber)}. This publisher only supports a single subscriber.
106112
*
@@ -212,7 +218,12 @@ public void subscribe(Subscriber<? super T> s) {
212218
}
213219

214220
this.subscriber = s;
215-
s.onSubscribe(new SubscriptionImpl());
221+
onSubscribeInProgress = true;
222+
try {
223+
s.onSubscribe(new SubscriptionImpl());
224+
} finally {
225+
onSubscribeInProgress = false;
226+
}
216227
processEventQueue();
217228
}
218229

@@ -329,6 +340,11 @@ private boolean shouldProcessQueueEntry(QueueEntry<T> entry) {
329340
return false;
330341
}
331342

343+
if (onSubscribeInProgress) {
344+
// Do not deliver signals until onSubscribe has returned (Reactive Streams rule 1.03).
345+
return false;
346+
}
347+
332348
if (entry.type() != ON_NEXT) {
333349
// This event isn't an on-next event, so we don't need subscriber demand in order to process it.
334350
return true;

0 commit comments

Comments
 (0)