Skip to content

Fix SelectingIpDatagramSocket option and close handing - #3764

Open
reardonj wants to merge 5 commits into
typelevel:mainfrom
reardonj:3762-datagram-not-closed
Open

Fix SelectingIpDatagramSocket option and close handing#3764
reardonj wants to merge 5 commits into
typelevel:mainfrom
reardonj:3762-datagram-not-closed

Conversation

@reardonj

@reardonj reardonj commented Sep 3, 2026

Copy link
Copy Markdown

Fixes two issues with SelectingIpDatagramSockets from #3762:

  • Options not applied before binding: Some UDP socket options (eg. SO_REUSEPORT) must be set before the socket is bound to a port. SelectingIpDatagramSocketsProvider was waiting until after bind to set options, making these options ineffective. All options can be set before binding, so we just need to move up the setOptions before the bind.
  • No wait for selector channels to be deregistered when cleaning up: Duplicated the wait logic from SelectingIpSocketsProvider for UDP as well, so we wait for the socket to be unregistered, as the port will not actually be available for reuse until after this. I also simpified the logic in both places to use whileM_ instead of an explicit tailrec loop. Note: It may still be unavailable at the OS level, so it is best to also use the SO_REUSEADDR option (SocketOption.reuseAddress(true)) if you really want to close and reopen a UDP socket immediately.

Some UDP socket options (eg. SO_REUSEPORT) must be set before the socket is bound to a port. `SelectingIpDatagramSocketsProvider` was waiting until after bind to set options, making these options ineffective. All options can be set before binding, so we just need to move up the setOptions before the bind.
Duplicate the wait logic from SelectingIpSocketsProvider for UDP as well, so we wait for the socket to be unregistered, as the socket will not actually be available for reuse until after this.

Note: It may still be unavailable at the operating system level, so it is best to also use the SO_REUSEADDR option (`SocketOption.reuseAddress(true)`) if you really want to close and reopen a UDP socket immediately
@reardonj
reardonj force-pushed the 3762-datagram-not-closed branch from 2bc3021 to 5ef9823 Compare September 3, 2026 15:11
JS does not support socket options, so we can't add the option to reuse the port, a whick point the OS may fail to release it in a timely fashion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant