|
111 | 111 | * re-throw the causing exception as {@link IOException}. The stream can be |
112 | 112 | * terminated at any time, including during a graceful shutdown. |
113 | 113 | * In case the context if terminated <i>before</i> a graceful shutdown begins, |
114 | | - * the parent thread is also interrupted to prevent {@link #add()} from blocking |
| 114 | + * the parent thread is also interrupted to prevent {@link #add} from blocking |
115 | 115 | * indefinitely, "sender" will not be there to pop items from the task queue). |
116 | 116 | * |
117 | 117 | * <p> |
|
122 | 122 | * |
123 | 123 | * @see StreamObserver |
124 | 124 | * @see State |
125 | | - * @see shutdownNow |
| 125 | + * @see #shutdownNow |
126 | 126 | * @see TaskHandle#result() |
127 | 127 | * |
128 | 128 | * @author Dyma Solovei |
@@ -294,6 +294,13 @@ void start() { |
294 | 294 | * is reached. |
295 | 295 | */ |
296 | 296 | void reconnect() throws InterruptedException, ExecutionException { |
| 297 | + // We do not need to wait for the current latch to be "opened". |
| 298 | + // The "sender" survives reconnects and will not call countDown |
| 299 | + // until it's interrupted or the context is closed. |
| 300 | + // The "recv" thread is guaranteed to have already exited, because |
| 301 | + // the context can only transition into the Reconnecting state |
| 302 | + // after the server half of the stream is closed (EOF or hangup). |
| 303 | + assert workers.getCount() == 1 : "recv must exit before reconnect"; |
297 | 304 | workers = new CountDownLatch(2); |
298 | 305 |
|
299 | 306 | messages = streamFactory.createStream(new Recv()); |
@@ -620,23 +627,15 @@ public void onNext(Event event) { |
620 | 627 | */ |
621 | 628 | @Override |
622 | 629 | public void onCompleted() { |
623 | | - try { |
624 | | - onEvent(Event.EOF); |
625 | | - } finally { |
626 | | - System.out.println("recv countDown (onCompleted)"); |
627 | | - workers.countDown(); |
628 | | - } |
| 630 | + workers.countDown(); |
| 631 | + onEvent(Event.EOF); |
629 | 632 | } |
630 | 633 |
|
631 | 634 | /** An exception occurred either on our end or in the channel internals. */ |
632 | 635 | @Override |
633 | 636 | public void onError(Throwable t) { |
634 | | - try { |
635 | | - onEvent(Event.StreamHangup.fromThrowable(t)); |
636 | | - } finally { |
637 | | - System.out.println("recv countDown (onError)"); |
638 | | - workers.countDown(); |
639 | | - } |
| 637 | + workers.countDown(); |
| 638 | + onEvent(Event.StreamHangup.fromThrowable(t)); |
640 | 639 | } |
641 | 640 | } |
642 | 641 |
|
|
0 commit comments