Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions vm/backend/src/com/codename1/backend/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -6372,6 +6397,9 @@ boolean fill() throws IOException {
buffer = direct;
pos = 0;
borrowed = true;
if (host != null) {
host.borrower = this;
}
return true;
}
return fillCopying();
Expand Down Expand Up @@ -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;
Expand Down
Loading