File tree Expand file tree Collapse file tree 3 files changed +22
-13
lines changed
src/main/kotlin/com/jetpackduba/gitnuro Expand file tree Collapse file tree 3 files changed +22
-13
lines changed Original file line number Diff line number Diff line change @@ -42,17 +42,15 @@ class SshProcess : Process() {
4242 check(! isRunning())
4343 println (" exitValue called" )
4444
45- channel.close()
46-
4745 return 0
4846 }
4947
5048 override fun destroy () {
51- if (channel.isOpen()) {
52- channel.close()
53- }
49+ closeChannel()
50+ }
5451
55- println (" Destroy called" )
52+ fun closeChannel () {
53+ channel.close()
5654 }
5755
5856 private fun isRunning (): Boolean {
Original file line number Diff line number Diff line change @@ -12,20 +12,20 @@ private const val NOT_EXPLICIT_PORT = -1
1212class SshRemoteSession @Inject constructor(
1313 private val credentialsStateManager : CredentialsStateManager ,
1414) : RemoteSession {
15- private var session: Session ? = null
16-
15+ private lateinit var session: Session
16+ private lateinit var process : SshProcess
1717 override fun exec (commandName : String , timeout : Int ): Process {
1818 println (" Running command $commandName " )
1919
20- val session = this .session ? : throw Exception (" Session is null" )
21- val process = SshProcess ()
20+ process = SshProcess ()
2221
2322 process.setup(session, commandName)
2423 return process
2524 }
2625
2726 override fun disconnect () {
28- session?.disconnect()
27+ process.closeChannel()
28+ session.disconnect()
2929 }
3030
3131 fun setup (uri : URIish ) {
Original file line number Diff line number Diff line change @@ -5,10 +5,13 @@ import Session
55import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelInputErrStream
66import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelInputStream
77import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelOutputStream
8+ import java.util.concurrent.Semaphore
89
910class ChannelWrapper internal constructor(sshSession : Session ) {
1011 private val channel = Channel .new(sshSession)
1112
13+ private var isClosed = false
14+ private var closeMutex = Semaphore (1 )
1215 val outputStream = SshChannelOutputStream (channel)
1316 val inputStream = SshChannelInputStream (channel)
1417 val errorOutputStream = SshChannelInputErrStream (channel)
@@ -26,7 +29,15 @@ class ChannelWrapper internal constructor(sshSession: Session) {
2629 }
2730
2831 fun close () {
29- channel.closeChannel()
30- channel.close()
32+ closeMutex.acquire()
33+ try {
34+ if (! isClosed) {
35+ channel.closeChannel()
36+ channel.close()
37+ isClosed = true
38+ }
39+ } finally {
40+ closeMutex.release()
41+ }
3142 }
3243}
You can’t perform that action at this time.
0 commit comments