fix(flatpak): keep the session occupied until a late guest launch is torn down

This commit is contained in:
Kurt Himebauch committed 2026-11-17 12:30:08 +00:00
1 parent 9f310d672d
commit 96c7a8cdda
4 files changed
+118 -13

No files matched your search

@@ -46,8 +46,6 @@ object FlatpakData {
private fun cardIn(storage: File, id: String) = File(File(SecondaryLibrary.contentPath(storage), DIR), id)
internal fun cardDir(context: Context, id: String): File? = storage(context, id)?.let { cardIn(it, id) }
internal fun internalDir(context: Context, id: String) = File(LinuxRuntime.rootDir(context), "$HOST/$id")
fun binds(context: Context, writable: (File) -> Boolean = { it.canWrite() }): List<String> = synchronized(lock) {
@@ -0,0 +1,17 @@
package com.droiddeck.launcher.session
internal class GuestLaunchGate {
private val lock = Object()
private var pending = 0
fun begin() = synchronized(lock) { pending++ }
fun end() = synchronized(lock) {
pending--
lock.notifyAll()
}
fun await() = synchronized(lock) {
while (pending > 0) lock.wait()
}
}
@@ -67,6 +67,7 @@ class SessionService : Service() {
private var wakeLock: PowerManager.WakeLock? = null
private var wifiLock: WifiManager.WifiLock? = null
private var sessionPid = -1
private val guestLaunch = GuestLaunchGate()
private val auxiliaryProcesses = HashMap<Int, Long>()
private val auxiliaryProcessesLock = Any()
private val mainHandler = Handler(Looper.getMainLooper())
@@ -820,17 +821,38 @@ class SessionService : Service() {
watchLaunchRequests(sessionRoot)
watchSyncWanted(root)
EsyncPacks.fetchInBackground(this, root)
val pid = SessionEvents.step("guest-launch") { HostProcess.start(line, hostEnv.asArray(), root, { status ->
if (gen != sessionGen) {
Log.i(TAG, "an earlier session's process ended ($status); the current one carries on")
return@start
val launchHeld = synchronized(stopLock) {
(gen == sessionGen && SessionState.running).also { if (it) guestLaunch.begin() }
}
if (!launchHeld) {
Log.i(TAG, "session stopped while it was starting; not launching it")
if (gen == sessionGen) {
launchWatcher?.stopWatching()
launchWatcher = null
syncWatcher?.stopWatching()
syncWatcher = null
}
val program = programStatus(sessionRoot)
Log.i(TAG, "session ended: $status" + if (program != null) " (program $program)" else "")
stopSession(program ?: status ?: -1, gen)
}, null, sessionLog.takeIf { "PROOT_PROFILE=1" in hostEnv.asArray() }) }
return
}
val pid = try {
SessionEvents.step("guest-launch") { HostProcess.start(line, hostEnv.asArray(), root, { status ->
if (gen != sessionGen) {
Log.i(TAG, "an earlier session's process ended ($status); the current one carries on")
return@start
}
val program = programStatus(sessionRoot)
Log.i(TAG, "session ended: $status" + if (program != null) " (program $program)" else "")
stopSession(program ?: status ?: -1, gen)
}, null, sessionLog.takeIf { "PROOT_PROFILE=1" in hostEnv.asArray() }) }
} catch (e: Throwable) {
guestLaunch.end()
throw e
}
Log.i(TAG, "session pid $pid, log ${sessionLog.path}")
if (gen != sessionGen || !SessionState.running) {
val stoppedDuringLaunch = synchronized(stopLock) {
(gen != sessionGen || !SessionState.running).also { if (!it) sessionPid = pid }
}
if (stoppedDuringLaunch) {
Log.i(TAG, "session stopped while its guest was starting; taking it down")
if (gen == sessionGen) {
launchWatcher?.stopWatching()
@@ -840,10 +862,16 @@ class SessionService : Service() {
components.reversed().forEach { runCatching { it.stop() } }
components.clear()
}
if (pid > 1) Thread({ teardown(pid) }, "session-teardown").start()
if (pid > 1) {
Thread({
try { teardown(pid) } finally { guestLaunch.end() }
}, "session-teardown").start()
} else {
guestLaunch.end()
}
return
}
sessionPid = pid
guestLaunch.end()
if (pid > 1) {
raiseTracer(pid, gen)
SessionEvents.guestStarted(pid)
@@ -2156,6 +2184,7 @@ class SessionService : Service() {
}
private fun finishSessionStop(status: Int, stoppedGen: Int, afterStopped: () -> Unit = {}) {
guestLaunch.await()
mainHandler.post {
try {
if (stoppedGen != sessionGen || SessionState.running) {
@@ -0,0 +1,61 @@
package com.droiddeck.launcher.session
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class GuestLaunchGateTest {
private fun finishingThread(gate: GuestLaunchGate, finished: AtomicBoolean, done: CountDownLatch) =
Thread {
gate.await()
finished.set(true)
done.countDown()
}.apply { start() }
@Test fun stopFinishesImmediatelyWhenNoLaunchIsPending() {
val finished = AtomicBoolean()
val done = CountDownLatch(1)
finishingThread(GuestLaunchGate(), finished, done)
assertTrue(done.await(5, TimeUnit.SECONDS))
assertTrue(finished.get())
}
@Test fun stopDuringGuestStartWaitsForTheLateGuestTeardown() {
val gate = GuestLaunchGate()
val finished = AtomicBoolean()
val done = CountDownLatch(1)
gate.begin()
finishingThread(gate, finished, done)
assertFalse(done.await(300, TimeUnit.MILLISECONDS))
assertFalse(finished.get())
gate.end()
assertTrue(done.await(5, TimeUnit.SECONDS))
assertTrue(finished.get())
}
@Test fun stopWaitsForEveryPendingLaunch() {
val gate = GuestLaunchGate()
val finished = AtomicBoolean()
val done = CountDownLatch(1)
gate.begin()
gate.begin()
finishingThread(gate, finished, done)
gate.end()
assertFalse(done.await(300, TimeUnit.MILLISECONDS))
gate.end()
assertTrue(done.await(5, TimeUnit.SECONDS))
}
@Test fun aLaunchThatEndedBeforeTheStopDoesNotBlockIt() {
val gate = GuestLaunchGate()
gate.begin()
gate.end()
val finished = AtomicBoolean()
val done = CountDownLatch(1)
finishingThread(gate, finished, done)
assertTrue(done.await(5, TimeUnit.SECONDS))
}
}