You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by "Lyor Goldstein (Jira)" <ji...@apache.org> on 2022/07/12 17:16:00 UTC

[jira] [Commented] (SSHD-1278) How to release client connection resources after connection timeout occurs ?

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

Lyor Goldstein commented on SSHD-1278:
--------------------------------------

I don't understand the code and what you are trying to achieve - it does seem though that you have not read the documentation carefully. In any case, here is the recommended way to connect and authenticate using {{try-with-resource}}:

{code:java}
try (ClientSession session = client.connect(...uri...).verify(CONNECT_TIMEOUT).getSession()) {
      session.addPasswordIdentity(getCurrentTestName()); // or add key identity
      session.auth().verify(AUTH_TIMEOUT);
}
{code}
I fail to see the need to use the {{ConnectFuture}} or query it for {{isConnected}}. As far as releasing the connection - there is no need - if {{verify}} fails, it throws an exception which causes the {{try-with-resource}} block to exit and thus release whatever needs to be released.


As far as your code is concerned - it behaves as it should:
{code:java}
ConnectFuture connectFuture = sshClient.connect(uri.toString());
try {
    //connectFuture.isConnected() returns false - Of course it does, only when verify returns the connection is established
    connectFuture.verify(timeout);
} catch (IOException e) {
    //connectFuture.isConnected() returns true - so what ? there was an exception so obviously the connection is doomed.
                                 Furthermore, the disconnect is ASYNCHRONOUS - so you might have sampled isConnected too soon
    connectFuture.cancel();
    //connectFuture.isCanceled() returns false - ASYNCHRONOUS (!)
    //connectFuture.isConnected() returns true - ASYNCHRONOUS (!)

    throw e;
}
{code}

Please documentation carefully (again, and again, and again) - and make sure to look at the code samples there for recommended usage. Try to adhere to the coding patterns shown in the documentation unless there is very good justification to do otherwise. Also, I highly recommend to look at you test code as it contains 100's of examples of possible use cases - even unusual ones.

> 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: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org