From 96b2f978732e058cf0d585aa111a2408da37877a Mon Sep 17 00:00:00 2001 From: Jason Monk Date: Tue, 14 Apr 2026 20:00:46 -0400 Subject: [PATCH] Fix #149: Rethrow CancellationException in FcmTokenRegistrar The broad catch(Exception) around sendFcmToken swallowed CancellationException (which inherits from IllegalStateException on JVM), breaking structured concurrency. Rethrow CancellationException before the general handler so callers tied to a cancellable scope observe cancellation correctly. Added a test that cancels the calling scope mid-send and asserts the CancellationException propagates out of registerToken. --- .../rousecontext/work/FcmTokenRegistrar.kt | 3 ++ .../work/FcmTokenRegistrarTest.kt | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/work/src/main/kotlin/com/rousecontext/work/FcmTokenRegistrar.kt b/work/src/main/kotlin/com/rousecontext/work/FcmTokenRegistrar.kt index f38be83c..72fb48af 100644 --- a/work/src/main/kotlin/com/rousecontext/work/FcmTokenRegistrar.kt +++ b/work/src/main/kotlin/com/rousecontext/work/FcmTokenRegistrar.kt @@ -3,6 +3,7 @@ package com.rousecontext.work import android.util.Log import com.rousecontext.tunnel.TunnelClient import com.rousecontext.tunnel.TunnelState +import kotlinx.coroutines.CancellationException /** * Forwards refreshed FCM tokens to the relay over the active tunnel connection. @@ -34,6 +35,8 @@ class FcmTokenRegistrar(private val tunnelClient: TunnelClient) { try { tunnelClient.sendFcmToken(token) Log.i(TAG, "Forwarded refreshed FCM token to relay") + } catch (e: CancellationException) { + throw e } catch (e: Exception) { Log.w(TAG, "Failed to push refreshed FCM token; next wake will pull", e) } diff --git a/work/src/test/kotlin/com/rousecontext/work/FcmTokenRegistrarTest.kt b/work/src/test/kotlin/com/rousecontext/work/FcmTokenRegistrarTest.kt index 22d66f57..01492f1e 100644 --- a/work/src/test/kotlin/com/rousecontext/work/FcmTokenRegistrarTest.kt +++ b/work/src/test/kotlin/com/rousecontext/work/FcmTokenRegistrarTest.kt @@ -8,8 +8,14 @@ import io.mockk.coVerify import io.mockk.every import io.mockk.just import io.mockk.mockk +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.Job import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner @@ -79,4 +85,41 @@ class FcmTokenRegistrarTest { coVerify(exactly = 1) { tunnelClient.sendFcmToken("token-abc") } } + + @Test + fun `registerToken propagates cancellation when scope is cancelled mid-send`() = runBlocking { + val tunnelClient = mockk() + every { tunnelClient.state } returns MutableStateFlow(TunnelState.CONNECTED) + val sendStarted = CompletableDeferred() + val neverCompletes = CompletableDeferred() + coEvery { tunnelClient.sendFcmToken(any()) } coAnswers { + sendStarted.complete(Unit) + // Suspend until cancelled — simulates a slow send that never returns. + neverCompletes.await() + } + + val registrar = FcmTokenRegistrar(tunnelClient) + val caught = CompletableDeferred() + val job: Job = launch { + try { + registrar.registerToken("token-abc") + caught.complete(IllegalStateException("registerToken returned normally")) + } catch (e: CancellationException) { + caught.complete(e) + throw e + } + } + + // Wait until sendFcmToken is actually suspended, then cancel. + sendStarted.await() + job.cancel() + job.join() + + val thrown = caught.await() + assertTrue( + "registerToken must propagate CancellationException, got ${thrown::class}", + thrown is CancellationException + ) + assertNotNull(thrown) + } }