mirror of
https://github.com/Monkopedia/rouse-context.git
synced 2026-12-03 03:44:54 +00:00
The bridge jvmTest suite hung indefinitely (and had done so for multiple
days locally) because of two reentrant runBlocking patterns:
1. TlsClientInputStream / TlsClientOutputStream in TestCertHelper wrapped
the suspending ChannelMuxStream API in a `runBlocking { ... }` every
read/write. Because the test body invokes these blocking JDK streams
from inside an outer `runBlocking`, the inner `runBlocking` pumped
the outer event loop and picked up pending tasks (including the
server-side SessionHandler continuation), which then called Ktor's
CIOApplicationEngine.start -- itself a `runBlockingBridge` -- on the
same thread, producing a 3-deep nested runBlocking that deadlocked.
2. TunnelSessionManager launched each incoming session on the caller's
dispatcher. In tests that is the `runBlocking` event loop, so any
internal `runBlocking` the session did landed back on the same
thread. In production that dispatcher would typically be the Android
main thread; either way, Ktor's start-up bridge is not safe to run
on the same thread that is driving the outer coroutine.
Fixes:
* TunnelSessionManager: dispatch `handleStream` onto `Dispatchers.IO`
so blocking socket I/O and Ktor start-up bridges never run on the
caller's thread. This is also a production correctness fix -- the
previous behaviour could wedge whatever dispatcher the service scope
used on Android.
* TestCertHelper: bridge the MuxStream suspend API to blocking queues
via a dedicated inbound/outbound pump thread per client session. The
pumps own their own `runBlocking`s on threads that are not the test
thread, so the test's outer event loop is never reentered.
* Extract the TLS client handshake loop and pump setup into helpers to
keep detekt's weighted-issue count at baseline.
Safety net:
* Add a JUnit 4 `Timeout` rule (30 s) to every class in the bridge
jvmTest source set, so a future regression fails fast instead of
burning the 20-minute CI budget.
The full suite (15 tests) now completes in ~20 s and is stable across
repeated `--rerun-tasks` runs.
Co-authored-by: Claude Opus 4 (1M context) <noreply@anthropic.com>
|
||
|---|---|---|
| .. | ||
| src | ||
| build.gradle.kts | ||