From c55368864c947956d4af8d322da5929e56c5b4ce Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Mon, 30 Nov 2026 08:16:15 +0200 Subject: [PATCH] Backend: the zero-copy reader evicts the connection still borrowing the host buffer The read buffer is per host, and advance() privatised a connection that stopped mid-request only if it could find it by descriptor afterwards. A pipelined pair still came back 400 duplicate Host header on CI. Before a zero-copy read, a different connection still borrowing the buffer now gets its own copy, whatever path switched it out. Co-Authored-By: Claude Opus 5.5 --- .../src/com/codename1/backend/HttpServer.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/vm/backend/src/com/codename1/backend/HttpServer.java b/vm/backend/src/com/codename1/backend/HttpServer.java index 7c29f322f36..d886155f6a6 100644 --- a/vm/backend/src/com/codename1/backend/HttpServer.java +++ b/vm/backend/src/com/codename1/backend/HttpServer.java @@ -4003,6 +4003,22 @@ static void serveVirtual(int fd) { private static final class VtHost { final Reactor poller; + /// The connection whose request currently lives in this host's zero-copy + /// read buffer, or null. + /// + /// That buffer is per HOST, not per connection, so the next read by ANY + /// virtual thread on the host overwrites it. advance() privatises the + /// buffer of a connection that stops mid-request, but that depends on + /// finding the connection again by descriptor after the switch, and a + /// pipelined pair still came back "400 duplicate Host header" on CI: the + /// second request parsed out of bytes another connection's read had + /// written. Rather than close one more switch path, the READER evicts: + /// before a zero-copy read, a different connection still borrowing the + /// buffer is given its own copy first (Conn.evictOtherBorrower). That + /// holds however the switch happened, and copies only on a real overlap. + /// Only the host's own virtual threads touch it, so it needs no lock. + Conn borrower; + /// Virtual threads ready to run, as a preallocated ring of raw handles. /// /// NOT a LinkedList of boxed Longs, and this is the single most important @@ -4280,6 +4296,9 @@ void setConn(int fd, Conn conn) { } ensureCapacity(fd); connByFd[fd] = conn; + if (conn != null) { + conn.host = this; + } } Conn connOf(int fd) { @@ -5855,6 +5874,9 @@ private static final class Conn { int pos; /// True while `buffer` is the thread's shared buffer rather than ours. boolean borrowed; + /// The virtual-thread host this connection reads on; null in pool mode, + /// where each pthread's buffer only ever serves the connection it blocks on. + VtHost host; /// True once this request's header slices name positions in `buffer`. /// /// "Is anything still pointing at this buffer" is the question fill() has to @@ -6306,6 +6328,7 @@ boolean fill() throws IOException { // // Plaintext only (session == 0): a TLS read decrypts through its // own path and does not hand back a buffer we own. + evictOtherBorrower(); byte[] direct = ServerSocket.readIntoThreadBuffer(fd, READ_CHUNK); if (direct == null) { return false; @@ -6351,6 +6374,8 @@ boolean fill() throws IOException { if (!ServerSocket.parkUntilReadable(fd)) { return false; } + // The park ran other connections, and one may borrow now. + evictOtherBorrower(); direct = ServerSocket.readIntoThreadBuffer(fd, READ_CHUNK); if (direct == null) { return false; @@ -6372,6 +6397,9 @@ boolean fill() throws IOException { buffer = direct; pos = 0; borrowed = true; + if (host != null) { + host.borrower = this; + } return true; } return fillCopying(); @@ -6660,6 +6688,22 @@ void releaseIdleMemory() { } } + /// Before this connection reads into the host's shared buffer, give the + /// connection that still borrows it a copy of its own. See VtHost.borrower. + /// That connection is suspended on this same host, so nothing reads its + /// buffer while it is replaced -- the same thing advance() does. + private void evictOtherBorrower() { + VtHost h = host; + if (h == null) { + return; + } + Conn other = h.borrower; + if (other != null && other != this && other.borrowed) { //NOPMD CompareObjectsWithEquals - connections are compared by identity + other.detachPreservingOffsets(); + } + h.borrower = null; + } + void releaseBorrowed() { if (!borrowed) { return;