fix(dual-screen): the bottom-screen terminal keeps its shell when another app covers that screen (#2366)

* fix(dual-screen): the bottom-screen terminal keeps its shell when another app covers that screen

* fix(session): sweep the kept terminal shell's process tree when the session ends
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-15 10:16:01 -05:00
1 parent 4e3eeea5e2
commit d0398fd853
5 files changed
+128 -8

No files matched your search

@@ -2264,10 +2264,11 @@ class SessionService : Service() {
syncWatcher = null
val prootPid = sessionPid
sessionPid = -1
// The kept shell goes through the same teardown, which reads its descendants before signalling it.
val shell = SessionTerminal.detach(sessionGen)?.let { pid -> readStat(pid)?.second?.let { pid to it } }
val auxiliary = synchronized(auxiliaryProcessesLock) {
auxiliaryProcesses.toMap().also { auxiliaryProcesses.clear() }
}
SessionTerminal.clear(sessionGen)
} + listOfNotNull(shell)
BottomDesktop.stop(this, stoppedGen)
val controller = suspendController
suspendController = null
@@ -14,6 +14,12 @@ object SessionTerminal {
@Volatile private var launch: Launch? = null
private class Kept(val session: TerminalSession, val generation: Int)
// The bottom screen's presentation is dismissed whenever another app covers that display
// (a browser opened for a sign-in), so its shell is kept for the next panel.
@Volatile private var kept: Kept? = null
fun prepare(
command: List<String>,
hostEnvironment: Array<String>,
@@ -32,8 +38,41 @@ object SessionTerminal {
).also { it.mSessionName = "DroidDeck Linux shell" }
}
fun reuse(client: TerminalSessionClient): TerminalSession? {
val current = kept ?: return null
if (current.generation != launch?.generation || !current.session.isRunning) {
kept = null
return null
}
current.session.updateTerminalSessionClient(client)
return current.session
}
fun keep(session: TerminalSession) {
val generation = launch?.generation ?: return
kept = Kept(session, generation)
}
fun release(session: TerminalSession?) {
if (session != null && kept?.session === session) kept = null
}
fun clear(generation: Int? = null) {
val current = launch ?: return
if (generation == null || current.generation == generation) launch = null
if (generation == null || current.generation == generation) {
launch = null
kept?.session?.takeIf { it.pid > 0 }?.finishIfRunning()
kept = null
}
}
/** Ends this generation's terminal without killing its shell, and returns the shell's pid if it still runs. */
fun detach(generation: Int): Int? {
val current = launch ?: return null
if (current.generation != generation) return null
launch = null
val pid = kept?.session?.takeIf { it.isRunning && it.pid > 0 }?.pid
kept = null
return pid
}
}
@@ -364,7 +364,7 @@ class SecondScreenPresentation(
private fun renderTerminal() {
bar(SecondScreenMode.TERMINAL)
val panel = SessionTerminalPanel(context)
val panel = SessionTerminalPanel(context, keepShell = true)
terminalPanel = panel
root.addView(panel, weightParams())
}
@@ -21,7 +21,7 @@ import com.termux.terminal.TerminalSessionClient
import com.termux.view.TerminalView
import com.termux.view.TerminalViewClient
class SessionTerminalPanel(context: Context) : LinearLayout(context) {
class SessionTerminalPanel(context: Context, private val keepShell: Boolean = false) : LinearLayout(context) {
private var terminalSession: TerminalSession? = null
private var terminalView: TerminalView? = null
private var terminalPid = -1
@@ -45,7 +45,8 @@ class SessionTerminalPanel(context: Context) : LinearLayout(context) {
isFocusableInTouchMode = true
}
terminalView = view
val session = SessionTerminal.create(terminalClient)
val session = (if (keepShell) SessionTerminal.reuse(terminalClient) else null)
?: SessionTerminal.create(terminalClient)?.also { if (keepShell) SessionTerminal.keep(it) }
if (session == null) {
addView(TextView(context).apply {
text = context.getString(R.string.second_terminal_not_ready)
@@ -121,8 +122,12 @@ class SessionTerminalPanel(context: Context) : LinearLayout(context) {
(context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
.hideSoftInputFromWindow(view.windowToken, 0)
}
val pid = terminalPid.takeIf { it > 1 } ?: (terminalSession?.pid ?: -1)
if (pid > 1) SessionService.stopTerminalProcess(context, pid)
val session = terminalSession
if (!keepShell || session?.isRunning != true) {
val pid = terminalPid.takeIf { it > 1 } ?: (session?.pid ?: -1)
if (pid > 1) SessionService.stopTerminalProcess(context, pid)
SessionTerminal.release(session)
}
terminalPid = -1
terminalSession = null
terminalView = null
@@ -140,6 +145,7 @@ class SessionTerminalPanel(context: Context) : LinearLayout(context) {
override fun onSessionFinished(finishedSession: TerminalSession) {
val pid = terminalPid.takeIf { it > 1 } ?: finishedSession.pid
if (pid > 1) SessionService.terminalProcessExited(context, pid)
SessionTerminal.release(finishedSession)
terminalPid = -1
terminalView?.onScreenUpdated()
}