Skip to content

Conversation

@jdetter
Copy link
Collaborator

@jdetter jdetter commented Jan 12, 2026

Description of Changes

  • Small fix for checking to see if a port is available on some given interface.

updated:

The original implementation here used bind to try to discover if a port is currently in use. This isn't reliable due to platform differences - especially on windows where it's apparently acceptable to have a service running on both 0.0.0.0:3000 and 127.0.0.1:3000. This would cause bind to return successfully when we wanted it to fail. Also: binding on an ipv6 interface when a machine doesn't have ipv6 enabled caused random errors and it was too unreliable to be useful.

This new implementation uses get_socket_info which returns info on all sockets in use on the system. We can then look through this list to find services which conflict with our requested port.

updated 1/14:

This PR now includes a fix for flaky CLI tests. Originally we were using find_free_port to pick a random free port, but that was causing a race condition which resulted in test flakes. This PR fixes this issue by using 127.0.0.1:0 as the listen addr so the kernel will automatically pick a free port for us.

API and ABI breaking changes

None

Expected complexity level and risk

1 - this is a pretty isolated check, unlikely to introduce larger issues.

Testing

I tested on macos, windows and linux:

ALLOW
docusaurus is already running on   127.0.0.1:3000
SpacetimeDB then tries to start on 192.168.1.10:3000

ALLOW
docusaurus is already running on   ::1:3000
SpacetimeDB then tries to start on 192.168.1.10:3000

DENY
docusaurus is already running on   ::1:3000
SpacetimeDB then tries to start on 127.0.0.1:3000

DENY
docusaurus is already running on   0:0:0:0:0:0:0:0:3000
SpacetimeDB then tries to start on 0.0.0.0:3000

DENY
docusaurus is already running on   0:0:0:0:0:0:0:0:3000
SpacetimeDB then tries to start on 127.0.0.1:3000

DENY
docusaurus is already running on   0:0:0:0:0:0:0:0:3000
SpacetimeDB then tries to start on 192.168.1.10:3000

DENY
docusaurus is already running on   127.0.0.1:3000
SpacetimeDB then tries to start on 0:0:0:0:0:0:0:0:3000

DENY
docusaurus is already running on   192.168.1.10:3000
SpacetimeDB then tries to start on 0:0:0:0:0:0:0:0:3000

@jdetter jdetter marked this pull request as ready for review January 13, 2026 09:18
@jdetter jdetter force-pushed the jdetter/is-port-available-fix branch from 4ef42b4 to d10e7f5 Compare January 14, 2026 07:18
@jdetter jdetter changed the base branch from tyler/claude-docs to master January 14, 2026 07:18
None => return false, // invalid host string => treat as not available
};

let sockets = match get_sockets_info(AddressFamilyFlags::IPV4 | AddressFamilyFlags::IPV6, ProtocolFlags::TCP) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This grabs the socket info from the kernel instead of manually trying to bind to the port on the ipv4/ipv6 interfaces.

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.

4 participants