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()) check(!isRunning())
println("exitValue called") println("exitValue called")
channel.close()
return 0 return 0
} }
override fun destroy() { override fun destroy() {
if (channel.isOpen()) { closeChannel()
channel.close()
} }
println("Destroy called") fun closeChannel() {
channel.close()
} }
private fun isRunning(): Boolean { private fun isRunning(): Boolean {

View File

@ -12,20 +12,20 @@ private const val NOT_EXPLICIT_PORT = -1
class SshRemoteSession @Inject constructor( class SshRemoteSession @Inject constructor(
private val credentialsStateManager: CredentialsStateManager, private val credentialsStateManager: CredentialsStateManager,
) : RemoteSession { ) : RemoteSession {
private var session: Session? = null private lateinit var session: Session
private lateinit var process: SshProcess
override fun exec(commandName: String, timeout: Int): Process { override fun exec(commandName: String, timeout: Int): Process {
println("Running command $commandName") println("Running command $commandName")
val session = this.session ?: throw Exception("Session is null") process = SshProcess()
val process = SshProcess()
process.setup(session, commandName) process.setup(session, commandName)
return process return process
} }
override fun disconnect() { override fun disconnect() {
session?.disconnect() process.closeChannel()
session.disconnect()
} }
fun setup(uri: URIish) { 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.SshChannelInputErrStream
import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelInputStream import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelInputStream
import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelOutputStream import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelOutputStream
import java.util.concurrent.Semaphore
class ChannelWrapper internal constructor(sshSession: Session) { class ChannelWrapper internal constructor(sshSession: Session) {
private val channel = Channel.new(sshSession) private val channel = Channel.new(sshSession)
private var isClosed = false
private var closeMutex = Semaphore(1)
val outputStream = SshChannelOutputStream(channel) val outputStream = SshChannelOutputStream(channel)
val inputStream = SshChannelInputStream(channel) val inputStream = SshChannelInputStream(channel)
val errorOutputStream = SshChannelInputErrStream(channel) val errorOutputStream = SshChannelInputErrStream(channel)
@ -26,7 +29,15 @@ class ChannelWrapper internal constructor(sshSession: Session) {
} }
fun close() { fun close() {
closeMutex.acquire()
try {
if (!isClosed) {
channel.closeChannel() channel.closeChannel()
channel.close() channel.close()
isClosed = true
}
} finally {
closeMutex.release()
}
} }
} }