Files
floci/src/main/java
Jerald Lindberg bf517ac952 fix(elbv2): do not let a listener bind a port Floci reserves for itself (#3034)
* fix(elbv2): do not let a listener bind a port Floci reserves for itself

The ELBv2 data plane binds each listener's port for real, and the TLS proxy
binds the emulator port plus floci.tls.aws-https-port (443 by default) because
CDK's custom-resource cfn-response callback hardcodes https:// and ignores the
port in the ResponseURL. Nothing arbitrated between them, so the two raced and
whichever started first won.

That made the outcome depend on start order. On a fresh start the proxy took
443 and the load balancer logged a bind failure; on a start that restored a
persisted ALB with an HTTPS listener, the listener took 443 and answered CDK's
callback in plain HTTP, so every Custom:: resource hung until CloudFormation
timed out - with only a warning to explain it.

startPortServer now yields a reserved port, registering the listener on the
control plane but leaving it without a data plane, and says so. Load balancers
can still have 443 by setting floci.tls.aws-https-port=0 or disabling TLS.

* fix(elbv2): yield 443 only while the TLS proxy actually holds it

The reserved-port check read configuration, not state. Binding the AWS-HTTPS
port is privileged and its failure is deliberately non-fatal, so a proxy that
lost the bind still had the port marked reserved and the ELBv2 listener also
declined it. The port then went unserved by either side, which is worse than
the race the reservation exists to settle.

TlsProxyServer now records which binds failed and exposes reservesPort(), and
ElbV2DataPlane asks it instead of inferring from config. An unresolved bind
still reserves, so a listener cannot win the start-up race by arriving first.

Adds TlsProxyServerReservedPortTest for the bind-outcome half of the contract:
reserved while unresolved, released once failed, and nothing reserved when TLS
is off or aws-https-port is 0.

* fix(elbv2): tell the operator the right way to free a reserved port

The warning always suggested floci.tls.aws-https-port=0, which is not a remedy
when the reserved port is the emulator's own: that one cannot be handed over at
all. Pick the advice from which port was asked for.

* fix(elbv2): rebind a listener when the proxy gives up its port

Yielding a port whose bind had not resolved yet is the safe default, but if
that bind then failed nothing retried the listener: startPortServer had
returned null, so no server was stored and the listener stayed registered with
no data plane while the port sat free.

TlsProxyServer now notifies on a failed bind, and the data plane binds any
listener already registered on that port. Handlers registered after the fact
are replayed the ports already given up, since the data plane only wires itself
in once a listener exists.

Pinned by notifiesHandlersWhenABindFails, which resolves listen() to a failure
and asserts the port is both released and reported.

* fix(elbv2): subscribe to released ports outside the server map's mapping function

startPortServer registered the proxy's release handler on first use, from inside
servers.computeIfAbsent. onPortReleased replays the ports already given up as it
subscribes, so when the AWS-HTTPS bind had already failed before any listener
existed, that replay ran bindListenersWaitingOn for the same port, which called
servers.computeIfAbsent again for the key the outer call was still computing.
ConcurrentHashMap rejects that outright: IllegalStateException: Recursive update,
straight out of startListener.

Registration moves to the callers, after the listener is in listenersByHostAndPort
so the replay still finds it, and before the server map is touched so nothing
re-enters it. The mapping function now only builds a server.

Pinned by aListenerStartsOnAPortTheProxyGaveUpBeforeItSubscribed, which throws
Recursive update when the registration is moved back inside.
2026-09-06 10:35:16 -04:00
..