Modern Apps git

← Modern-Apps.git

communicate: make the direct-SIP stack restartable and its MESSAGE heads well-formed

Commit
72144e5a3216cab506698a8d753ad9a08585f149
Author
Vayun Mathur <[email protected]>
When
2026-11-07 08:24 UTC (29 days ago)
Parents
e0979dd
stop() cancelled the process-wide coroutine scope. The reader and
refresh loops launch on that scope, so after any teardown a later
start() wrote its REGISTER and then waited out the full transact
timeout with nobody reading responses - the leg could never come
back in the same process. Cancel only the scope's children; the
loops also exit on running=false / socket close.

rebuildMessageHeaders kept the trailing empty line of the caller's
CRLF-terminated header block, which ended the SIP head early and
pushed the rebuilt Via/From/Route into the body of every direct-leg
MESSAGE. Blank lines are now filtered with the replaced headers.

Found by RcsSlice3Pass2ImsTest / RcsSlice3Pass2DirectSipAuthTest,
which register against a loopback P-CSCF, exchange MESSAGEs in both
directions, deregister and restart; both halves failed before.

(cherry picked from commit a7f2b982dcf8739e397b934195ad58cb4a3fc6f5)

Diff

communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDirectSip.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDirectSip.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDirectSip.ktindex c158f9bca0..4bdb84f890 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDirectSip.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDirectSip.kt@@ -16,7 +16,7 @@ import kotlin.coroutines.resume import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob-import kotlinx.coroutines.cancel+import kotlinx.coroutines.cancelChildren import kotlinx.coroutines.launch import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withTimeoutOrNull@@ -152,7 +152,11 @@ object RcsDirectSip {         pcscf = null         pendingSends.values.forEach { runCatching { it(false) } }         pendingSends.clear()-        scope.coroutineContext.cancel()+        // Cancel children only, never the scope itself: cancelling the+        // process-wide scope would leave every later start() without a+        // reader loop (transact writes, then waits out its full timeout),+        // making the stack un-restartable after any teardown.+        scope.coroutineContext.cancelChildren()     }      /** True when REGISTERed with a live socket. */@@ -508,7 +512,11 @@ object RcsDirectSip {         val from = publicIdentity.ifBlank { "sip:[email protected]" }         val filtered = headers.lineSequence().filter { line ->             val t = line.trim()-            !(t.startsWith("Via:", ignoreCase = true) ||+            // Blank lines must go too: callers pass a header block ending in+            // CRLF, and keeping its trailing empty line would end the head+            // here, pushing the Via/From/Route below into the body.+            !(t.isBlank() ||+                t.startsWith("Via:", ignoreCase = true) ||                 t.startsWith("From:", ignoreCase = true) ||                 t.startsWith("Route:", ignoreCase = true))         }.joinToString("\r\n")