hierynomus / hierynomus/sshj

Simplex sessions leads to ConnectionException "open failed" even after closing previous one.

Open
#856 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.7k
Forks
620
Avg merge
3d 23h
Merged PRs (30d)
11

Description

I tried closing Session and it's streams individually but it always fails to close it immediately when I try to use same connection for multiple Sessions. If I add a sleep of 1 sec, it works fine. Seems that there is some delay in closing session which leads to this error. Is there any way we can wait for session closure?
It can be reproduced if we set `MaxSessions` = 1 in `sshd_config`

```
public static void run() throws Exception {
final SSHClient client = new SSHClient();
client.addHostKeyVerifier(new PromiscuousVerifier());
client.connect("54.197.72.143");
try {
OpenSSHKeyFile openSSHKeyFile = new OpenSSHKeyFile();
openSSHKeyFile.init(KEY, null);
client.authPublickey("ubuntu", openSSHKeyFile);
exec(client);
exec(client);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}

public static void exec(SSHClient client) throws IOException {
Session.Command cmd;
try (Session session = client.startSession()) {
cmd = session.exec("ping -c 2 google.com; whoami");
ByteArrayOutputStream byteArrayOutputStream =
IOUtils.readFully(cmd.getInputStream());
System.out.println(byteArrayOutputStream.toString());
cmd.join(50, TimeUnit.SECONDS);
System.out.println("Done reading");
}
cmd.getInputStream().close();
cmd.getErrorStream().close();
cmd.close();
}

```

sshd_config:
```
Include /etc/ssh/sshd_config.d/*.conf
LogLevel VERBOSE
MaxSessions 1
HostKeyAlgorithms +ssh-rsa
PubkeyAcceptedKeyTypes +ssh-rsa
PasswordAuthentication no
KbdInteractiveAuthentication no
UsePAM yes
X11Forwarding yes
PrintMotd no

AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

```

sshd_config logs:

```
Apr 18 22:13:08 ip-*** sshd[136118]: Read error from remote host *.*.*.* port 64955: Connection reset by peer
Apr 18 22:13:08 ip-*** sshd[136052]: pam_unix(sshd:session): session closed for user ubuntu
Apr 18 22:13:10 ip-*** sshd[142604]: Connection from *.*.*.* port 49605 on *.*.*.* port 22 rdomain ""
Apr 18 22:13:11 ip-*** sshd[142604]: Accepted key RSA SHA256:*** found at /home/ubuntu/.ssh/authorized_keys:3
Apr 18 22:13:11 ip-*** sshd[142604]: Postponed publickey for ubuntu from *.*.*.* port 49605 ssh2 [preauth]
Apr 18 22:13:11 ip-*** sshd[142604]: Accepted key RSA SHA256:*** found at /home/ubuntu/.ssh/authorized_keys:3
Apr 18 22:13:11 ip-*** sshd[142604]: Accepted publickey for ubuntu from *.*.*.* port 49605 ssh2: RSA SHA256:***
Apr 18 22:13:11 ip-*** sshd[142604]: pam_unix(sshd:session): session opened for user ubuntu(uid=1000) by (uid=0)
Apr 18 22:13:12 ip-*** sshd[142604]: User child is on pid 142670
Apr 18 22:13:12 ip-*** sshd[142670]: Starting session: command for ubuntu from *.*.*.* port 49605 id 0
Apr 18 22:13:13 ip-*** sshd[142670]: error: no more sessions
Apr 18 22:13:13 ip-*** sshd[142670]: Close session: user ubuntu from *.*.*.* port 49605 id 0
Apr 18 22:13:14 ip-*** sshd[142670]: Connection closed by *.*.*.* port 49605
Apr 18 22:13:14 ip-*** sshd[142670]: Transferred: sent 3588, received 4440 bytes
Apr 18 22:13:14 ip-*** sshd[142670]: Closing connection to *.*.*.* port 49605
Apr 18 22:13:14 ip-*** sshd[142604]: pam_unix(sshd:session): session closed for user ubuntu
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.