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;
}
}
}