mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
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).
This commit is contained in:
1 parent
593ef8fcaf
commit
74b4c2ef09
4 files changed
+34
-7
No files matched your search
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user