[ 
https://issues.apache.org/jira/browse/SSHD-1278?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17566288#comment-17566288
 ] 

dgü commented on SSHD-1278:
---------------------------

+For ClientSession:+


{code:java}
/* If timeout happens, no ClientSession instance will be created. session will 
be null. It will not be possible to close it. But, connection attempt will 
continue in background since it is asynchronous.
At the and there will be orphan resources created after timeout exception.
*/
try (ClientSession session = 
client.connect(...uri...).verify(CONNECT_TIMEOUT).getSession()) {
      ...
}
{code}


+For ConnectFuture#isConnected():+

I am not using {{ConnectFuture#isConnected()}} indeed. I wrote in the example 
to show asynchronous nature and race condition problem when timeout or any 
other exception occurs.


{code:java}
/*Application goes into to this branch when connectFuture.isConnected() returns 
false.
But, after a while in the branch, connectFuture.isConnected() may return true 
since it's asynchronous. So application logic in this branch my be buggy.*/
if (!connectFuture.isConnected()) {
...
}
{code}

The problem is not limited to timeout exceptions. When an application gets an 
exception, SSHD may be working in the background. How can I release and check 
the resources working in background ?

> How to release client connection resources after connection timeout occurs ?
> ----------------------------------------------------------------------------
>
>                 Key: SSHD-1278
>                 URL: https://issues.apache.org/jira/browse/SSHD-1278
>             Project: MINA SSHD
>          Issue Type: Question
>    Affects Versions: 2.8.0
>         Environment: Java SE 8, NetBeans IDE 8.2
>            Reporter: dgü
>            Assignee: Lyor Goldstein
>            Priority: Major
>
> Hello!
> I want to release client connection resources after connection timeout occurs 
> ?
> This is the code:
> {code:java}
> ConnectFuture connectFuture = sshClient.connect(uri.toString());
> try {
>     //connectFuture.isConnected() returns false
>     connectFuture.verify(timeout);
> } catch (IOException e) {
>     //connectFuture.isConnected() returns true
>     connectFuture.cancel();
>     //connectFuture.isCanceled() returns false
>     //connectFuture.isConnected() returns true
>     throw e;
> }
> {code}
> I guess there is a race condition between client connection and 
> {{ConnectFuture#isConnected()}}. 
> For example, this code may be wrong:
> {code:java}
> if (!connectFuture.isConnected()) {
>   //connectFuture.isConnected() may return true
> }
> {code}
> In my case, session is connected after timeout. 
> How can I be sure that client is still not connected and release its 
> resources after connection timeout occurs ?
> Thanks in advance!



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

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

Reply via email to