diff --git a/sshlib/src/main/kotlin/org/connectbot/sshlib/client/SshConnection.kt b/sshlib/src/main/kotlin/org/connectbot/sshlib/client/SshConnection.kt index 86297a3..a7ee905 100644 --- a/sshlib/src/main/kotlin/org/connectbot/sshlib/client/SshConnection.kt +++ b/sshlib/src/main/kotlin/org/connectbot/sshlib/client/SshConnection.kt @@ -2048,6 +2048,7 @@ class SshConnection( private fun startPacketLoop() { if (packetLoopJob != null) return packetLoopJob = connectionScope.launch { + var loopException: Exception? = null try { while (isActive) { processNextPacket() @@ -2061,7 +2062,7 @@ class SshConnection( } else { logger.warn("Packet loop ended unexpectedly", e) } - _disconnectedFlow.tryEmit(e) + loopException = e } finally { for (ch in channels.values) { ch.onClose() @@ -2069,6 +2070,9 @@ class SshConnection( for (ch in forwardingChannels.values) { ch.onClose() } + if (loopException != null) { + _disconnectedFlow.tryEmit(loopException) + } } } }