Fixed SIGSEGV when dropping Channel from memory due to memory corruption

This commit is contained in:
Abdelilah El Aissaoui 2024-07-18 01:13:48 +02:00
parent 26f2aeaaf6
commit 21c64d390d
No known key found for this signature in database
GPG Key ID: 7587FC860F594869
3 changed files with 22 additions and 13 deletions

View File

@ -42,17 +42,15 @@ class SshProcess : Process() {
check(!isRunning())
println("exitValue called")
channel.close()
return 0
}
override fun destroy() {
if (channel.isOpen()) {
channel.close()
}
closeChannel()
}
println("Destroy called")
fun closeChannel() {
channel.close()
}
private fun isRunning(): Boolean {

View File

@ -12,20 +12,20 @@ private const val NOT_EXPLICIT_PORT = -1
class SshRemoteSession @Inject constructor(
private val credentialsStateManager: CredentialsStateManager,
) : RemoteSession {
private var session: Session? = null
private lateinit var session: Session
private lateinit var process: SshProcess
override fun exec(commandName: String, timeout: Int): Process {
println("Running command $commandName")
val session = this.session ?: throw Exception("Session is null")
val process = SshProcess()
process = SshProcess()
process.setup(session, commandName)
return process
}
override fun disconnect() {
session?.disconnect()
process.closeChannel()
session.disconnect()
}
fun setup(uri: URIish) {

View File

@ -5,10 +5,13 @@ import Session
import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelInputErrStream
import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelInputStream
import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelOutputStream
import java.util.concurrent.Semaphore
class ChannelWrapper internal constructor(sshSession: Session) {
private val channel = Channel.new(sshSession)
private var isClosed = false
private var closeMutex = Semaphore(1)
val outputStream = SshChannelOutputStream(channel)
val inputStream = SshChannelInputStream(channel)
val errorOutputStream = SshChannelInputErrStream(channel)
@ -26,7 +29,15 @@ class ChannelWrapper internal constructor(sshSession: Session) {
}
fun close() {
channel.closeChannel()
channel.close()
closeMutex.acquire()
try {
if (!isClosed) {
channel.closeChannel()
channel.close()
isClosed = true
}
} finally {
closeMutex.release()
}
}
}