Skip to content

Debloat your Android TV, install apps, browse and transfer files on it, all from your phone. Just 3 MB, no tracking, no root, no PC.

DeviceLink: a cancelled loop no longer clears the new session

jesse-agent-2327d9de2c28470
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d9bb779..d0faa8b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@ until this file has a section for its version.
- **New:** The Remote has a touchpad: swipe to move, tap for OK. Switch between it and the D-pad in the Remote's ⋮ menu. [#312](../../pull/312)
- **Improved:** On the Remote, the text box stays hidden until you tap the keyboard key, which leaves more room for the keys. [#312](../../pull/312)
+- **Fixed:** Reconnecting to a TV, or installing right after the file picker, no longer leaves every action saying the TV is offline. [#312](../../pull/312)
- **Fixed:** Off Wi-Fi, the TV tabs and failed sends blame the phone, not the TV, and Reconnect on Overview opens the Wi-Fi panel. [#313](../../pull/313)
- **Fixed:** On a Fire TV or another HDMI box whose volume is fixed, the Remote's mute and volume keys are grayed out, with a line saying to use the TV's own remote. [#307](../../pull/307)
- **Fixed:** On the Remote, a perfectly still tap on empty space now closes the keyboard. [#300](../../pull/300)
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/DeviceLink.kt b/adb/src/main/kotlin/app/tellyscope/adb/DeviceLink.kt
index 8a9a7a6..b73539b 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/DeviceLink.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/DeviceLink.kt
@@ -9,6 +9,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
+import java.util.concurrent.atomic.AtomicReference
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
@@ -42,8 +43,8 @@ class DeviceLink(
/** Pauses the stats while a change runs on this TV, and sets their refresh rates. */
val gate = StatsGate()
- @Volatile var session: AdbSession? = null
- private set
+ private val current = AtomicReference<AdbSession?>(null)
+ val session: AdbSession? get() = current.get()
@Volatile var lastSeenMillis: Long? = null
private set
@@ -67,7 +68,7 @@ class DeviceLink(
*/
fun connect(asked: Boolean = false) {
job?.cancel()
- session = null
+ current.set(null)
if (asked) allowedBefore = false
// Set here, not in the loop: nobody may see the old state once connect has returned.
_state.value = _state.value.on(DeviceEvent.Connect)
@@ -78,7 +79,7 @@ class DeviceLink(
fun holdLoggedOut(at: Long) {
job?.cancel()
job = null
- session = null
+ current.set(null)
allowedBefore = false
_loggedOutAt.value = at
_state.value = DeviceState.NOT_ALLOWED
@@ -118,7 +119,7 @@ class DeviceLink(
/** Streams stats until the link drops. The session is closed on every exit, cancellation included. */
private suspend fun online(s: AdbSession) {
- session = s
+ current.set(s)
_state.value = _state.value.on(DeviceEvent.Authorized)
try {
monitorFor(s, gate).stats().collect {
@@ -130,7 +131,8 @@ class DeviceLink(
} catch (_: Exception) {
// Link dropped mid-read: the caller marks it offline and retries.
} finally {
- session = null
+ // A cancelled loop gets here after connect has started over: leave the new session alone.
+ current.compareAndSet(s, null)
s.close()
}
}
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/DeviceLinkTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/DeviceLinkTest.kt
index 899ded5..74c0f06 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/DeviceLinkTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/DeviceLinkTest.kt
@@ -2,11 +2,14 @@
package app.tellyscope.adb
import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.NonCancellable
+import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
+import kotlinx.coroutines.withContext
import java.io.IOException
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -153,6 +156,27 @@ class DeviceLinkTest {
assertTrue(link.session != null && link.session != first)
}
+ @Test fun theOldLoopStoppingLateLeavesTheNewSessionAlone() = runTest {
+ // The cancelled loop's cleanup can run after the new one is online, as on a real thread pool (#290).
+ val second = FakeShell()
+ var opens = 0
+ val link = DeviceLink("tv", 5555, backgroundScope,
+ monitorFor = { _, _ -> if (opens == 1) slowToStop() else fakeMonitor(failFirst = false) },
+ open = { _, _ -> ConnectResult.Online(if (opens++ == 0) FakeShell() else second) })
+ link.connect(); runCurrent()
+ link.connect(); runCurrent()
+ assertEquals(second, link.session)
+ advanceTimeBy(1_001)
+ assertEquals(second, link.session)
+ }
+
+ private fun slowToStop() = MonitorSource {
+ flow {
+ emit(reading)
+ try { awaitCancellation() } finally { withContext(NonCancellable) { delay(1_000) } }
+ }
+ }
+
private fun fakeMonitor(failFirst: Boolean) = object : MonitorSource {
override fun stats() = flow {
emit(reading)