Close unregistered TCP channels on setup failure
Fixed Apache Pekko TCP cleanup so outgoing connection setup failures before ChannelRegistration no longer leave the underlying SocketChannel open.
apache/pekko · #3273
Problem
If a TCP connection failed during setup before the selector delivered ChannelRegistration, TcpConnection could report CommandFailed and stop the actor while leaving the SocketChannel open. Under repeated failure rates, cleanup depended on GC finalization and could leak file descriptors.
Approach
Updated TcpConnection.postStop to close still-open channels directly when registration is empty, while preserving the existing cancelAndClose path for registered channels. Extended the failing beforeConnect socket-option integration test to capture the socket and assert it closes after CommandFailed.
Impact and scope
- Prevents file descriptor leaks in Apache Pekko TCP client setup failure paths.
- Preserves the existing registered-channel cleanup path while adding deterministic cleanup for pre-registration failures.
- Improves resilience for reactive and actor-based systems that may see high connection-failure rates.
Validation
- Extended TcpIntegrationSpec to verify the captured Socket is closed after CommandFailed.
- PR validation covered the focused TcpIntegrationSpec test for the TCP setup failure path.
- Formatting checks covered actor and actor-tests scalafmt, with git diff checks reported in the PR body.