From e418cdc82c176ce5915d05b5dd0b4cd2b1f9eab1 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Sat, 7 Nov 2026 21:32:00 +0000 Subject: [PATCH 1/2] tcp: a connection whose handler let go gives its buffers back when a recv runs short, and the read timeout ends it even with no recv running (#21) A recv parked on -ENOBUFS can't see the peer's close, and the sweep's shutdown has no completion to end, while the buffers come back only at the teardown those completions run. Nobody will read such a connection, so what it holds goes back, and the sweep runs the teardown itself. In incremental mode the connection kept its descriptor and group id for good; on the shared ring, the default, a few kilobytes of one-byte sends stopped every connection on the proactor. A port of MDA2AV/ioxide#546, with its tests. --- .../kronos/connection/tcp/TcpConnection.kt | 20 +- .../kotlin/kronos/connection/tcp/TcpPipe.kt | 10 +- .../proactor/transport/tcp/TcpTransport.kt | 45 +- .../commonMain/kotlin/kronos/tls/TlsPipe.kt | 6 +- .../commonTest/kotlin/kronos/LetGoDriver.kt | 209 ++++++++ .../commonTest/kotlin/kronos/TestSupport.kt | 15 +- .../connection/tcp/IncrementalDryRingTest.kt | 217 ++++++++ .../kronos/connection/tcp/LetGoBufferTest.kt | 494 ++++++++++++++++++ .../connection/tcp/LetGoHandlerStyleTest.kt | 141 +++++ .../kotlin/kronos/tls/TlsLetGoBufferTest.kt | 169 ++++++ 10 files changed, 1303 insertions(+), 23 deletions(-) create mode 100644 kronos/src/commonTest/kotlin/kronos/LetGoDriver.kt create mode 100644 kronos/src/commonTest/kotlin/kronos/connection/tcp/IncrementalDryRingTest.kt create mode 100644 kronos/src/commonTest/kotlin/kronos/connection/tcp/LetGoBufferTest.kt create mode 100644 kronos/src/commonTest/kotlin/kronos/connection/tcp/LetGoHandlerStyleTest.kt create mode 100644 kronos/src/nativeTest/kotlin/kronos/tls/TlsLetGoBufferTest.kt diff --git a/kronos/src/commonMain/kotlin/kronos/connection/tcp/TcpConnection.kt b/kronos/src/commonMain/kotlin/kronos/connection/tcp/TcpConnection.kt index bc9f419..b741b1d 100644 --- a/kronos/src/commonMain/kotlin/kronos/connection/tcp/TcpConnection.kt +++ b/kronos/src/commonMain/kotlin/kronos/connection/tcp/TcpConnection.kt @@ -235,18 +235,20 @@ class TcpConnection internal constructor( releaseParkedFlush() } - // Recycle: hands back the buffers still queued - shared ones; a connection's own ring is reset whole. + // Hands back the buffers still queued, for a handler that won't read them, the way a handler returns them. internal fun drainRecv(scratch: RecvItem) { while (recv.tryDequeue(scratch)) { - if (scratch.hasBuffer && ring == null) { - tcp.returnBufferDirect(scratch.bufferId) - } + returnBuffer(scratch) } } + /** Recv completions queued and not taken yet. */ + internal val queuedRecvs: Int get() = recv.countUntil(recv.snapshotTail()) + /** * The adapter holding buffers taken out of the queue, if any: recycle asks it to give them back, so a - * buffer stranded by a handler that returned early does not depend on the handler remembering. + * buffer stranded by a handler that returned early does not depend on the handler remembering. The proactor + * asks sooner when the handler has let go and a recv needs the buffers. */ internal var bufferHolder: RecvBufferHolder? = null @@ -489,7 +491,8 @@ class TcpConnection internal constructor( /** * The handler's release, once per life - the proactor makes it when the handler returns, so calling it is * only for letting go earlier. A peer still connected gets a FIN; the read side stays open until the peer - * closes, bounded by [TcpOptions.readTimeoutMs]. + * closes, bounded by [TcpOptions.readTimeoutMs]. Nobody reads that side from here on: what is queued and what a + * reader holds can go back to the kernel at any point, so a handler that goes on must leave them alone. */ fun decRef() { if (!tcp.proactor.onProactorThread) { @@ -662,8 +665,9 @@ class TcpConnection internal constructor( } /** - * An adapter that takes buffers out of a connection's queue and holds them past one read (a pipe reader): - * recycle asks it to hand them back. + * An adapter that takes buffers out of a connection's queue and holds them past one read (a pipe reader): recycle + * asks it to hand them back, and so can the proactor before that, once the handler has let go. It can be asked more + * than once, and each buffer goes back only once. */ internal interface RecvBufferHolder { fun releaseHeldBuffers() diff --git a/kronos/src/commonMain/kotlin/kronos/connection/tcp/TcpPipe.kt b/kronos/src/commonMain/kotlin/kronos/connection/tcp/TcpPipe.kt index 8fe71d7..48cc925 100644 --- a/kronos/src/commonMain/kotlin/kronos/connection/tcp/TcpPipe.kt +++ b/kronos/src/commonMain/kotlin/kronos/connection/tcp/TcpPipe.kt @@ -51,7 +51,7 @@ class TcpPipeReader(private val connection: TcpConnection) : PipeReader, RecvBuf private val lostToOverflow: Boolean get() = connectionClosed && connection.recvOverflowed init { - // So the proactor can reclaim at recycle what this reader dequeued and never gave back. + // So the proactor can reclaim what this reader dequeued and never gave back. connection.bufferHolder = this } @@ -220,10 +220,10 @@ class TcpPipeReader(private val connection: TcpConnection) : PipeReader, RecvBuf } /** - * Hands every held buffer back and forgets the chain. Called by [complete], and by the proactor at recycle - * for a reader its handler never completed. Idempotent: once the chain is empty it does nothing, so - * complete-then-recycle returns each buffer once. Terminal, so a reader kept past its handler fails instead - * of reading the recycled connection's next tenant. + * Hands every held buffer back and forgets the chain. Called by [complete], and by the proactor for a reader + * its handler never completed: at recycle, or sooner once the handler has let go and a recv needs the buffers. + * Idempotent: once the chain is empty it does nothing, so complete-then-recycle returns each buffer once. + * Terminal, so a reader kept past its handler fails instead of reading the recycled connection's next tenant. */ override fun releaseHeldBuffers() { completed = true diff --git a/kronos/src/commonMain/kotlin/kronos/proactor/transport/tcp/TcpTransport.kt b/kronos/src/commonMain/kotlin/kronos/proactor/transport/tcp/TcpTransport.kt index 6ef036f..7ae5806 100644 --- a/kronos/src/commonMain/kotlin/kronos/proactor/transport/tcp/TcpTransport.kt +++ b/kronos/src/commonMain/kotlin/kronos/proactor/transport/tcp/TcpTransport.kt @@ -394,8 +394,7 @@ internal class TcpTransport(val proactor: Proactor, private val config: ServerCo // The group ran dry, not a peer error: the multishot ended, so park the connection until buffers // come back. if (conn != null) { - conn.readParkedMs = proactor.nowMs // the peer sent; the wait is ours, so the clock restarts - recvStarved.add((gen.toLong() shl 32) or (fd.toLong() and 0xFFFF_FFFFL)) + parkStarvedRecv(conn, fd, gen) } return @@ -442,8 +441,7 @@ internal class TcpTransport(val proactor: Proactor, private val config: ServerCo // buffers. Only this connection waits - its bytes stay in the kernel and TCP slows its peer - until a // buffer comes back and the loop re-arms the recv. if (conn != null) { - conn.readParkedMs = proactor.nowMs // the peer sent; the wait is ours, so the clock restarts - recvStarved.add((gen.toLong() shl 32) or (fd.toLong() and 0xFFFF_FFFFL)) + parkStarvedRecv(conn, fd, gen) } return @@ -473,6 +471,22 @@ internal class TcpTransport(val proactor: Proactor, private val config: ServerCo } } + private fun parkStarvedRecv(conn: TcpConnection, fd: Int, gen: Int) { + conn.readParkedMs = proactor.nowMs // the peer sent; the wait is ours, so the clock restarts + if (conn.handlerReleased) { + returnReleasedBuffers(conn) + } + recvStarved.add((gen.toLong() shl 32) or (fd.toLong() and 0xFFFF_FFFFL)) + } + + // Nobody will read a connection its handler let go, and its recv needs a buffer to reach the peer's close, which + // is what tears it down. So what it holds goes back. + private fun returnReleasedBuffers(conn: TcpConnection) { + // Not claimed the way recycle claims it: a read left parked can still take more, for recycle to ask back. + conn.bufferHolder?.releaseHeldBuffers() + conn.drainRecv(scratchItem) + } + // Once per loop pass, and only when a buffer came back since the last one, so a parked connection // cannot spin the loop. fun rearmStarvedRecvs() { @@ -603,9 +617,16 @@ internal class TcpTransport(val proactor: Proactor, private val config: ServerCo val now = Clock.monotonicMs() val conns = connections + val starved = recvStarved.isNotEmpty() for (fd in conns.indices) { val conn = conns[fd] ?: continue + + // A recv is waiting for a buffer, maybe its own, and this connection holds some for nobody. + if (starved && conn.handlerReleased) { + returnReleasedBuffers(conn) + } + if (conn.sweepClosed) { continue } @@ -632,11 +653,25 @@ internal class TcpTransport(val proactor: Proactor, private val config: ServerCo } } } + + // A condemned connection whose recv is parked has no completion coming for its shutdown to end. + var i = recvStarved.size - 1 + while (i >= 0) { + val parked = recvStarved[i] + val fd = parked.toInt() + val conn = connAt(fd, (parked ushr 32).toInt()) + if (conn != null && conn.sweepClosed) { + recvStarved.removeAt(i) + closeFromRecv(conn, fd) + } + i-- + } } // shutdown() is what the peer sees, and also what releases the connection: it ends the recv a silent // peer never completes, or the send a stalled one holds, and those completions run the teardown that - // gets the refcount right once the kernel is done with the slab. markClosed wakes a parked read now. + // gets the refcount right once the kernel is done with the slab. markClosed wakes a parked read now. A recv + // parked on -ENOBUFS has no completion to wait for, so [sweep] runs that teardown itself. private fun sweepClose(conn: TcpConnection) { conn.sweepClosed = true conn.suppressFin() // this shutdown is the FIN diff --git a/kronos/src/commonMain/kotlin/kronos/tls/TlsPipe.kt b/kronos/src/commonMain/kotlin/kronos/tls/TlsPipe.kt index 7d27943..5e5b82d 100644 --- a/kronos/src/commonMain/kotlin/kronos/tls/TlsPipe.kt +++ b/kronos/src/commonMain/kotlin/kronos/tls/TlsPipe.kt @@ -334,9 +334,9 @@ class TlsDecryptingPipeReader( } /** - * Frees the plaintext buffer. Called by [complete], and by the proactor at recycle for a reader its handler never - * completed; idempotent. Terminal, so a reader kept past its handler fails instead of reading the recycled - * connection's next tenant. + * Frees the plaintext buffer. Called by [complete], and by the proactor for a reader its handler never completed: + * at recycle, or sooner once the handler has let go and a recv needs the buffers. Idempotent. Terminal, so a reader + * kept past its handler fails instead of reading the recycled connection's next tenant. */ override fun releaseHeldBuffers() { completed = true diff --git a/kronos/src/commonTest/kotlin/kronos/LetGoDriver.kt b/kronos/src/commonTest/kotlin/kronos/LetGoDriver.kt new file mode 100644 index 0000000..6b026ab --- /dev/null +++ b/kronos/src/commonTest/kotlin/kronos/LetGoDriver.kt @@ -0,0 +1,209 @@ +package kronos + +import kotlin.concurrent.atomics.AtomicInt +import kotlin.coroutines.Continuation +import kotlin.coroutines.EmptyCoroutineContext +import kotlin.coroutines.startCoroutine +import kotlin.test.assertTrue + +/** + * Clients that outlive their handlers, for any handler on the TCP transport. Once a handler has let go nobody reads + * its connection, so what its client goes on sending holds buffers. A server survives that if a new client is still + * served and the connections end, whichever way the client goes and whether or not a read timeout is set. + */ +internal object LetGoDriver { + const val GRACE_MS = 4_000L + + private const val CLIENTS = 3 + + /** Which rings the clients empty, and what that takes: below the 64-entry queue, so nothing overflows. */ + class Mode(val name: String, val sendsEach: Int, val bytesEach: Int, val incremental: IncrementalOptions?) + + val SHARED = Mode("shared ring", sendsEach = 30, bytesEach = 1, incremental = null) + + val INCREMENTAL = Mode( + "incremental", + sendsEach = 8, + bytesEach = 8 * 1024, + incremental = IncrementalOptions(maxConnections = 16, recvSlots = 4, recvBufferSize = 4096), + ) + + /** What the clients do next, and whether that ends the connections. */ + class Ending(val name: String, val leave: Boolean, val readTimeoutMs: Long, val ends: Boolean) + + private val THEY_LEAVE = Ending("they leave", leave = true, readTimeoutMs = 300, ends = true) + private val THEY_LEAVE_NO_TIMEOUT = + Ending("they leave, no read timeout", leave = true, readTimeoutMs = 0, ends = true) + private val THEY_STAY = Ending("they stay, until the read timeout", leave = false, readTimeoutMs = 300, ends = true) + private val THEY_STAY_NO_TIMEOUT = + Ending("they stay, no read timeout", leave = false, readTimeoutMs = 0, ends = false) + + // IOU_PBUF_RING_INC arrived in Linux 6.12. + val incrementalSupported: Boolean = run { + val parts = kernelRelease().split('.', '-') + val major = parts.getOrNull(0)?.toIntOrNull() ?: 0 + val minor = parts.getOrNull(1)?.toIntOrNull() ?: 0 + major > 6 || (major == 6 && minor >= 12) + } + + val modes: List = if (incrementalSupported) listOf(SHARED, INCREMENTAL) else listOf(SHARED) + + /** + * Runs [case] for every mode and ending that can show anything, naming the one that failed: a ring of its own takes + * nobody else's buffers, and with no end coming nothing can be seen to close. + */ + fun forEachCase(case: (Mode, Ending) -> Unit) { + for (mode in modes) { + for (ending in listOf(THEY_LEAVE, THEY_LEAVE_NO_TIMEOUT, THEY_STAY, THEY_STAY_NO_TIMEOUT)) { + if (mode !== INCREMENTAL || ending !== THEY_STAY_NO_TIMEOUT) { + naming("${mode.name}, ${ending.name}") { case(mode, ending) } + } + } + } + } + + /** Runs [block], putting [name] on what it fails with. */ + fun naming(name: String, block: () -> Unit) { + try { + block() + } catch (e: AssertionError) { + throw AssertionError("$name: ${e.message}") + } catch (e: IOException) { + throw AssertionError("$name: ${e.message}") + } + } + + fun config(mode: Mode, readTimeoutMs: Long, sendTimeoutMs: Long = 500) = ServerConfig( + proactorCount = 1, + ringEntries = 256, + recvBufferSize = 4096, + recvSlots = 64, + tcp = TcpOptions(port = 0, recvQueueEntries = 64, readTimeoutMs = readTimeoutMs, sendTimeoutMs = sendTimeoutMs), + incremental = mode.incremental, + ) + + /** A client that outlived its handler, and how it goes: a TLS client has a session of its own to free. */ + class Peer(val socket: TestSocket, private val release: () -> Unit = socket::close) { + fun close() = release() + + fun trySend(bytes: ByteArray) { + try { + socket.write(bytes) + } catch (e: IOException) { + // The server has already ended this one, which is allowed at any point from here. + } + } + } + + /** + * [letGoClient] makes a connected client whose handler has let go, or is about to; [served] says whether a new + * client, using the server as it should, gets its answer. + */ + fun check(proactor: Proactor, mode: Mode, ending: Ending, letGoClient: () -> Peer, served: () -> Boolean) { + val clients = ArrayList() + try { + repeat(CLIENTS) { + clients.add(letGoClient()) + } + + val message = ByteArray(mode.bytesEach) + repeat(mode.sendsEach) { + clients.forEach { it.trySend(message) } + sleepMs(1) // apart, so each is a recv of its own + } + + if (ending.leave) { + clients.forEach { it.close() } + clients.clear() + } + + sleepMs(if (ending.readTimeoutMs > 0) 1_000 else 300) + + assertTrue(served(), "a new client was never answered: the let-go connections kept the buffers") + + if (ending.ends) { + assertTrue( + eventually { openConnections(proactor) == 0 }, + "${openConnections(proactor)} connections still open, a new client served and their handlers gone", + ) + } + } finally { + clients.forEach { it.close() } + } + } + + fun connect(port: Int, receiveBuffer: Int = 0): TestSocket { + val socket = TestSocket() + socket.setReceiveTimeout(GRACE_MS.toInt()) + socket.setNoDelay() + if (receiveBuffer > 0) { + socket.setReceiveBuffer(receiveBuffer) // before connect, so it is the window advertised + } + + socket.connect(port) + return socket + } + + /** Connected, and everything the server sent has been read through to its FIN or reset. */ + fun refused(port: Int, first: ByteArray): Peer { + val client = connect(port) + client.write(first) + readToEnd(client) + return Peer(client) + } + + private fun readToEnd(client: TestSocket) { + val buffer = ByteArray(4096) + try { + while (client.read(buffer, 0, buffer.size) >= 0) { + // what it was refused with + } + } catch (e: IOException) { + // A reset is an end too: the sends that follow are what the test is about. A timeout is not. + if (e.message?.contains("errno 104") != true) { + throw e + } + } + } + + /** Whether [request] got its answer: a connection the server ended or never served did not. */ + fun answers(request: () -> Boolean): Boolean = try { + request() + } catch (e: IOException) { + false + } + + fun openConnections(proactor: Proactor): Int = onProactor(proactor) { proactor.tcp!!.openConnections } + + /** Runs [block] on the proactor thread and waits for its answer. */ + fun onProactor(proactor: Proactor, block: () -> Int): Int { + val result = AtomicInt(Int.MIN_VALUE) + val done = Latch(1) + + proactor.post { + result.store(block()) + done.countDown() + } + + assertTrue(done.await(10_000), "the proactor never ran the task") + return result.load() + } + + fun eventually(condition: () -> Boolean): Boolean { + val deadline = Clock.monotonicMs() + GRACE_MS + while (Clock.monotonicMs() < deadline) { + if (condition()) { + return true + } + + sleepMs(10) + } + + return condition() + } + + /** Starts [block] apart from the handler, which goes on, and returns, while it waits. */ + fun detached(block: suspend () -> Unit) { + block.startCoroutine(Continuation(EmptyCoroutineContext) {}) + } +} diff --git a/kronos/src/commonTest/kotlin/kronos/TestSupport.kt b/kronos/src/commonTest/kotlin/kronos/TestSupport.kt index e5b29af..8de38d2 100644 --- a/kronos/src/commonTest/kotlin/kronos/TestSupport.kt +++ b/kronos/src/commonTest/kotlin/kronos/TestSupport.kt @@ -81,17 +81,27 @@ class TestSocket : AutoCloseable { } /** A read that waits longer than this fails with EAGAIN (11). */ - fun setReceiveTimeout(milliseconds: Int) { + fun setReceiveTimeout(milliseconds: Int) = setTimeout(SO_RCVTIMEO, milliseconds) + + /** A write that waits longer than this fails with EAGAIN (11). */ + fun setSendTimeout(milliseconds: Int) = setTimeout(SO_SNDTIMEO, milliseconds) + + private fun setTimeout(option: Int, milliseconds: Int) { val tv = Native.malloc(TIMEVAL_SIZE) try { putLong(tv, (milliseconds / 1000).toLong()) putLong(tv + 8, (milliseconds % 1000 * 1000).toLong()) - Native.setsockopt(fd, Sockets.SOL_SOCKET, SO_RCVTIMEO, tv, TIMEVAL_SIZE.toInt()) + Native.setsockopt(fd, Sockets.SOL_SOCKET, option, tv, TIMEVAL_SIZE.toInt()) } finally { Native.free(tv) } } + /** Every write leaves as the test cut it, not coalesced behind an unacknowledged one. */ + fun setNoDelay() { + Sockets.setInt(fd, Sockets.IPPROTO_TCP, Sockets.TCP_NODELAY, 1) + } + /** The kernel's receive buffer, before [connect]: small, so a peer's send backs up soon. */ fun setReceiveBuffer(bytes: Int) { Sockets.setInt(fd, Sockets.SOL_SOCKET, Sockets.SO_RCVBUF, bytes) @@ -224,6 +234,7 @@ class TestSocket : AutoCloseable { const val SOCKADDR_IN_SIZE = 16L const val TIMEVAL_SIZE = 16L const val SO_RCVTIMEO = 20 + const val SO_SNDTIMEO = 21 const val EINTR = 4 } } diff --git a/kronos/src/commonTest/kotlin/kronos/connection/tcp/IncrementalDryRingTest.kt b/kronos/src/commonTest/kotlin/kronos/connection/tcp/IncrementalDryRingTest.kt new file mode 100644 index 0000000..74cd6a3 --- /dev/null +++ b/kronos/src/commonTest/kotlin/kronos/connection/tcp/IncrementalDryRingTest.kt @@ -0,0 +1,217 @@ +package kronos + +import kotlin.concurrent.atomics.AtomicInt +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +/** + * A connection whose own ring is dry once its handler has gone, whichever came first. Nothing will read what fills the + * ring, and with its recv parked on -ENOBUFS there is no completion for the sweep's shutdown to end, and no recv to see + * the peer's FIN. + */ +class IncrementalDryRingTest { + private fun config(slots: Int, bufferBytes: Int, readTimeoutMs: Long, cap: Int = 8) = ServerConfig( + proactorCount = 1, + ringEntries = 256, + tcp = TcpOptions(port = 0, readTimeoutMs = readTimeoutMs, sendTimeoutMs = 300), + incremental = IncrementalOptions(maxConnections = cap, recvSlots = slots, recvBufferSize = bufferBytes), + ) + + private fun unsupported(): Boolean { + if (!LetGoDriver.incrementalSupported) { + println("skipped: incremental mode needs Linux 6.12, this is ${kernelRelease()}") + } + + return !LetGoDriver.incrementalSupported + } + + private suspend fun consume(conn: TcpConnection) { + val item = RecvItem() + val snapshot = conn.read() + while (conn.tryGetItem(snapshot, item)) { + conn.returnBuffer(item) + } + } + + // Sends [sent] once the handler has let go, and leaves. Whether the connection ended. + private fun closedAfterItsClientLeft(sent: Int, slots: Int, bufferBytes: Int, readTimeoutMs: Long = 300): Boolean { + TestServer(config(slots, bufferBytes, readTimeoutMs)) { conn -> consume(conn) }.use { server -> + val client = LetGoDriver.connect(server.port) + try { + client.write("hi") + assertEquals(-1, client.read(), "no FIN after the handler let go") + client.write(ByteArray(sent)) + } finally { + client.close() + } + + return LetGoDriver.eventually { LetGoDriver.openConnections(server.proactor) == 0 } + } + } + + // A handler that waits until a 2 KiB ring is dry, has what it holds taken by [hold], and returns. Whether the + // connection ended once its client left, after checking the handler did hold the ring. + private fun closedAfterLettingGoOfADryRing(readTimeoutMs: Long, hold: suspend (TcpConnection) -> Int): Boolean { + val held = AtomicInt(0) + val handler: suspend (TcpConnection) -> Unit = { conn -> + RingTimer(conn.proactor).delay(250) + held.store(hold(conn)) + } + + TestServer(config(slots = 2, bufferBytes = 1024, readTimeoutMs), handler).use { server -> + val client = LetGoDriver.connect(server.port) + try { + client.write(ByteArray(64 * 1024)) // past the 2 KiB ring + sleepMs(400) + } finally { + client.close() + } + + val closed = LetGoDriver.eventually { LetGoDriver.openConnections(server.proactor) == 0 } + assertTrue(held.load() >= 2 * 1024, "the handler held only ${held.load()} bytes: this proved nothing") + return closed + } + } + + // Takes everything, and returns without handing it back: this breaks tryGetItem's contract. + private suspend fun takeRaw(conn: TcpConnection): Int { + val item = RecvItem() + val snapshot = conn.read() + var bytes = 0 + while (conn.tryGetItem(snapshot, item)) { + bytes += item.length + } + + return bytes + } + + // The same through a pipe reader, which may hold: nothing obliges a handler to complete it. + private suspend fun holdInAReader(conn: TcpConnection): Int = TcpPipeReader(conn).read().buffer.length.toInt() + + @Test + fun `a connection whose ring ran dry after its handler let go is closed once its client leaves`() { + if (unsupported()) { + return + } + + assertTrue( + closedAfterItsClientLeft(sent = 64 * 1024, slots = 2, bufferBytes = 1024), + "the connection is still open after its client left", + ) + } + + @Test + fun `a connection whose ring ran dry after its handler let go is closed once its client leaves - with no read timeout`() { + if (unsupported()) { + return + } + + assertTrue( + closedAfterItsClientLeft(sent = 64 * 1024, slots = 2, bufferBytes = 1024, readTimeoutMs = 0), + "the connection is still open after its client left", + ) + } + + @Test + fun `control - a connection whose ring never ran dry is closed when its client leaves`() { + if (unsupported()) { + return + } + + assertTrue( + closedAfterItsClientLeft(sent = 1024, slots = 16, bufferBytes = 4096), + "the connection is still open after its client left", + ) + } + + @Test + fun `a handler that let go holding its dry ring raw is closed by the read timeout`() { + if (unsupported()) { + return + } + + assertTrue( + closedAfterLettingGoOfADryRing(readTimeoutMs = 300, ::takeRaw), + "the connection is still open, many read timeouts after its client left", + ) + } + + @Test + fun `a handler that let go holding its dry ring in a reader is closed once its client leaves`() { + if (unsupported()) { + return + } + + assertTrue( + closedAfterLettingGoOfADryRing(readTimeoutMs = 300, ::holdInAReader), + "the connection is still open after its client left", + ) + } + + @Test + fun `a handler that let go holding its dry ring in a reader is closed once its client leaves - with no read timeout`() { + if (unsupported()) { + return + } + + assertTrue( + closedAfterLettingGoOfADryRing(readTimeoutMs = 0, ::holdInAReader), + "the connection is still open after its client left", + ) + } + + private val answerRefused = + "HTTP/1.1 413 Payload Too Large\r\nConnection: close\r\nContent-Length: 0\r\n\r\n".encodeToByteArray() + + // Eight clients send [sent] to a handler that refuses and returns, then leave, using up a cap of 8 if their + // connections stay. Whether the next client reached a handler. + private fun nextClientServedAfterUploadsOf(sent: Int): Boolean { + val handled = AtomicInt(0) + val refuse: suspend (TcpConnection) -> Unit = { conn -> + handled.addAndFetch(1) + consume(conn) + conn.write(answerRefused) + conn.flush() + } + + TestServer(config(slots = 16, bufferBytes = 4096, readTimeoutMs = 300), refuse).use { server -> + repeat(8) { + val client = LetGoDriver.connect(server.port) + client.write(ByteArray(sent)) + sleepMs(50) + client.close() + } + sleepMs(2_000) + + val before = handled.load() + val next = LetGoDriver.connect(server.port) + next.write("GET / HTTP/1.1\r\nHost: x\r\n\r\n") + sleepMs(200) + next.close() + + return handled.load() == before + 1 + } + } + + @Test + fun `refused uploads do not use up the connection cap`() { + if (unsupported()) { + return + } + + assertTrue( + nextClientServedAfterUploadsOf(256 * 1024), + "the next client was shed, every group id taken by clients long gone", + ) + } + + @Test + fun `control - uploads that fit the ring leave the connection cap free`() { + if (unsupported()) { + return + } + + assertTrue(nextClientServedAfterUploadsOf(1024), "the next client was shed, with uploads that fit the ring") + } +} diff --git a/kronos/src/commonTest/kotlin/kronos/connection/tcp/LetGoBufferTest.kt b/kronos/src/commonTest/kotlin/kronos/connection/tcp/LetGoBufferTest.kt new file mode 100644 index 0000000..c39914e --- /dev/null +++ b/kronos/src/commonTest/kotlin/kronos/connection/tcp/LetGoBufferTest.kt @@ -0,0 +1,494 @@ +package kronos + +import kotlin.concurrent.atomics.AtomicReference +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull +import kotlin.test.assertTrue + +/** + * What a connection holds once its handler has let go. Nobody will read it, and its buffers come back only at + * teardown, which takes a recv completion, which takes a buffer. So it has to give them up, and the read timeout has + * to end it even with no recv running. On the shared ring that decides whether anyone else can receive. + */ +class LetGoBufferTest { + private val big = ByteArray(8 * 1024 * 1024) // far past what a 4 KiB window and the socket buffers hold + private val pong = "pong".encodeToByteArray() + + private class LetGoList { + private val connections = AtomicReference>(emptyList()) + + val all: List get() = connections.load() + + fun add(conn: TcpConnection) = connections.store(all + conn) + + fun clear() = connections.store(emptyList()) + } + + private fun server(config: ServerConfig, letGo: LetGoList) = TestServer(config) { conn -> handle(conn, letGo) } + + private fun shared(slots: Int, readTimeoutMs: Long, sendTimeoutMs: Long = 0) = ServerConfig( + proactorCount = 1, + ringEntries = 256, + recvBufferSize = 4096, + recvSlots = slots, + tcp = TcpOptions(port = 0, recvQueueEntries = 64, readTimeoutMs = readTimeoutMs, sendTimeoutMs = sendTimeoutMs), + ) + + // 'R' lets go, 'W' lets go with a read left parked, 'P' answers, 'E' echoes from here on, 'B' parks in a flush its + // peer won't drain, 'F' lets go with that flush in flight, and 'G' waits it out and returns with its input unread. + private suspend fun handle(conn: TcpConnection, letGo: LetGoList) { + val item = RecvItem() + var echoing = false + while (true) { + val snapshot = conn.read() + + var marker = -1 + while (conn.tryGetItem(snapshot, item)) { + if (item.hasBuffer) { + marker = item[0].toInt() + echoing = echoing || marker == 'E'.code + if (echoing) { + conn.write(item.address, item.length) + } + + conn.returnBuffer(item) + } + } + + if (echoing) { + conn.flush() + if (snapshot.isClosed) { + return + } + + continue + } + + when (marker) { + 'F'.code -> { + conn.write(big) + LetGoDriver.detached { conn.flush() } + return + } + + 'G'.code -> { + conn.write(big) + conn.flush() + return + } + + 'R'.code -> { + letGo.add(conn) + return + } + + 'W'.code -> { + LetGoDriver.detached { TcpPipeReader(conn).read() } + return + } + + 'B'.code -> { + conn.write(big) + conn.flush() + } + + 'P'.code -> { + conn.write(pong) + conn.flush() + } + } + + if (snapshot.isClosed) { + return + } + } + } + + // Connected, with its handler gone: the FIN of the release has arrived. + private fun letGoPeer(port: Int): TestSocket { + val peer = LetGoDriver.connect(port) + peer.write("R") + assertEquals(-1, peer.read(), "no FIN after the handler let go") + return peer + } + + // A buffer each, each seen to arrive, so the ring ends with exactly [deliveries] gone and no recv having noticed. + private fun holdBuffers( + port: Int, + letGo: LetGoList, + deliveries: Int, + peerCount: Int, + peers: MutableList, + ) { + repeat(peerCount) { + peers.add(letGoPeer(port)) + } + + val first = peers.size - peerCount + val one = byteArrayOf('x'.code.toByte()) + for (sent in 1..deliveries) { + peers[first + (sent - 1) % peerCount].write(one) + awaitQueued(letGo, sent) + } + } + + private fun awaitQueued(letGo: LetGoList, expected: Int) { + val deadline = Clock.monotonicMs() + LetGoDriver.GRACE_MS + while (letGo.all.sumOf { it.queuedRecvs } != expected) { + assertTrue(Clock.monotonicMs() < deadline, "message $expected never reached its queue") + sleepMs(0) + } + } + + private fun asked(socket: TestSocket): Boolean = LetGoDriver.answers { + socket.write("P") + socket.readBytes(pong.size).contentEquals(pong) + } + + private fun askedOn(port: Int): Boolean = LetGoDriver.answers { + val next = LetGoDriver.connect(port) + try { + asked(next) + } finally { + next.close() + } + } + + // Peers that sent [sendsEach] apart and left, many read timeouts ago. Whether a new client was served, and how + // many of them are still open. + private fun thenLeave(sendsEach: Int): Pair { + val letGo = LetGoList() + server(shared(slots = 256, readTimeoutMs = 300), letGo).use { server -> + val peers = List(5) { letGoPeer(server.port) } + repeat(sendsEach) { + peers.forEach { it.write("x") } + sleepMs(1) // apart, so each is a recv of its own + } + + peers.forEach { it.close() } + sleepMs(2_000) + + val served = askedOn(server.port) + LetGoDriver.eventually { LetGoDriver.openConnections(server.proactor) == 0 } + return served to LetGoDriver.openConnections(server.proactor) + } + } + + @Test + fun `shared ring - connections whose handlers let go holding every buffer are closed and a new client is served`() { + val (served, open) = thenLeave(sendsEach = 60) + assertTrue(served, "the new client was never answered, with $open connections still open") + assertEquals(0, open, "connections outlived their clients") + } + + @Test + fun `shared ring - control - the same with room left in the ring`() { + val (served, open) = thenLeave(sendsEach = 20) + assertTrue(served, "the new client was never answered, with $open connections still open") + assertEquals(0, open, "connections outlived their clients") + } + + // 66 peers, so 4096 buffers stay inside each one's 64-entry queue. No read timeout, so nothing but the buffers ends + // these connections, and nothing limits how long the sends may take. + private fun servedAtDefaultSizes(deliveries: Int): Boolean { + val letGo = LetGoList() + val config = ServerConfig(proactorCount = 1, tcp = TcpOptions(port = 0, readTimeoutMs = 0)) + assertEquals(4096, config.recvSlots, "this test is about the default ring") + + server(config, letGo).use { server -> + val peers = ArrayList() + try { + holdBuffers(server.port, letGo, deliveries, peerCount = 66, peers) + return askedOn(server.port) + } finally { + peers.forEach { it.close() } + } + } + } + + @Test + fun `shared ring - at the default sizes 4096 one-byte sends on let-go connections do not stop a new client`() { + assertTrue( + servedAtDefaultSizes(4096), + "the new client was never answered: let-go connections kept every buffer", + ) + } + + @Test + fun `shared ring - control - at the default sizes with four buffers left`() { + assertTrue(servedAtDefaultSizes(4092), "the new client was never answered, with buffers left in the ring") + } + + @Test + fun `shared ring - a starved recv takes back what let-go connections hold - with no read timeout and their peers still connected`() { + val letGo = LetGoList() + server(shared(slots = 16, readTimeoutMs = 0), letGo).use { server -> + val peers = ArrayList() + try { + holdBuffers(server.port, letGo, deliveries = 16, peerCount = 2, peers) + assertTrue( + askedOn(server.port), + "the new client was never answered: let-go connections kept every buffer", + ) + } finally { + peers.forEach { it.close() } + } + } + } + + @Test + fun `shared ring - a starved recv takes back what a let-go connection's reader holds`() { + val slots = 16 + val handler: suspend (TcpConnection) -> Unit = { conn -> + val reader = TcpPipeReader(conn) + while (true) { + val read = reader.read() + if (read.buffer.isEmpty || read.buffer[0L] != 'P'.code.toByte()) { + break // with what it read still in the reader, neither advanced nor completed + } + + reader.advanceTo(read.buffer.length) + conn.write(pong) + conn.flush() + } + } + + TestServer(shared(slots, readTimeoutMs = 0), handler).use { server -> + val peers = List(slots) { letGoPeer(server.port) } // one send, so one buffer + try { + assertTrue(askedOn(server.port), "the new client was never answered: the readers kept every buffer") + } finally { + peers.forEach { it.close() } + } + } + } + + @Test + fun `shared ring - a read left parked when its handler let go strands nothing it takes later`() { + val slots = 16 + val letGo = LetGoList() + server(shared(slots, readTimeoutMs = 0), letGo).use { server -> + val peers = ArrayList() + val parked = ArrayList() + try { + repeat(slots) { + val socket = LetGoDriver.connect(server.port) + socket.write("W") + assertEquals(-1, socket.read(), "no FIN after the handler let go") + parked.add(socket) + } + + // Starved once, so the proactor asks these connections for what they hold, with their reads parked. + holdBuffers(server.port, letGo, deliveries = slots, peerCount = 2, peers) + assertTrue( + askedOn(server.port), + "the new client was never answered: let-go connections kept every buffer", + ) + + // Those reads take a buffer each, which only their holders know about. + parked.forEach { + it.write("x") + sleepMs(1) + } + + parked.forEach { it.close() } + parked.clear() + + assertTrue( + askedOn(server.port), + "the last client was never answered: parked reads stranded their buffers", + ) + } finally { + peers.forEach { it.close() } + parked.forEach { it.close() } + } + } + } + + @Test + fun `shared ring - the read timeout closes a let-go connection parked on a ring that others hold`() { + val letGo = LetGoList() + server(shared(slots = 256, readTimeoutMs = 500), letGo).use { server -> + val parked = letGoPeer(server.port) + val holders = ArrayList() + try { + // Their handlers are parked in a flush with no send timeout, and keep what their peers send. + repeat(5) { + val holder = LetGoDriver.connect(server.port, receiveBuffer = 4096) + holder.write("B") + holders.add(holder) + } + sleepMs(200) + + repeat(60) { + holders.forEach { it.write("x") } + sleepMs(1) // apart, so each is a recv of its own + } + + parked.write("x") // on the dry ring: its recv parks, with nothing of its own to give back + + assertTrue( + LetGoDriver.eventually { LetGoDriver.openConnections(server.proactor) == holders.size }, + "${LetGoDriver.openConnections(server.proactor)} of ${holders.size + 1} open, many timeouts later", + ) + } finally { + parked.close() + holders.forEach { it.close() } + } + } + } + + // Asks for an answer too big for a client that isn't reading, and doesn't wait: no FIN comes while it is flushing. + private fun asking(port: Int, marker: String): LetGoDriver.Peer { + val client = LetGoDriver.connect(port, receiveBuffer = 4096) + client.write(marker) + sleepMs(50) + return LetGoDriver.Peer(client) + } + + // The handlers that end this way have no read clock running: their flush holds it off. + private val endedBySendTimeout = + LetGoDriver.Ending("they stay, until the send timeout", leave = false, readTimeoutMs = 0, ends = true) + + private fun endedBySendTimeout(marker: String) { + for (mode in LetGoDriver.modes) { + LetGoDriver.naming(mode.name) { + val letGo = LetGoList() + server(LetGoDriver.config(mode, readTimeoutMs = 0), letGo).use { server -> + LetGoDriver.check( + server.proactor, + mode, + endedBySendTimeout, + { asking(server.port, marker) }, + { askedOn(server.port) }, + ) + } + } + } + } + + @Test + fun `a handler that let go with a flush in flight is ended by the send timeout - with its recv parked`() { + endedBySendTimeout("F") + } + + @Test + fun `a handler the send timeout wakes - and that returns with its input unread - gives it all back`() { + endedBySendTimeout("G") + } + + // Several clients at once, each checking its own bytes come back: a buffer in the ring twice would mix them. + private fun echoesIntact(port: Int) { + val failure = AtomicReference(null) + val threads = TestThreads() + + repeat(4) { c -> + threads.start { + try { + val client = LetGoDriver.connect(port) + try { + client.write("E") + assertEquals('E'.code, client.read(), "the echo of the marker") + + val sent = ByteArray(512) { ('a'.code + c).toByte() } + repeat(200) { round -> + client.write(sent) + val got = client.readBytes(sent.size) + assertTrue(got.contentEquals(sent), "round $round came back with another client's bytes") + } + } finally { + client.close() + } + } catch (e: Throwable) { + failure.store(e.message ?: e.toString()) + } + } + } + + threads.joinAll() + assertNull(failure.load()) + } + + @Test + fun `after let-go connections gave their buffers back connections on them are served intact`() { + for (mode in LetGoDriver.modes) { + LetGoDriver.naming(mode.name) { + val slots = 16 + val letGo = LetGoList() + val config = if (mode === LetGoDriver.SHARED) { + shared(slots, readTimeoutMs = 300) + } else { + ServerConfig( + proactorCount = 1, + ringEntries = 256, + tcp = TcpOptions(port = 0, recvQueueEntries = 64, readTimeoutMs = 300, sendTimeoutMs = 0), + incremental = IncrementalOptions(maxConnections = 32, recvSlots = 4, recvBufferSize = 4096), + ) + } + + server(config, letGo).use { server -> + // Round after round, with what the last one left, so anything lost or returned twice shows. + repeat(3) { round -> + val peers = ArrayList() + try { + if (mode === LetGoDriver.SHARED) { + holdBuffers(server.port, letGo, deliveries = slots, peerCount = 2, peers) + } else { + peers.add(letGoPeer(server.port)) + peers[0].write(ByteArray(256 * 1024)) + } + + assertTrue(askedOn(server.port), "round $round: the new client was never answered") + } finally { + peers.forEach { it.close() } + } + + assertTrue( + LetGoDriver.eventually { LetGoDriver.openConnections(server.proactor) == 0 }, + "round $round: a let-go connection is still open after its client left", + ) + letGo.clear() + + echoesIntact(server.port) + } + } + } + } + } + + @Test + fun `incremental - a client that keeps uploading after its handler let go is read to its close - not reset`() { + if (!LetGoDriver.incrementalSupported) { + println("skipped: incremental mode needs Linux 6.12, this is ${kernelRelease()}") + return + } + + val letGo = LetGoList() + val config = ServerConfig( + proactorCount = 1, + ringEntries = 256, + tcp = TcpOptions(port = 0, readTimeoutMs = 30_000), + incremental = IncrementalOptions(maxConnections = 8), + ) + + server(config, letGo).use { server -> + val client = letGoPeer(server.port) + try { + client.setSendTimeout(LetGoDriver.GRACE_MS.toInt()) + + val chunk = ByteArray(64 * 1024) + repeat(256) { + client.write(chunk) // 16 MiB: far past the ring, the queue and the socket buffers + } + } finally { + client.close() + } + + assertTrue( + LetGoDriver.eventually { LetGoDriver.openConnections(server.proactor) == 0 }, + "the connection is still open after its client left", + ) + } + } +} diff --git a/kronos/src/commonTest/kotlin/kronos/connection/tcp/LetGoHandlerStyleTest.kt b/kronos/src/commonTest/kotlin/kronos/connection/tcp/LetGoHandlerStyleTest.kt new file mode 100644 index 0000000..d013d0d --- /dev/null +++ b/kronos/src/commonTest/kotlin/kronos/connection/tcp/LetGoHandlerStyleTest.kt @@ -0,0 +1,141 @@ +package kronos + +import kotlin.test.Test + +/** + * Every way a handler can be reading when it lets go, in both ring modes, against every way its client can carry on. + * What it took has to come back through whatever holds it: the queue, or a reader's chain. + */ +class LetGoHandlerStyleTest { + private val pong = "pong".encodeToByteArray() + + private fun runCases(handler: suspend (TcpConnection) -> Unit) { + LetGoDriver.forEachCase { mode, ending -> + TestServer(LetGoDriver.config(mode, ending.readTimeoutMs), handler).use { server -> + val refuse = "RRRR".encodeToByteArray() + LetGoDriver.check( + server.proactor, + mode, + ending, + { LetGoDriver.refused(server.port, refuse) }, + { asked(server.port) }, + ) + } + } + } + + private fun asked(port: Int): Boolean = LetGoDriver.answers { + val socket = LetGoDriver.connect(port) + try { + socket.write("P") + socket.readBytes(pong.size).contentEquals(pong) + } finally { + socket.close() + } + } + + private suspend fun answer(conn: TcpConnection) { + conn.write(pong) + conn.flush() + } + + // The first byte of what comes next, every buffer handed back: -1 when the connection ended with nothing. + private suspend fun nextMarker(conn: TcpConnection): Int { + val item = RecvItem() + while (true) { + val snapshot = conn.read() + + var marker = -1 + while (conn.tryGetItem(snapshot, item)) { + if (item.hasBuffer) { + if (marker < 0) { + marker = item[0].toInt() + } + + conn.returnBuffer(item) + } + } + + if (marker >= 0 || snapshot.isClosed) { + return marker + } + } + } + + // Answers while it's asked, and stops at anything else with what it read still where it read it. + private suspend fun readWith(reader: PipeReader, conn: TcpConnection) { + while (true) { + val read = reader.read() + if (read.buffer.isEmpty || read.buffer[0L] != 'P'.code.toByte()) { + return + } + + reader.advanceTo(read.buffer.length) + answer(conn) + } + } + + @Test + fun `a handler returning from raw reads gives everything back`() = runCases { conn -> + while (nextMarker(conn) == 'P'.code) { + answer(conn) + } + } + + @Test + fun `a handler throwing from raw reads gives everything back`() = runCases { conn -> + var marker: Int + while (true) { + marker = nextMarker(conn) + if (marker != 'P'.code) { + break + } + + answer(conn) + } + + if (marker >= 0) { + throw IllegalStateException("the handler failed, for the test") + } + } + + @Test + fun `a handler leaving what it read in a pipe reader gives everything back`() = runCases { conn -> + readWith(TcpPipeReader(conn), conn) + } + + @Test + fun `a handler completing its pipe reader gives everything back`() = runCases { conn -> + val reader = TcpPipeReader(conn) + try { + readWith(reader, conn) + } finally { + reader.complete() + } + } + + @Test + fun `a handler leaving a read parked on its pipe reader gives everything back`() = runCases { conn -> + val reader = TcpPipeReader(conn) + while (true) { + val read = reader.read() + if (read.buffer.isEmpty) { + break + } + + val isAsked = read.buffer[0L] == 'P'.code.toByte() + reader.advanceTo(read.buffer.length) + if (!isAsked) { + LetGoDriver.detached { reader.read() } + break + } + + answer(conn) + } + } + + @Test + fun `a handler leaving a duplex pipe as it is gives everything back`() = runCases { conn -> + readWith(TcpDuplexPipe(conn).input, conn) + } +} diff --git a/kronos/src/nativeTest/kotlin/kronos/tls/TlsLetGoBufferTest.kt b/kronos/src/nativeTest/kotlin/kronos/tls/TlsLetGoBufferTest.kt new file mode 100644 index 0000000..b3f8c60 --- /dev/null +++ b/kronos/src/nativeTest/kotlin/kronos/tls/TlsLetGoBufferTest.kt @@ -0,0 +1,169 @@ +package kronos.tls + +import kronos.IOException +import kronos.LetGoDriver +import kronos.RecvItem +import kronos.TcpConnection +import kronos.tls.TlsPipeContractTest.TlsServer +import kronos.tls.TlsPipeContractTest.WireClient +import kotlin.test.Test + +/** + * Handlers that let go of a TLS connection with its client still sending: having answered, through the pipes or the + * raw session, on each backend; or because the handshake failed, or never finished. Nobody decrypts what follows. + */ +class TlsLetGoBufferTest { + private val request = "GET / HTTP/1.1\r\nHost: x\r\n\r\n".encodeToByteArray() + private val response = "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok".encodeToByteArray() + + private fun options( + kernelTx: Boolean = false, + kernelRx: Boolean = false, + handshakeTimeoutMs: Int = 2_000, + ) = TlsOptions( + certificatePath = TestCertificate.certificate, + keyPath = TestCertificate.key, + kernelTx = kernelTx, + kernelRx = kernelRx, + handshakeTimeoutMs = handshakeTimeoutMs, + ) + + private fun skipWithoutKernelTls(): Boolean { + if (kernelTlsAvailable()) { + return false + } + + println("skipped: the tls kernel module is not loaded (sudo modprobe tls)") + return true + } + + private fun runCases( + handler: suspend (TcpConnection, TlsService) -> Unit, + options: TlsOptions, + letGoClient: (Int) -> LetGoDriver.Peer, + ) { + LetGoDriver.forEachCase { mode, ending -> + TlsServer(LetGoDriver.config(mode, ending.readTimeoutMs), options, handler).use { server -> + LetGoDriver.check(server.proactor, mode, ending, { letGoClient(server.port) }, { served(server.port) }) + } + } + } + + private fun served(port: Int): Boolean = LetGoDriver.answers { + TlsTestClient(port).use { client -> + client.write(request) + + val got = ByteArray(response.size) + client.readFully(got, 0, got.size) + + // Through to the server's close_notify, so closing sends a FIN: with it unread the close is a reset, which + // kronos does not always notice when it follows data closely. + val rest = ByteArray(256) + while (client.read(rest, 0, rest.size) >= 0) { + // the end of the session + } + + got.contentEquals(response) + } + } + + // Asked over TLS and answered, read through to the server's end, with the raw socket left for what follows. + private fun answered(port: Int): LetGoDriver.Peer { + val client = WireClient(port, receiveTimeoutMs = LetGoDriver.GRACE_MS.toInt()) + client.write(request) + client.drainToEnd() + return LetGoDriver.Peer(client.socket) { client.close() } + } + + private val pipes: suspend (TcpConnection, TlsService) -> Unit = { conn, service -> + var tls: TlsSession? = null + try { + tls = service.accept(conn) + + val pipe = TlsDuplexPipe(conn, tls, ownsSession = false) + try { + val read = pipe.input.read() + pipe.input.advanceTo(read.buffer.length) + + pipe.output.write(response) + pipe.output.flush() + } finally { + pipe.close() + } + } catch (e: IOException) { + // A handshake that failed, as any handler would see it: it lets go on returning. + } finally { + tls?.close() + } + } + + private val rawSession: suspend (TcpConnection, TlsService) -> Unit = handler@{ conn, service -> + var tls: TlsSession? = null + try { + tls = service.accept(conn) + + val item = RecvItem() + var plaintext = tls.drainPlaintext() // the request can ride in with the client's Finished + while (plaintext == 0) { + val snapshot = conn.read() + while (plaintext == 0 && conn.tryGetItem(snapshot, item)) { + if (item.hasBuffer) { + plaintext += tls.decrypt(item.address, item.length) + conn.returnBuffer(item) + } + } + + if (plaintext == 0 && (snapshot.isClosed || tls.closed)) { + return@handler + } + } + + tls.write(conn, response) + conn.flush() + } catch (e: IOException) { + // A handshake that failed, as any handler would see it: it lets go on returning. + } finally { + tls?.close() + } + } + + @Test + fun `answered through the pipes - everything comes back`() = runCases(pipes, options(), ::answered) + + @Test + fun `answered through the raw session - everything comes back`() = runCases(rawSession, options(), ::answered) + + @Test + fun `a failed handshake - everything comes back`() = runCases(pipes, options()) { port -> + LetGoDriver.refused(port, request) + } + + @Test + fun `a handshake that timed out - everything comes back`() { + runCases(pipes, options(handshakeTimeoutMs = 200)) { port -> + // A record header promising more than ever arrives. + LetGoDriver.refused(port, byteArrayOf(0x16, 0x03, 0x01, 0x01, 0x00, 0x01)) + } + } + + @Test + fun `kernel TX - answered through the pipes - everything comes back`() { + if (!skipWithoutKernelTls()) { + runCases(pipes, options(kernelTx = true), ::answered) + } + } + + @Test + fun `kernel TX - answered through the raw session - everything comes back`() { + if (!skipWithoutKernelTls()) { + runCases(rawSession, options(kernelTx = true), ::answered) + } + } + + @Test + fun `kernel TX and RX - answered through the pipes - everything comes back`() { + if (!skipWithoutKernelTls()) { + runCases(pipes, options(kernelTx = true, kernelRx = true), ::answered) + } + } +} From 907f91337724b9305de26bff6d47c44cad681972 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Sun, 8 Nov 2026 13:14:35 +0000 Subject: [PATCH 2/2] tests: the reset TlsLetGoBufferTest's client avoids is missed by kernels without io_uring fix a68ed2df7213, not by kronos With a client's data and its reset both pending when the recv's poll runs, such a kernel takes both wakeups in one pass, returns the data and waits for a wakeup that never comes. The client still reads to the end before closing, so it closes with a FIN and the test passes on either kernel. --- kronos/src/nativeTest/kotlin/kronos/tls/TlsLetGoBufferTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kronos/src/nativeTest/kotlin/kronos/tls/TlsLetGoBufferTest.kt b/kronos/src/nativeTest/kotlin/kronos/tls/TlsLetGoBufferTest.kt index b3f8c60..a0d987a 100644 --- a/kronos/src/nativeTest/kotlin/kronos/tls/TlsLetGoBufferTest.kt +++ b/kronos/src/nativeTest/kotlin/kronos/tls/TlsLetGoBufferTest.kt @@ -56,8 +56,8 @@ class TlsLetGoBufferTest { val got = ByteArray(response.size) client.readFully(got, 0, got.size) - // Through to the server's close_notify, so closing sends a FIN: with it unread the close is a reset, which - // kronos does not always notice when it follows data closely. + // Through to the server's close_notify, so closing sends a FIN: with it unread the close is a reset, and a + // kernel without io_uring fix a68ed2df7213 (in 7.0) can miss a reset that follows data closely. val rest = ByteArray(256) while (client.read(rest, 0, rest.size) >= 0) { // the end of the session