[ 
https://issues.apache.org/jira/browse/SSHD-1261?focusedWorklogId=759781&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-759781
 ]

ASF GitHub Bot logged work on SSHD-1261:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 21/Apr/22 06:36
            Start Date: 21/Apr/22 06:36
    Worklog Time Spent: 10m 
      Work Description: tomaswolf commented on code in PR #218:
URL: https://github.com/apache/mina-sshd/pull/218#discussion_r854828577


##########
sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelAsyncOutputStream.java:
##########
@@ -127,6 +129,28 @@ protected CloseFuture doCloseGracefully() {
         return builder().when(pendingWrite.get()).build().close(false);
     }
 
+    @Override
+    protected void doCloseImmediately() {
+        Channel channel = getChannel();
+        long channelId = (channel == null) ? -1L : channel.getChannelId();
+        abortCurrentWrite(() -> new SshChannelClosedException(channelId, 
"Channel closed before pending write completed"));
+
+        super.doCloseImmediately();
+    }
+
+    protected synchronized IoWriteFutureImpl abortCurrentWrite(Supplier<? 
extends Exception> errorProvider) {
+        IoWriteFutureImpl future = pendingWrite.get();
+        if ((future != null) && (!future.isDone())) {
+            Exception error = errorProvider.get();
+            log.debug("abortCurrentWrite({}) aborting pending write={} - {} : 
{}",
+                    this, future, error == null ? null : 
error.getClass().getSimpleName(),
+                    error == null ? null : error.getMessage());
+            future.setValue(error);
+        }
+
+        return future;
+    }

Review Comment:
   Terminating the current future is probably right, but still I don't think 
this will help in all situations. On a graceful shutdown, 
`AbstractCloseable.close` will call `doCloseGracefully()` *first*, which will 
block until the pending future is fulfilled.
   
   However, it also calls `preClose()` first, which will already close the 
`packetWriter`.
   
   I'm not sure a `ChannelAsyncOutputStream` *can* be closed gracefully in this 
situation.





Issue Time Tracking
-------------------

            Worklog Id:     (was: 759781)
    Remaining Estimate: 0h
            Time Spent: 10m

> Sometimes async write listener is not called
> --------------------------------------------
>
>                 Key: SSHD-1261
>                 URL: https://issues.apache.org/jira/browse/SSHD-1261
>             Project: MINA SSHD
>          Issue Type: Bug
>    Affects Versions: 2.8.0
>            Reporter: Evgeny Pasynkov
>            Assignee: Lyor Goldstein
>            Priority: Major
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> Hello,
> I have discovered the case when the async write listener is not called.
> Imagine the case: The `ChannelAsyncOutputStream::doWriteIfPossible` method 
> discover that window size is too small (lines 160-167). It setup new future 
> and exit the doWriteIfPossible method, relying on `onWindowExpanded` will be 
> called shortly. But if the channel is disconnected, the method will never be 
> invoked, thus future won't be completed.
>  I suggest adding the following override to ChannelAsyncOutputStream class:
> {code:java}
>     @Override
>     protected void doCloseImmediately() {
>         abortCurrentWrite();
>         super.doCloseImmediately();
>     }
>     protected synchronized void abortCurrentWrite() {
>         IoWriteFutureImpl future = pendingWrite.get();
>         if (future != null) {
>             future.setValue(new ClosedChannelException());
>         }        
>     }
> {code}



--
This message was sent by Atlassian Jira
(v8.20.7#820007)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to