@@ -68,10 +68,10 @@ public abstract class AbstractListenerReadPublisher<T> implements Publisher<T> {
68
68
@ Nullable
69
69
private volatile Subscriber <? super T > subscriber ;
70
70
71
- private volatile boolean completionBeforeDemand ;
71
+ private volatile boolean completionPending ;
72
72
73
73
@ Nullable
74
- private volatile Throwable errorBeforeDemand ;
74
+ private volatile Throwable errorPending ;
75
75
76
76
private final String logPrefix ;
77
77
@@ -228,21 +228,24 @@ private void changeToDemandState(State oldState) {
228
228
}
229
229
}
230
230
231
- private void handleCompletionOrErrorBeforeDemand () {
231
+ private boolean handlePendingCompletionOrError () {
232
232
State state = this .state .get ();
233
- if (! state .equals (State .UNSUBSCRIBED ) && ! state .equals (State .SUBSCRIBING )) {
234
- if (this .completionBeforeDemand ) {
235
- rsReadLogger .trace (getLogPrefix () + "Completed before demand " );
233
+ if (state .equals (State .DEMAND ) || state .equals (State .NO_DEMAND )) {
234
+ if (this .completionPending ) {
235
+ rsReadLogger .trace (getLogPrefix () + "Processing pending completion " );
236
236
this .state .get ().onAllDataRead (this );
237
+ return true ;
237
238
}
238
- Throwable ex = this .errorBeforeDemand ;
239
+ Throwable ex = this .errorPending ;
239
240
if (ex != null ) {
240
241
if (rsReadLogger .isTraceEnabled ()) {
241
- rsReadLogger .trace (getLogPrefix () + "Completed with error before demand : " + ex );
242
+ rsReadLogger .trace (getLogPrefix () + "Processing pending completion with error : " + ex );
242
243
}
243
244
this .state .get ().onError (this , ex );
245
+ return true ;
244
246
}
245
247
}
248
+ return false ;
246
249
}
247
250
248
251
private Subscription createSubscription () {
@@ -305,7 +308,7 @@ <T> void subscribe(AbstractListenerReadPublisher<T> publisher, Subscriber<? supe
305
308
publisher .subscriber = subscriber ;
306
309
subscriber .onSubscribe (subscription );
307
310
publisher .changeState (SUBSCRIBING , NO_DEMAND );
308
- publisher .handleCompletionOrErrorBeforeDemand ();
311
+ publisher .handlePendingCompletionOrError ();
309
312
}
310
313
else {
311
314
throw new IllegalStateException ("Failed to transition to SUBSCRIBING, " +
@@ -315,14 +318,14 @@ <T> void subscribe(AbstractListenerReadPublisher<T> publisher, Subscriber<? supe
315
318
316
319
@ Override
317
320
<T > void onAllDataRead (AbstractListenerReadPublisher <T > publisher ) {
318
- publisher .completionBeforeDemand = true ;
319
- publisher .handleCompletionOrErrorBeforeDemand ();
321
+ publisher .completionPending = true ;
322
+ publisher .handlePendingCompletionOrError ();
320
323
}
321
324
322
325
@ Override
323
326
<T > void onError (AbstractListenerReadPublisher <T > publisher , Throwable ex ) {
324
- publisher .errorBeforeDemand = ex ;
325
- publisher .handleCompletionOrErrorBeforeDemand ();
327
+ publisher .errorPending = ex ;
328
+ publisher .handlePendingCompletionOrError ();
326
329
}
327
330
},
328
331
@@ -341,14 +344,14 @@ <T> void request(AbstractListenerReadPublisher<T> publisher, long n) {
341
344
342
345
@ Override
343
346
<T > void onAllDataRead (AbstractListenerReadPublisher <T > publisher ) {
344
- publisher .completionBeforeDemand = true ;
345
- publisher .handleCompletionOrErrorBeforeDemand ();
347
+ publisher .completionPending = true ;
348
+ publisher .handlePendingCompletionOrError ();
346
349
}
347
350
348
351
@ Override
349
352
<T > void onError (AbstractListenerReadPublisher <T > publisher , Throwable ex ) {
350
- publisher .errorBeforeDemand = ex ;
351
- publisher .handleCompletionOrErrorBeforeDemand ();
353
+ publisher .errorPending = ex ;
354
+ publisher .handlePendingCompletionOrError ();
352
355
}
353
356
},
354
357
@@ -379,14 +382,17 @@ <T> void onDataAvailable(AbstractListenerReadPublisher<T> publisher) {
379
382
boolean demandAvailable = publisher .readAndPublish ();
380
383
if (demandAvailable ) {
381
384
publisher .changeToDemandState (READING );
385
+ publisher .handlePendingCompletionOrError ();
382
386
}
383
387
else {
384
388
publisher .readingPaused ();
385
389
if (publisher .changeState (READING , NO_DEMAND )) {
386
- // Demand may have arrived since readAndPublish returned
387
- long r = publisher .demand ;
388
- if (r > 0 ) {
389
- publisher .changeToDemandState (NO_DEMAND );
390
+ if (!publisher .handlePendingCompletionOrError ()) {
391
+ // Demand may have arrived since readAndPublish returned
392
+ long r = publisher .demand ;
393
+ if (r > 0 ) {
394
+ publisher .changeToDemandState (NO_DEMAND );
395
+ }
390
396
}
391
397
}
392
398
}
@@ -408,6 +414,18 @@ <T> void request(AbstractListenerReadPublisher<T> publisher, long n) {
408
414
publisher .changeToDemandState (NO_DEMAND );
409
415
}
410
416
}
417
+
418
+ @ Override
419
+ <T > void onAllDataRead (AbstractListenerReadPublisher <T > publisher ) {
420
+ publisher .completionPending = true ;
421
+ publisher .handlePendingCompletionOrError ();
422
+ }
423
+
424
+ @ Override
425
+ <T > void onError (AbstractListenerReadPublisher <T > publisher , Throwable ex ) {
426
+ publisher .errorPending = ex ;
427
+ publisher .handlePendingCompletionOrError ();
428
+ }
411
429
},
412
430
413
431
COMPLETED {
0 commit comments