cast: close the proxy's client sockets so stopping it actually unwinds
- Commit
15836df42104f5b165636c77dab05f00660f7c61- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-24 00:21 UTC (3 months ago)
- Parents
ca253b8
Diff
cast/build.gradle.kts
diff --git a/cast/build.gradle.kts b/cast/build.gradle.ktsindex bef4fc3668..207579888d 100644--- a/cast/build.gradle.kts+++ b/cast/build.gradle.kts@@ -11,6 +11,11 @@ android { defaultConfig { applicationId = "com.vayunmathur.cast" }+ // MediaProxyServer is sockets and lifecycle, and every branch of it logs. Its teardown is worth+ // proving on the JVM, so android.util.Log has to answer rather than throw.+ testOptions {+ unitTests.isReturnDefaultValues = true+ } } dependencies {
cast/src/main/java/com/vayunmathur/cast/platform/MediaProxyServer.kt
diff --git a/cast/src/main/java/com/vayunmathur/cast/platform/MediaProxyServer.kt b/cast/src/main/java/com/vayunmathur/cast/platform/MediaProxyServer.ktindex 416420b51e..4bbbaf079c 100644--- a/cast/src/main/java/com/vayunmathur/cast/platform/MediaProxyServer.kt+++ b/cast/src/main/java/com/vayunmathur/cast/platform/MediaProxyServer.kt@@ -13,12 +13,15 @@ import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import java.net.InetAddress import java.net.NetworkInterface+import java.net.ServerSocket import java.net.Socket import java.security.SecureRandom import java.util.Base64+import java.util.Collections+import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.atomic.AtomicInteger-import javax.net.ssl.SSLServerSocket import javax.net.ssl.SSLSocket+import javax.net.ssl.SSLSocketFactory private const val TAG = "MediaProxyServer" @@ -54,6 +57,10 @@ private const val MAX_CONNECTIONS = 16 * Everything that decides *what* to answer lives in `:cast:protocol` * ([MediaProxyExchange], [EphemeralTls]), where it can be tested without a TV. What is left here * is sockets, threads and lifecycle.+ *+ * **Single-use: an instance that has been [stop]ped cannot be started again**, because [stop]+ * cancels [scope] and a later [start] would bind a port and then accept nothing. Every session+ * builds a fresh one, so this is a property worth stating rather than a bug worth guarding. */ class MediaProxyServer( private val token: String,@@ -64,7 +71,40 @@ class MediaProxyServer( private val exchange = MediaProxyExchange(token, resolver) private val connections = AtomicInteger(0) - private var server: SSLServerSocket? = null+ /**+ * A plain listening socket, with TLS layered on each connection after it is accepted.+ *+ * **Not an `SSLServerSocket`, and that is what makes [stop] work.** Closing an `SSLSocket` is not+ * a close, it is a *write*: the JDK sends a `close_notify` first, which takes the socket's write+ * lock - the very lock held by the thread parked in the body transfer this needs to interrupt. So+ * `stop()` deadlocked against the connection it was trying to end. Owning the underlying TCP+ * socket means the teardown can close *that*, which needs no lock and unblocks the writer at once.+ */+ private var server: ServerSocket? = null++ private var tls: SSLSocketFactory? = null++ /**+ * The connections currently being served, as their underlying TCP sockets, so [stop] can end them.+ *+ * **Closing the socket is the only thing that can end a serving thread**, and an unknown-length+ * body makes that thread's whole life one `write` call: once the TV's buffer is full it parks in+ * TCP backpressure for as long as the track lasts. Coroutine cancellation cannot interrupt a+ * thread blocked in socket I/O, and `soTimeout` is a *read* timeout that never applies to it -+ * so before this existed a session left a thread and a descriptor behind, [connections] never+ * came back down, and enough reconnects turned [MAX_CONNECTIONS] into a ratchet that refused to+ * serve at all.+ */+ private val live: MutableSet<Socket> = Collections.newSetFromMap(ConcurrentHashMap())++ /**+ * How many connections are currently being served.+ *+ * Exposed for the test that proves [stop] unwinds them. The ratchet this fixes is invisible from+ * outside - a leaked thread and descriptor look like nothing at all until the seventeenth+ * session is refused - so the count is what the test can actually assert on.+ */+ internal val openConnections: Int get() = connections.get() /** What the TV has to be told to be able to fetch anything. */ data class Endpoint(val port: Int, val certificateFingerprint: ByteArray) {@@ -85,9 +125,8 @@ class MediaProxyServer( if (server != null) return null return try { val credentials = EphemeralTls.server(addresses)- val socket = credentials.sslContext.serverSocketFactory- .createServerSocket(0, MAX_CONNECTIONS, InetAddress.getByName("0.0.0.0")) as SSLServerSocket- restrictProtocols(socket.supportedProtocols) { socket.enabledProtocols = it }+ tls = credentials.sslContext.socketFactory+ val socket = ServerSocket(0, MAX_CONNECTIONS, InetAddress.getByName("0.0.0.0")) server = socket Log.i(TAG, "media proxy listening on ${socket.localPort} for ${addresses.joinToString { it.hostAddress ?: "?" }}") accept(socket)@@ -104,37 +143,66 @@ class MediaProxyServer( // leave it parked in a blocking accept until a connection happened to arrive. runCatching { server?.close() } server = null+ tls = null+ // And closing each connection's TCP socket is what unblocks the thread serving it - see+ // [live] and [server] for why it is the TCP socket and not the TLS one. Before the scope is+ // cancelled, because a cancelled scope cannot reach them at all.+ for (client in live) runCatching { client.close() }+ live.clear() scope.cancel() } - private fun accept(socket: SSLServerSocket) {+ private fun accept(socket: ServerSocket) { scope.launch { while (isActive) { val client = try {- socket.accept() as SSLSocket+ socket.accept() } catch (e: Exception) { if (isActive) Log.w(TAG, "accept failed", e) break }- if (connections.get() >= MAX_CONNECTIONS) {- Log.w(TAG, "refusing a connection: already serving ${connections.get()}")+ // Claimed before the comparison, so admission is atomic: two accepts that each+ // read the count before either raised it would both have been let in.+ if (connections.incrementAndGet() > MAX_CONNECTIONS) {+ Log.w(TAG, "refusing a connection: already serving $MAX_CONNECTIONS")+ connections.decrementAndGet() runCatching { client.close() } continue }- connections.incrementAndGet()+ // Registered here rather than inside `serve`, so a socket accepted moments before a+ // [stop] is still closed by it even if its coroutine never runs.+ live += client scope.launch { serve(client) } } } } - private fun serve(client: SSLSocket) {+ /**+ * One connection: the TLS handshake, then requests until the client is done or the socket dies.+ *+ * The handshake happens here rather than in the accept loop because it is a round trip on the+ * network, and a loop that waited for one would refuse to accept the next connection while it did.+ */+ private fun serve(client: Socket) {+ var secure: SSLSocket? = null try { client.soTimeout = IDLE_TIMEOUT_MS // Nagle would hold back the tail of a response waiting for more to send, which on a // small range request is pure added latency. client.tcpNoDelay = true- val input = client.inputStream.buffered()- val output = client.outputStream.buffered()+ val factory = tls ?: return+ // autoClose, so closing either one closes the other and no descriptor outlives this.+ val layered = factory.createSocket(+ client,+ client.inetAddress?.hostAddress,+ client.port,+ true,+ ) as SSLSocket+ layered.useClientMode = false+ restrictProtocols(layered.supportedProtocols) { layered.enabledProtocols = it }+ secure = layered+ val input = layered.inputStream.buffered()+ val output = layered.outputStream.buffered() while (true) { val result = exchange.serve(input, output) report(result.outcome, client)@@ -145,12 +213,18 @@ class MediaProxyServer( // of a connection as often as it is a fault. Log.d(TAG, "connection from ${client.inetAddress?.hostAddress} ended: ${e.javaClass.simpleName}") } finally {+ live -= client+ // The TLS close first, which writes a `close_notify`: an unknown-length body is delimited+ // by the connection closing, so without it the TV sees a truncation rather than a clean+ // end of track. Safe from here and only from here - this is the thread that was writing,+ // so the write lock it needs is this thread's own.+ runCatching { secure?.close() } runCatching { client.close() } connections.decrementAndGet() } } - private fun report(outcome: ExchangeOutcome, client: SSLSocket) {+ private fun report(outcome: ExchangeOutcome, client: Socket) { val peer = client.inetAddress?.hostAddress when (outcome) { is ExchangeOutcome.Served ->
cast/src/test/java/com/vayunmathur/cast/platform/MediaProxyServerTest.kt
diff --git a/cast/src/test/java/com/vayunmathur/cast/platform/MediaProxyServerTest.kt b/cast/src/test/java/com/vayunmathur/cast/platform/MediaProxyServerTest.ktnew file mode 100644index 0000000000..9e2d2f0802--- /dev/null+++ b/cast/src/test/java/com/vayunmathur/cast/platform/MediaProxyServerTest.kt@@ -0,0 +1,93 @@+package com.vayunmathur.cast.platform++import com.vayunmathur.cast.protocol.EphemeralTls+import com.vayunmathur.cast.protocol.MediaProxy+import com.vayunmathur.cast.protocol.MediaResource+import java.io.InputStream+import java.net.InetAddress+import javax.net.ssl.SSLSocket+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNotNull+import kotlin.test.assertTrue++/**+ * That stopping the proxy actually lets go of its sockets.+ *+ * Everything about *what* the proxy answers is `:cast:protocol`'s `MediaProxyTest`; what is left in+ * this class is sockets and lifecycle, and the one fault there is invisible from outside. An+ * unknown-length body is a single unbounded `write` for the whole track, so once the reader stops+ * the serving thread parks in TCP backpressure - and coroutine cancellation cannot interrupt a+ * thread blocked in socket I/O. A `stop()` that closed only the listening socket therefore leaked a+ * thread and a descriptor per session, and `connections` never came back down: after enough+ * reconnects `MAX_CONNECTIONS` refused to serve anything at all.+ */+class MediaProxyServerTest {++ private val loopback: InetAddress = InetAddress.getLoopbackAddress()++ @Test+ fun `stop unwinds a connection parked in an unbounded write`() {+ val token = MediaProxyServer.randomToken()+ val server = MediaProxyServer(token) { Endless() }+ val endpoint = assertNotNull(server.start(listOf(loopback)), "the proxy did not bind")+ try {+ val client = EphemeralTls.client(endpoint.certificateFingerprint)+ .socketFactory.createSocket(loopback, endpoint.port) as SSLSocket+ client.use {+ it.soTimeout = AWAIT_MS.toInt()+ it.startHandshake()+ it.outputStream.write(+ "GET /$token/anything HTTP/1.1\r\nHost: ${loopback.hostAddress}\r\n\r\n"+ .toByteArray(Charsets.ISO_8859_1),+ )+ it.outputStream.flush()+ // Enough of the answer to know a body has started, and then deliberately nothing+ // more: an unread socket is what fills the window and parks the writer.+ assertTrue(it.inputStream.read() >= 0, "the proxy answered nothing")+ awaitConnections(server, 1)+ Thread.sleep(WRITE_BLOCK_MS)+ // The resource never ends, so a connection still counted here is one whose thread+ // is inside `write` rather than one that finished.+ assertEquals(1, server.openConnections, "the transfer ended by itself")++ server.stop()+ awaitConnections(server, 0)+ }+ } finally {+ server.stop()+ }+ }++ private fun awaitConnections(server: MediaProxyServer, expected: Int) {+ val deadline = System.currentTimeMillis() + AWAIT_MS+ while (System.currentTimeMillis() < deadline) {+ if (server.openConnections == expected) return+ Thread.sleep(POLL_MS)+ }+ assertEquals(expected, server.openConnections, "the connection count never settled")+ }++ /** A resource that is always ready with more bytes and never ends, like a live transcode. */+ private class Endless : MediaResource {+ override val length: Long = MediaProxy.UNKNOWN_LENGTH+ override val contentType: String = "audio/ogg"++ override fun open(offset: Long): InputStream = object : InputStream() {+ override fun read(): Int = 0++ override fun read(b: ByteArray, off: Int, len: Int): Int {+ b.fill(0, off, off + len)+ return len+ }+ }+ }++ private companion object {+ const val AWAIT_MS = 5_000L+ const val POLL_MS = 20L++ /** Long enough for the socket buffers to fill and the serving thread to stop making progress. */+ const val WRITE_BLOCK_MS = 500L+ }+}