Skip to content

Commit

Permalink
[SSHD-1281] Close Nio2Session properly on failure after connecting
Browse files Browse the repository at this point in the history
If there's an exception in Nio2Connector after the session was set on
the DefaultIoConnectFuture, a program might just hang on that session
until the next read or write attempt, or until a timeout expired.

This was caused by closing the underlying connection, but not properly
closing the already existing session, and because the session was
already set on the future, setting the exception had no effect anymore.

Fix this by immediately closing the Nio2Session properly in that case.
  • Loading branch information
tomaswolf committed Aug 2, 2022
1 parent 6514e09 commit 7cfe640
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@
# [Version 2.8.0 to 2.9.0](./docs/changes/2.9.0.md)

# Planned for next version

## Bug fixes

* [SSHD-1281](https://issues.apache.org/jira/browse/SSHD-1281) ClientSession.auth().verify() is terminated with timeout
* [SSHD-1285](https://issues.apache.org/jira/browse/SSHD-1285) 2.9.0 release broken on Java 8
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,24 @@ protected void onCompleted(Void result, Object attachment) {
}
}

debug("onCompleted - failed {} to start session: {}",
t.getClass().getSimpleName(), t.getMessage(), t);
log.debug("onCompleted - failed to start session: {} {}", t.getClass().getSimpleName(), t.getMessage(), t);

try {
socket.close();
} catch (IOException err) {
if (debugEnabled) {
log.debug("onCompleted - failed {} to close socket: {}",
err.getClass().getSimpleName(), err.getMessage());
IoSession session = future.getSession();
if (session != null) {
session.close(true);
} else {
try {
socket.close();
} catch (IOException err) {
if (debugEnabled) {
log.debug("onCompleted - failed to close socket: {} {}", err.getClass().getSimpleName(),
err.getMessage());
}
}
}

future.setException(t);
unmapSession(sessionId);
future.setException(t);
unmapSession(sessionId);
}
}
}

Expand Down

0 comments on commit 7cfe640

Please sign in to comment.