Skip to content

Commit 21c64d3

Browse files
committed
Fixed SIGSEGV when dropping Channel from memory due to memory corruption
1 parent 26f2aea commit 21c64d3

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

src/main/kotlin/com/jetpackduba/gitnuro/credentials/SshProcess.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff 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 {

src/main/kotlin/com/jetpackduba/gitnuro/credentials/SshRemoteSession.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ private const val NOT_EXPLICIT_PORT = -1
1212
class 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) {

src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/ChannelWrapper.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import Session
55
import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelInputErrStream
66
import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelInputStream
77
import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelOutputStream
8+
import java.util.concurrent.Semaphore
89

910
class 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
}

0 commit comments

Comments
 (0)