Skip to content

Native Android client for Spotify, built with Jetpack Compose

Extensions: a call its caller gave up on (a skip, a newer song) is stopped too: it kept the extension's only thread, and the next song's audio lookup waited behind it until its own time ran out (a test: the next call answers at once)

kota-zo13d41d2fd8dc
diff --git a/base/extensions/src/main/kotlin/app/myspoti/extensions/ExtensionRuntime.kt b/base/extensions/src/main/kotlin/app/myspoti/extensions/ExtensionRuntime.kt
index 43426350..b1594649 100644
--- a/base/extensions/src/main/kotlin/app/myspoti/extensions/ExtensionRuntime.kt
+++ b/base/extensions/src/main/kotlin/app/myspoti/extensions/ExtensionRuntime.kt
@@ -261,6 +261,13 @@ class ExtensionRuntime(
try { return task.get(10, TimeUnit.MILLISECONDS) } catch (_: TimeoutException) { /* not its turn yet */ }
}
await(task, timeoutMs, began.get(), pause)
+ } catch (e: InterruptedException) {
+ // The caller gave up (a skip, a newer song): the call is stopped as well, or it kept the
+ // extension's only thread and the next song's lookup waited behind it. Running, it ends
+ // at the instruction observer's next look (it sees the interrupt) or at its next wait.
+ task.cancel(true)
+ Thread.currentThread().interrupt()
+ throw e
} catch (e: TimeoutException) {
task.cancel(true)
fail("took longer than $timeoutMs ms", e, timeout = true)
@@ -727,7 +734,8 @@ class ExtensionRuntime(
override fun observeInstructionCount(cx: Context, instructionCount: Int) {
val runtime = RUNNING.get() ?: return
- if (System.nanoTime() > runtime.deadline) throw TimeLimit()
+ // Interrupted: its caller gave up and cancelled it (see run).
+ if (System.nanoTime() > runtime.deadline || Thread.currentThread().isInterrupted) throw TimeLimit()
}
}
diff --git a/base/extensions/src/test/kotlin/app/myspoti/extensions/SandboxTest.kt b/base/extensions/src/test/kotlin/app/myspoti/extensions/SandboxTest.kt
index 682f7f49..ff77f09a 100644
--- a/base/extensions/src/test/kotlin/app/myspoti/extensions/SandboxTest.kt
+++ b/base/extensions/src/test/kotlin/app/myspoti/extensions/SandboxTest.kt
@@ -116,6 +116,21 @@ class SandboxTest {
rt.close()
}
+ @Test fun aCallItsCallerGaveUpOnFreesTheThreadForTheNext() {
+ // The first call never ends by itself; its caller is interrupted (a skip) long before its time.
+ val rt = runtime("let n = 0; myspoti.provide('lyrics', function () { if (n++ === 0) { while (true) {} } return { v: n }; });")
+ val caller = Thread { runCatching { rt.call(Hook.LYRICS, track.toJson(), 20_000) } }
+ caller.start()
+ Thread.sleep(300)
+ caller.interrupt()
+ caller.join(2_000)
+ val started = System.currentTimeMillis()
+ val answer = rt.call(Hook.LYRICS, track.toJson(), 5_000)
+ assertTrue("the next call waited behind the abandoned one", System.currentTimeMillis() - started < 2_000)
+ assertTrue(answer!!.contains("2"))
+ rt.close()
+ }
+
@Test fun aCallCutShortByCloseIsNotTheScriptsFailure() {
val rt = runtime("myspoti.provide('lyrics', function () { while (true) {} });")
Thread { Thread.sleep(200); rt.close() }.start()