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

Thomas Wolf edited comment on SSHD-1223 at 11/9/21, 8:36 AM:
-------------------------------------------------------------

{quote}This test case is highly suspicious: ClientDeadlockTest
{quote}
I agree. If there is a race somewhere, that should have been tracked down and 
fixed back then.

The root of the MissingAttachedSessionException is the ClientSessionImpl 
constructor already trying to send messages. If that fails, it's possible that 
a just created and registered session gets unregistered from the write failure, 
which may or may not occur concurrently, but the onConnectOperationComplete not 
noticing and just continuing with the client session setup. It's not really a 
race; I can get that on the same thread even. It's just broken design. This 
sending messages from the ClientSessionImpl constructor has caused me problems 
before. :(

The test case is a bit of a special case, though; it tests the server failing 
to accept the connection. Not sure that this is the problem you are seeing with 
10+ concurrent sessions. What kind of server are you connecting to? An Apache 
MINA sshd server? Then the problem might be in the server-side code, and the 
MissingAttachedSessionException on the client side would be only a symptom.

In any case it would be very helpful to have client and server debug logs.


was (Author: wolft):
{quote}This test case is highly suspicious: ClientDeadlockTest{quote}

I agree. If there is a race somewhere, that should have been tracked down and 
fixed back then.

The root of the MissingAttachedSessionException is the ClientSessionImpl 
constructor already trying to send messages. If that fails, it's possible that 
a just created and registered session gets unregistered from the write failure, 
which may or may not occur concurrently, but the onConnectOperationComplete not 
noticing and just continuing with the client session setup. It's not really a 
race; I can get that on the same thread even. It's just broken design. This 
sending messages from the ClientSessionImpl has caused me problems before. :-(

The test case is a bit of a special case, though; it tests the server failing 
to accept the connection. Not sure that this is the problem you are seeing with 
10+ concurrent sessions. What kind of server are you connecting to? An Apache 
MINA sshd server? Then the problem might be in the server-side code, and the 
MissingAttachedSessionException on the client side would be only a symptom.

In any case it would be very helpful to have client and server debug logs.

> MissingAttachedSessionException when starting concurrent SshClient connections
> ------------------------------------------------------------------------------
>
>                 Key: SSHD-1223
>                 URL: https://issues.apache.org/jira/browse/SSHD-1223
>             Project: MINA SSHD
>          Issue Type: Bug
>    Affects Versions: 2.7.0
>         Environment: sshd 2.7.0
> Spring boot 2.5.4
> Java 11
>            Reporter: Steve Jankowski
>            Priority: Major
>
> When 10+ ssh clients are connecting concurrently, some of them fail with  
> MissingAttachedSessionException 
>  
> {noformat}
> org.apache.sshd.common.session.helpers.MissingAttachedSessionException: No 
> session attached to Nio2Session[local=/172.31.20.24:48754, 
> remote=/172.31.20.9:22]
>     at 
> org.apache.sshd.common.session.helpers.AbstractSession.getSession(AbstractSession.java:2443)
>     at 
> org.apache.sshd.common.session.helpers.AbstractSession.getSession(AbstractSession.java:2408)
>     at 
> org.apache.sshd.client.SshClient.onConnectOperationComplete(SshClient.java:739)
>     at 
> org.apache.sshd.client.SshClient$1.operationComplete(SshClient.java:716)
>     at 
> org.apache.sshd.client.SshClient$1.operationComplete(SshClient.java:697)
>     at 
> org.apache.sshd.common.future.AbstractSshFuture.notifyListener(AbstractSshFuture.java:159)
>     at 
> org.apache.sshd.common.future.DefaultSshFuture.notifyListeners(DefaultSshFuture.java:209)
>     at 
> org.apache.sshd.common.future.DefaultSshFuture.setValue(DefaultSshFuture.java:112)
>     at 
> org.apache.sshd.common.io.nio2.Nio2Connector$DefaultIoConnectFuture.setSession(Nio2Connector.java:233)
>     at 
> org.apache.sshd.common.io.nio2.Nio2Connector$ConnectionCompletionHandler.onCompleted(Nio2Connector.java:154)
>     at 
> org.apache.sshd.common.io.nio2.Nio2Connector$ConnectionCompletionHandler.onCompleted(Nio2Connector.java:116)
>     at 
> org.apache.sshd.common.io.nio2.Nio2CompletionHandler.lambda$completed$0(Nio2CompletionHandler.java:38)
>     at java.base/java.security.AccessController.doPrivileged(Native Method)
>     at 
> org.apache.sshd.common.io.nio2.Nio2CompletionHandler.completed(Nio2CompletionHandler.java:37)
>     at java.base/sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:127)
>     at java.base/sun.nio.ch.Invoker$2.run(Invoker.java:219)
>     at 
> java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
>     at 
> java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
>     at 
> java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
>     at java.base/java.lang.Thread.run(Thread.java:829) {noformat}
>  
> My code is roughly:
>  
> {noformat}
> SshClient ssh = SshClient.setUpDefaultClient();
> ssh.start();
> // loop this 10 ish times
> {
>       ConnectFuture future;
>               try {
>                       future = ssh.connect(SSH_USER, ip, SSH_PORT);
>                       future.addListener(new 
> SshFutureListener<ConnectFuture>() {
>                               @Override
>                               public void operationComplete(ConnectFuture 
> future) {
>                                       if (future.getException() != null) {
>                                             logger.error("connect failure", 
> future.getException());
>                                          }
>                               }
>                       });
>               } catch (IOException ex) {
>                       logger.error("startCommand",ex);
>                       return;
>               }
> }
> {noformat}
> Some of the ssh connections fail.  The ConnectFuture listener gets 
> MissingAttachedSessionException
> This test case is highly suspicious:   ClientDeadlockTest
> {noformat}
>     @Test
>     public void testSimpleClient() throws Exception {
>         client.start();
>         ConnectFuture future = client.connect(getCurrentTestName(), 
> TEST_LOCALHOST, port);
>         try (ClientSession session = 
> future.verify(CONNECT_TIMEOUT).getSession()) {
>             Collection<ClientSessionEvent> events
>                     = 
> session.waitFor(EnumSet.of(ClientSession.ClientSessionEvent.CLOSED), 
> TimeUnit.SECONDS.toMillis(7L));
>             assertTrue("Close event not signalled: " + events, 
> events.contains(ClientSession.ClientSessionEvent.CLOSED));
>             assertFalse("Session not marked as closed", session.isOpen());
>         } catch (SshException e) {
>             Throwable cause = e.getCause();
>             // Due to a race condition we might get this exception
>             if (!(cause instanceof MissingAttachedSessionException)) {
>                 throw e;
>             }
>         }
>     }
> {noformat}
> The test case explicitly calls out a race condition, and ignores the 
> exception.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

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

Reply via email to