mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
immediately closing a ServerSocket. Between that check and Docker binding the port, a second concurrent RunInstances call saw the same port as free — both instances raced to bind 2200 and the second one failed with "port is already allocated". Fix: PortAllocator now maintains an in-memory ConcurrentSkipListSet of reserved ports. allocate() is synchronized and adds the chosen port to the set before returning, so concurrent callers skip it even before Docker has bound it. release() removes the entry when the container is terminated, making the port available for future instances. Ec2ContainerManager.terminate() now calls portAllocator.release() after the Docker container is removed. Add PortAllocatorTest with 5 cases including a concurrency test that races 20 threads simultaneously to confirm uniqueness. Fixes #721