From 74b4c2ef099b4d2fe34997049b45fabe68258150 Mon Sep 17 00:00:00 2001 From: The412Banner Date: Tue, 22 Sep 2026 11:15:35 -0400 Subject: [PATCH] A replaced session's exit no longer ends the one that replaced it Hand-off from the desktop: the new session started, and a second later the old proot - killed by its own teardown - reported its exit, which the service took as the current session ending ("The session ended 137"). Exits are matched to the session that started the process. The same race took the new session's log capture and its folder (stopFor / release by folder), and a finishing activity unhooked the replacement's end listener (compared by identity now). --- .../com/steamdeck/launcher/SessionActivity.kt | 7 +++++-- .../launcher/core/SessionLogCapture.kt | 10 ++++++++++ .../steamdeck/launcher/session/SessionPaths.kt | 6 ++++-- .../launcher/session/SessionService.kt | 18 +++++++++++++++--- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/steamdeck/launcher/SessionActivity.kt b/app/src/main/java/com/steamdeck/launcher/SessionActivity.kt index d599a3ffd9..1f483e2a12 100644 --- a/app/src/main/java/com/steamdeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/steamdeck/launcher/SessionActivity.kt @@ -234,7 +234,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { hud.start() } } - SessionState.endListener = { status -> onSessionEnded(status) } + SessionState.endListener = endListener // Back opens the drawer (and closes it again). Leaving the session running in the // background and ending it are both actions in there, so neither can happen by accident // from a button a game might also be reading. @@ -724,13 +724,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } } + /** Ours, so a finishing activity never unhooks the one that replaced it. */ + private val endListener: (Int) -> Unit = { status -> onSessionEnded(status) } + override fun onDestroy() { // Deliberately does NOT end the session: this activity can be destroyed while the user is // in another app, and the whole point of the service is that Steam survives that. watching = false if (::hud.isInitialized) hud.stop() padBridge?.stop() - SessionState.endListener = null + if (SessionState.endListener === endListener) SessionState.endListener = null WaylandCompositor.setFirstFrameListener(null) super.onDestroy() } diff --git a/app/src/main/java/com/steamdeck/launcher/core/SessionLogCapture.kt b/app/src/main/java/com/steamdeck/launcher/core/SessionLogCapture.kt index 6210d36254..e76a66edd1 100644 --- a/app/src/main/java/com/steamdeck/launcher/core/SessionLogCapture.kt +++ b/app/src/main/java/com/steamdeck/launcher/core/SessionLogCapture.kt @@ -28,6 +28,7 @@ object SessionLogCapture { private var pid = -1 private var writer: BufferedWriter? = null + private var target: File? = null /** Start mirroring this process's log lines into [target]. Safe to call twice. */ @Synchronized @@ -39,6 +40,7 @@ object SessionLogCapture { out.write("Lines the app wrote before the session folder existed are in logcat only.\n\n") out.flush() writer = out + this.target = target // -T 1 starts at the newest line rather than replaying the whole buffer; -v threadtime // keeps the timestamps and thread ids that make two logs line up. pid = ProcessHelper.exec( @@ -63,8 +65,16 @@ object SessionLogCapture { } } + /** Stops the capture only if it is the one writing into [dir]: a newer session's is left alone. */ + @Synchronized + fun stopFor(dir: File) { + val t = target ?: return + if (t.parentFile?.absolutePath == dir.absolutePath) stop() + } + @Synchronized fun stop() { + target = null if (pid != -1) { try { Process.killProcess(pid) diff --git a/app/src/main/java/com/steamdeck/launcher/session/SessionPaths.kt b/app/src/main/java/com/steamdeck/launcher/session/SessionPaths.kt index 7e8673ff17..9f5f1518ae 100644 --- a/app/src/main/java/com/steamdeck/launcher/session/SessionPaths.kt +++ b/app/src/main/java/com/steamdeck/launcher/session/SessionPaths.kt @@ -56,8 +56,10 @@ object SessionPaths { /** Let the next session claim a new folder. Called when a session ends. */ @Synchronized - fun release(context: Context) { - val ended = dir + fun release(context: Context, ended: File) { + // Only the session that owns the folder lets go of it: the next session may have claimed + // its own by the time the last one's collecting thread gets here. + if (dir != ended) return dir = null // A folder kept in the cache was never meant to outlive its session. if (ended != null && ended.path.startsWith(context.cacheDir.path)) { diff --git a/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt b/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt index aac6af793d..42e4ff12e3 100644 --- a/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt @@ -56,6 +56,8 @@ class SessionService : Service() { private var wakeLock: PowerManager.WakeLock? = null private var wifiLock: WifiManager.WifiLock? = null private var sessionPid = -1 + /** Counts sessions this service has started; a process exit from an earlier one is ignored. */ + private var sessionGen = 0 override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { if (intent?.action == ACTION_STOP) { @@ -98,9 +100,10 @@ class SessionService : Service() { try { SessionArtifacts.collect(this, dir, "session stopped") } finally { - // Last, so everything above is in the file it is about. - SessionLogCapture.stop() - SessionPaths.release(this) + // Last, so everything above is in the file it is about - and only this session's: + // a session that replaced this one may already own the capture and the folder. + SessionLogCapture.stopFor(dir) + SessionPaths.release(this, dir) } } @@ -387,7 +390,16 @@ class SessionService : Service() { val line = command.joinToString(" ") { it.replace(" ", "\\ ") } watchLaunchRequests(sessionRoot) + // One session replacing another (the desktop's Steam launchers): the old proot is killed + // by the teardown a second after the new one has started, and its exit used to arrive + // here as "session ended: 137" and end the NEW session. An exit belongs to the session + // that started it. + val gen = ++sessionGen sessionPid = ProcessHelper.exec(line, hostEnv.toStringArray(), root, { status -> + if (gen != sessionGen) { + Log.i(TAG, "an earlier session's process ended ($status); the current one carries on") + return@exec + } Log.i(TAG, "session ended: $status") stopSession(status ?: -1) }, null)