Skip to content

Improve how we detect database port is free #4762

Description

@infomiho

When you run nc -l 5432 on the dev-database port:

$ lsof -nP -i :5432 | grep LISTEN
nc      67588 ilakovac    3u  IPv4 0x2b284e848f681f22      0t0  TCP *:5432 (LISTEN)
nc      69683 ilakovac    3u  IPv4 0x8350f3e3a67bb38f      0t0  TCP *:5432 (LISTEN)

wasp doctor claims the port is free:

$ wasp doctor
[✓] Dev database port (5432): free

and starting the dev database dies with a raw runtime error instead of the intended friendly message:

$ wasp start db
wasp-cli: Network.Socket.connect: <socket: 147>: timeout (Operation timed out)
HasCallStack backtrace:
  bracket, called at lib/System/IO/Utf8.hs:154:24 in wth-tf8-1.1.0.0-1dd5724a:System.IO.Utf8

exit=1

Cause: two stacked probe flaws around port 5432:

  1. Doctor's only probe is checkIfPortIsInUse (src/Wasp/Util/Network/Socket.hs:37), which binds 127.0.0.1:PORT with SO_REUSEADDR set - that succeeds under a wildcard (*:5432) listener, so the port reads as free. start db knows about this hole and adds a second probe (checkIfPortIsAcceptingConnections, comment at cli/src/Wasp/Cli/Command/Start/Db.hs:146)...
  2. ...but that probe connects, and any exception other than ECONNREFUSED is rethrown (Socket.hs:24). Against nc -l the connect times out; the resulting IOException escapes both the requirement wrapper and Main.hs's ErrorCall-only handler, producing the crash above. The clean [Error] Port already in use path only triggers for ports that refuse connections outright.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions