fix(lambda): skip Runtime API ports held by other processes (#4969)

* fix(lambda): skip Runtime API ports held by other processes

PortAllocator only tracked ports handed out inside its own JVM. When
another process in the same network namespace held the lowest port of the
range, allocate() returned it anyway and RuntimeApiServer retried the same
bind for 5s before the cold start failed, while the rest of the range sat
idle. Probe each candidate by binding 0.0.0.0 (as RuntimeApiServer does)
and fall through to the next port; a held port is not kept reserved, so it
is handed out again once its holder lets go.

* fix(lambda): keep Runtime API port tests off host ports

The exhaustion message now says a port may be in use by this floci or
held by another process, since allocate() skips externally held ports and
the pool can run dry with few of its own ports out.

Pool-bookkeeping tests ran on 9200-9299 (OpenSearch's port) and 12000+,
which the bindability probe now binds for real. Make the probe injectable
through the package-private constructor and pass an always-bindable one
there, so those tests no longer depend on what is listening on the host.
The held-port test keeps the real probe and skips itself when the OS hands
out a port too close to 65535 for its range. Rewrap the class Javadoc.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-02 22:37:14 -04:00
1 parent 12e9cbd78b
commit 2e09bb4efa
3 files changed
+75 -13

No files matched your search

@@ -5,16 +5,20 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.IntPredicate;
/**
* Hands out ports from a configured range for Lambda Runtime API servers.
* Throws {@link IllegalStateException} when the range is exhausted; the
* caller releases a port back to the pool via {@link #release(int)}.
* Hands out ports from a configured range for Lambda Runtime API servers, skipping any port
* another process already holds. Throws {@link IllegalStateException} when the range is
* exhausted; the caller releases a port back to the pool via {@link #release(int)}.
*/
@ApplicationScoped
public class PortAllocator {
@@ -29,6 +33,7 @@ public class PortAllocator {
private final Set<Integer> inUse = ConcurrentHashMap.newKeySet();
private final AtomicBoolean pressureWarningEmitted = new AtomicBoolean();
private final Consumer<String> warningSink;
private final IntPredicate bindable;
@Inject
public PortAllocator(EmulatorConfig config) {
@@ -41,17 +46,30 @@ public class PortAllocator {
}
PortAllocator(int basePort, int maxPort, Consumer<String> warningSink) {
this(basePort, maxPort, warningSink, PortAllocator::isBindable);
}
PortAllocator(int basePort, int maxPort, Consumer<String> warningSink, IntPredicate bindable) {
this.basePort = basePort;
this.maxPort = maxPort;
this.poolSize = maxPort - basePort + 1;
this.pressureWarningThreshold = Math.max(1,
(int) Math.ceil(poolSize * (PRESSURE_WARNING_PERCENT / 100.0)));
this.warningSink = warningSink;
this.bindable = bindable;
}
public int allocate() {
for (int p = basePort; p <= maxPort; p++) {
if (inUse.add(p)) {
// inUse only knows this JVM's ports. A port held by anything else in the same
// network namespace (a second floci, another JVM) would leave RuntimeApiServer
// retrying a bind it cannot win while the rest of the range sits idle. A held
// port is not kept reserved: a later allocate() takes it once its holder lets go.
if (!bindable.test(p)) {
inUse.remove(p);
continue;
}
warnIfUnderPressure();
return p;
}
@@ -64,8 +82,9 @@ public class PortAllocator {
throw new IllegalStateException(
"Lambda Runtime API port pool exhausted: no free ports in range "
+ basePort + "-" + maxPort + " (" + poolSize
+ " ports, all in use). One port is held per running Lambda container, "
+ "so this is the concurrent-execution ceiling. Widen it with "
+ " ports, all in use by this floci or held by another process). One port "
+ "is held per running Lambda container, so this is the concurrent-execution "
+ "ceiling. Widen it with "
+ "floci.services.lambda.runtime-api-base-port / "
+ "floci.services.lambda.runtime-api-max-port.");
}
@@ -91,4 +110,16 @@ public class PortAllocator {
+ "before the pool is exhausted.",
(allocated * 100) / poolSize, allocated, poolSize, basePort, maxPort));
}
// Binds the same address RuntimeApiServer listens on, with SO_REUSEADDR as Vert.x sets it,
// so a port only lingering in TIME_WAIT from a stopped server still counts as free.
private static boolean isBindable(int port) {
try (ServerSocket socket = new ServerSocket()) {
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress("0.0.0.0", port));
return true;
} catch (IOException e) {
return false;
}
}
}
@@ -2,6 +2,8 @@ package io.github.hectorvent.floci.core.common.port;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@@ -10,16 +12,23 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.IntPredicate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
class PortAllocatorTest {
// Pool bookkeeping is asserted against a host where every port is free, so these tests do
// not depend on what is listening on the machine running them (9200 is OpenSearch's port).
private static final IntPredicate ALWAYS_BINDABLE = p -> true;
@Test
void allocatesSequentiallyFromBase() {
PortAllocator allocator = new PortAllocator(9200, 9299);
PortAllocator allocator = new PortAllocator(9200, 9299, w -> { }, ALWAYS_BINDABLE);
assertEquals(9200, allocator.allocate());
assertEquals(9201, allocator.allocate());
assertEquals(9202, allocator.allocate());
@@ -27,7 +36,7 @@ class PortAllocatorTest {
@Test
void concurrentAllocationsAreUnique() throws InterruptedException {
PortAllocator allocator = new PortAllocator(9200, 9299);
PortAllocator allocator = new PortAllocator(9200, 9299, w -> { }, ALWAYS_BINDABLE);
int threads = 50;
Set<Integer> ports = ConcurrentHashMap.newKeySet();
CountDownLatch latch = new CountDownLatch(threads);
@@ -47,7 +56,7 @@ class PortAllocatorTest {
@Test
void allocateNeverReturnsPortAlreadyHandedOut() {
PortAllocator allocator = new PortAllocator(9200, 9209);
PortAllocator allocator = new PortAllocator(9200, 9209, w -> { }, ALWAYS_BINDABLE);
Set<Integer> handed = new HashSet<>();
for (int i = 0; i < 10; i++) {
assertTrue(handed.add(allocator.allocate()));
@@ -57,7 +66,7 @@ class PortAllocatorTest {
@Test
void exhaustionMessageNamesThePoolAndTheWideningProperty() {
PortAllocator allocator = new PortAllocator(9200, 9200);
PortAllocator allocator = new PortAllocator(9200, 9200, w -> { }, ALWAYS_BINDABLE);
allocator.allocate();
IllegalStateException thrown = assertThrows(IllegalStateException.class, allocator::allocate);
@@ -74,7 +83,7 @@ class PortAllocatorTest {
@Test
void warnsOnceWhenPoolCrossesNinetyPercent() {
List<String> warnings = new ArrayList<>();
PortAllocator allocator = new PortAllocator(9200, 9209, warnings::add);
PortAllocator allocator = new PortAllocator(9200, 9209, warnings::add, ALWAYS_BINDABLE);
for (int i = 0; i < 10; i++) {
allocator.allocate();
@@ -89,7 +98,7 @@ class PortAllocatorTest {
@Test
void warningRearmsAfterPressureDropsBelowThreshold() {
List<String> warnings = new ArrayList<>();
PortAllocator allocator = new PortAllocator(9200, 9209, warnings::add);
PortAllocator allocator = new PortAllocator(9200, 9209, warnings::add, ALWAYS_BINDABLE);
List<Integer> ports = new ArrayList<>();
for (int i = 0; i < 9; i++) {
@@ -104,7 +113,7 @@ class PortAllocatorTest {
@Test
void releasedPortBecomesAvailableAgain() {
PortAllocator allocator = new PortAllocator(9200, 9201);
PortAllocator allocator = new PortAllocator(9200, 9201, w -> { }, ALWAYS_BINDABLE);
int first = allocator.allocate();
allocator.allocate();
assertThrows(IllegalStateException.class, allocator::allocate);
@@ -112,4 +121,24 @@ class PortAllocatorTest {
allocator.release(first);
assertEquals(first, allocator.allocate());
}
@Test
void skipsPortHeldByAnotherProcessAndReusesItOnceReleased() throws IOException {
// Holding the base port from outside the allocator stands in for a second floci (or any
// other process) in the same network namespace. Handing it out would leave the Runtime
// API server retrying a bind it can never win while the rest of the range sits idle.
int held;
PortAllocator allocator;
try (ServerSocket holder = new ServerSocket(0)) {
held = holder.getLocalPort();
assumeTrue(held <= 65535 - 50, "OS picked a port too close to 65535 for the range");
allocator = new PortAllocator(held, held + 50);
int first = allocator.allocate();
assertNotEquals(held, first, "allocate() must skip a port another process holds");
assertTrue(first > held && first <= held + 50);
}
assertEquals(held, allocator.allocate(),
"a port skipped while held must be handed out once its holder lets go");
}
}
@@ -124,7 +124,9 @@ class RuntimeApiPortPoolDefaultTest {
// configured range, so widening the default genuinely raises the concurrency ceiling.
int base = defaultOf("runtimeApiBasePort");
int max = defaultOf("runtimeApiMaxPort");
PortAllocator allocator = new PortAllocator(base, max);
// Every port counts as bindable so the assertion is about the configured width, not about
// what happens to be listening on 12000+ on the machine running the suite.
PortAllocator allocator = new PortAllocator(base, max, w -> { }, p -> true);
java.util.Set<Integer> handed = new java.util.HashSet<>();
for (int i = 0; i < MIN_USABLE_POOL_WIDTH; i++) {