fix(lambda): make PortAllocator a checkout/release pool (#936)

* test(lambda): assert PortAllocator never reuses a port still in use

* fix(lambda): make PortAllocator a checkout/release pool

The previous modular counter (basePort + offset % range) wrapped
silently when allocations exceeded the configured range, handing the
same port to two RuntimeApiServers. The second listen() either failed
with EADDRINUSE or bound on top of a draining socket (in both cases
producing a runtime API the Lambda bootstrap could not reach).

PortAllocator now tracks in-use ports in a Set and throws
IllegalStateException on exhaustion. RuntimeApiServerFactory releases
the port on start failure; ContainerLauncher releases it after the
server is stopped.
This commit is contained in:
Steve authored and GitHub committed 2026-05-19 18:01:19 -05:00
1 parent c0d6b71eb1
commit 040ef033b4
4 files changed
+53 -14

No files matched your search

@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.core.common.port;
import org.junit.jupiter.api.Test;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
@@ -39,4 +40,25 @@ class PortAllocatorTest {
executor.shutdown();
assertEquals(threads, ports.size(), "All allocated ports must be unique");
}
@Test
void allocateNeverReturnsPortAlreadyHandedOut() {
PortAllocator allocator = new PortAllocator(9200, 9209);
Set<Integer> handed = new HashSet<>();
for (int i = 0; i < 10; i++) {
assertTrue(handed.add(allocator.allocate()));
}
assertThrows(IllegalStateException.class, allocator::allocate);
}
@Test
void releasedPortBecomesAvailableAgain() {
PortAllocator allocator = new PortAllocator(9200, 9201);
int first = allocator.allocate();
allocator.allocate();
assertThrows(IllegalStateException.class, allocator::allocate);
allocator.release(first);
assertEquals(first, allocator.allocate());
}
}