From 688b99b2bbf0cbaae6cd89345b3579734535da47 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 30 Oct 2026 11:51:45 +0000 Subject: [PATCH 1/5] ByteQueue: a chunk sent as a timed read runs out is no longer lost poll(ms) waited with withTimeoutOrNull { ch.receive() }. When a chunk arrived just as the timeout fired, the cancelled receive had already taken it from the channel, and kotlinx.coroutines drops an element delivered to a cancelled receive. Readers that poll in short waits (the fake fax line's modems, TransferIo under every job, the PPP loopback's pumps) lost bytes now and then: a lost AT command or frame became a 4-10 s stall, and on slower CI runners a failed test (EcmTest, JobsTest.dialUpInternet on iOS, PppLoopbackTest). The wait is now a select of the receive and a timeout, which can't both win, so the chunk stays in the channel when the timeout does. The new ByteQueueTest loses a chunk within the first hundred on the old code. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRSgB6M9Vp9dEP1haEE --- .../kotlin/com/neodesktop/dmodem/io/Io.kt | 18 +++++++-- .../com/neodesktop/dmodem/io/ByteQueueTest.kt | 37 +++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 shared/src/commonTest/kotlin/com/neodesktop/dmodem/io/ByteQueueTest.kt diff --git a/shared/src/commonMain/kotlin/com/neodesktop/dmodem/io/Io.kt b/shared/src/commonMain/kotlin/com/neodesktop/dmodem/io/Io.kt index ce8469b..f829973 100644 --- a/shared/src/commonMain/kotlin/com/neodesktop/dmodem/io/Io.kt +++ b/shared/src/commonMain/kotlin/com/neodesktop/dmodem/io/Io.kt @@ -2,10 +2,12 @@ // Copyright (C) 2026 Neo-Desktop package com.neodesktop.dmodem.io +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.withTimeoutOrNull +import kotlinx.coroutines.selects.onTimeout +import kotlinx.coroutines.selects.select import kotlin.time.TimeSource /** @@ -84,11 +86,21 @@ class ByteQueue { ch.trySend(b) } - /** the next chunk, waiting up to [ms]; null if none came (the reader's thread) */ + /** + * the next chunk, waiting up to [ms]; null if none came (the reader's + * thread). (A select, not receive() in withTimeoutOrNull: a chunk sent + * just as that timed out was taken by the cancelled receive and lost.) + */ + @OptIn(ExperimentalCoroutinesApi::class) fun poll(ms: Long): ByteArray? { poll()?.let { return it } if (ms <= 0) return null - return runBlocking { withTimeoutOrNull(ms) { ch.receive() } } + return runBlocking { + select { + ch.onReceive { it } + onTimeout(ms) { null } + } + } } /** the next chunk if one waits, else null */ diff --git a/shared/src/commonTest/kotlin/com/neodesktop/dmodem/io/ByteQueueTest.kt b/shared/src/commonTest/kotlin/com/neodesktop/dmodem/io/ByteQueueTest.kt new file mode 100644 index 0000000..f3e6fe2 --- /dev/null +++ b/shared/src/commonTest/kotlin/com/neodesktop/dmodem/io/ByteQueueTest.kt @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package com.neodesktop.dmodem.io + +import kotlin.concurrent.Volatile +import kotlin.test.Test +import kotlin.test.assertEquals + +/** A ByteQueue between two threads loses nothing, whatever its reads' timeouts. */ +class ByteQueueTest { + private class Flag { @Volatile var on = false } + + /** chunks put while the reader's short waits keep timing out all come, in order */ + @Test + fun nothingLostToATimeout() { + val q = ByteQueue() + val n = 3000 + val done = Flag() + background("writer") { + for (i in 0 until n) { + q.put(byteArrayOf((i shr 8).toByte(), i.toByte())) + // (now and then just as the reader's wait runs out) + if (i % 3 == 0) sleepMs(1) + } + done.on = true + } + var next = 0 + val end = monotonicMs() + 30_000 + while (next < n && monotonicMs() < end) { + val b = q.poll(1) ?: continue + val i = ((b[0].toInt() and 0xff) shl 8) or (b[1].toInt() and 0xff) + assertEquals(next, i, "chunk $next") + next++ + } + assertEquals(n, next) + } +} From acf2a05ecbe335e5b31ee7c522aaa33708b08928 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 30 Oct 2026 11:51:45 +0000 Subject: [PATCH 2/5] NativeModemTest: SIP's log at level 4, so a line comes before the test ends The test looks for a "sip: " line through the log hook. Since dmodemd's SIP moved to libre, nothing at level 3 is said without a call or a registrar's answer, and the test stops the modem within a few ms, so the emulator job failed on main. At level 4 the SIP thread names its addresses ("SIP on ...") before sip::start returns. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRSgB6M9Vp9dEP1haEE --- .../java/com/neodesktop/dmodem/NativeModemTest.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/src/androidTest/java/com/neodesktop/dmodem/NativeModemTest.kt b/app/src/androidTest/java/com/neodesktop/dmodem/NativeModemTest.kt index dcf44fd..62a935c 100644 --- a/app/src/androidTest/java/com/neodesktop/dmodem/NativeModemTest.kt +++ b/app/src/androidTest/java/com/neodesktop/dmodem/NativeModemTest.kt @@ -37,10 +37,15 @@ class NativeModemTest { NativeModem.listener = null } - /** a SIP account at nowhere (no call is made), the modem's data in the app's files */ + /** + * a SIP account at nowhere (no call is made), the modem's data in the + * app's files; SIP's log at level 4, which names its addresses + * ("SIP on ...") before start returns: at 3 a modem with no call and + * no registrar has nothing to say in the test's time + */ private fun config() = NativeModem.Config( user = "test", password = "x", domain = "127.0.0.1:5999", sipPort = 15060, - dns = listOf("127.0.0.1"), logLevel = 1, sipLogLevel = 3, + dns = listOf("127.0.0.1"), logLevel = 1, sipLogLevel = 4, dataFile = File(InstrumentationRegistry.getInstrumentation().targetContext.filesDir, "test_dsp").path, ) From fe6ab528f17f062cf359bd1ee5dca08dfe4ca3d4 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 30 Oct 2026 11:58:35 +0000 Subject: [PATCH 3/5] iOS engine build: #4's configure and link fixes, as they are there The Apple job now gets past the shared Kotlin tests and stops where #4 fixed it: CMAKE_MACOSX_BUNDLE=OFF for dmodemd's install rule, and libresolv, SystemConfiguration and CoreFoundation for libre. The files are #4's, unchanged, so the two merge cleanly in either order. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRSgB6M9Vp9dEP1haEE --- ios/DModemKit/Package.swift | 8 ++++++-- ios/DModemSmoke/Package.swift | 6 +++++- ios/build-native.sh | 5 ++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/ios/DModemKit/Package.swift b/ios/DModemKit/Package.swift index 4f46149..54381d7 100644 --- a/ios/DModemKit/Package.swift +++ b/ios/DModemKit/Package.swift @@ -20,11 +20,15 @@ let engineLinking: [LinkerSetting] = [ ]), .linkedLibrary("dmodem_engine"), .linkedLibrary("dmodem_sip"), .linkedLibrary("re"), .linkedLibrary("ssl"), .linkedLibrary("crypto"), .linkedLibrary("stdc++"), .linkedLibrary("dl"), - .linkedLibrary("m"), + .linkedLibrary("m"), .linkedLibrary("resolv"), ] #else let engine: Target = .binaryTarget(name: "DModemEngine", path: "../../build-apple/DModemEngine.xcframework") -let engineLinking: [LinkerSetting] = [.linkedLibrary("c++")] +let engineLinking: [LinkerSetting] = [ + .linkedLibrary("c++"), .linkedLibrary("resolv"), + // libre asks the system for its DNS servers (srv.c, SCDynamicStore) + .linkedFramework("SystemConfiguration"), .linkedFramework("CoreFoundation"), +] #endif let package = Package( diff --git a/ios/DModemSmoke/Package.swift b/ios/DModemSmoke/Package.swift index 9039447..c6934f9 100644 --- a/ios/DModemSmoke/Package.swift +++ b/ios/DModemSmoke/Package.swift @@ -14,7 +14,11 @@ let package = Package( .testTarget( name: "DModemSmokeTests", dependencies: ["DModemEngine"], - linkerSettings: [.linkedLibrary("c++")] + linkerSettings: [ + .linkedLibrary("c++"), .linkedLibrary("resolv"), + // libre asks macOS for its DNS servers (srv.c, SCDynamicStore) + .linkedFramework("SystemConfiguration"), .linkedFramework("CoreFoundation"), + ] ), ] ) diff --git a/ios/build-native.sh b/ios/build-native.sh index 476a62e..3c157fb 100755 --- a/ios/build-native.sh +++ b/ios/build-native.sh @@ -32,7 +32,10 @@ build() { -DCMAKE_OSX_DEPLOYMENT_TARGET="$min" \ -DDMODEM_CRYPTO=mbedtls if [ "$system" = iOS ]; then - set -- "$@" -DCMAKE_SYSTEM_NAME=iOS + # (no app bundles: D-Modem-RE's dmodemd, which is never built + # here, is an executable that CMake would make a bundle on iOS, + # and its install rule has no BUNDLE DESTINATION for one) + set -- "$@" -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_MACOSX_BUNDLE=OFF fi cmake "$@" cmake --build "$dir/build" --target dmodem_engine From 299daabfb1d37a06d02a16055f66f61cf4b225c7 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 30 Oct 2026 12:20:37 +0000 Subject: [PATCH 4/5] PeerLoopbackTest: the fake pipe delivers what was written before its close A phone turning down a wrong PIN writes DENIED and closes. The test's pipe returned -1 as soon as it saw the close, before reading what was already queued, so the caller now and then missed DENIED, took the call as unanswered (503) and aPinToCall waited out its 15 s for a 403. A socket delivers the data before the end; the fake now does too. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRSgB6M9Vp9dEP1haEE --- .../kotlin/com/neodesktop/dmodem/peer/PeerLoopbackTest.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shared/src/commonTest/kotlin/com/neodesktop/dmodem/peer/PeerLoopbackTest.kt b/shared/src/commonTest/kotlin/com/neodesktop/dmodem/peer/PeerLoopbackTest.kt index 214b39c..13ccd1a 100644 --- a/shared/src/commonTest/kotlin/com/neodesktop/dmodem/peer/PeerLoopbackTest.kt +++ b/shared/src/commonTest/kotlin/com/neodesktop/dmodem/peer/PeerLoopbackTest.kt @@ -34,11 +34,11 @@ class PeerLoopbackTest { /** a pipe's end: reads [inw], writes [outw] */ private class End(private val inw: Way, private val outw: Way, override val name: String) : Duplex { override val input = object : Source { - /** the next of what came the other way, into [b] (waited for): how much, -1 once closed */ + /** the next of what came the other way, into [b] (waited for): how much, -1 once closed and read to its end */ override fun read(b: ByteArray, off: Int, len: Int): Int { while (inw.at >= inw.cur.size) { - if (inw.closed) return -1 - inw.cur = inw.q.poll(50) ?: continue + // (what was written before the close still comes, as on a socket: a DENIED, say) + inw.cur = inw.q.poll() ?: if (inw.closed) return -1 else inw.q.poll(50) ?: continue inw.at = 0 } val n = minOf(len, inw.cur.size - inw.at) From 11861248eb8e78557b705d7114440814f76a0768 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 30 Oct 2026 12:21:12 +0000 Subject: [PATCH 5/5] Apple CI: the iOS app built for arm64 simulators only, as #4 has it The job now gets as far as the app, whose x86_64 simulator link fails: the shared Kotlin's XCFramework has no x86_64 simulator slice. #4's apple.yml, unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRSgB6M9Vp9dEP1haEE --- .github/workflows/apple.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/apple.yml b/.github/workflows/apple.yml index 5a387e1..38c1ccd 100644 --- a/.github/workflows/apple.yml +++ b/.github/workflows/apple.yml @@ -49,13 +49,14 @@ jobs: - name: The shared Kotlin as an XCFramework run: ./gradlew :shared:assembleDModemSharedReleaseXCFramework + # arm64 only: the shared Kotlin's XCFramework has no x86_64 simulator slice - name: The iOS app (simulator) run: | brew install xcodegen cd ios/DModem && xcodegen generate xcodebuild -project DModem.xcodeproj -scheme DModem \ -destination 'generic/platform=iOS Simulator' \ - CODE_SIGNING_ALLOWED=NO build + ARCHS=arm64 CODE_SIGNING_ALLOWED=NO build - uses: actions/upload-artifact@v4 with: