Skip to content

Force SOCK_STREAM in getAddrInfo hints to fix macOS/BSD connect bug#29

Merged
mzabani merged 1 commit into
mzabani:masterfrom
luntain:master
Jul 2, 2026
Merged

Force SOCK_STREAM in getAddrInfo hints to fix macOS/BSD connect bug#29
mzabani merged 1 commit into
mzabani:masterfrom
luntain:master

Conversation

@luntain

@luntain luntain commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Socket.defaultHints leaves addrSocketType as NoSocketType, so getaddrinfo() is free to return entries in any order for the requested host/port. On macOS this can return a SOCK_DGRAM (UDP) entry before the SOCK_STREAM (TCP) one for the same loopback host/port; internalConnectOrCancel takes the first result unconditionally, so hpgsql ends up opening a UDP socket to Postgres.

connect()/send() on that UDP socket "succeed" (UDP has no handshake), so the startup message appears to be sent successfully. The failure only surfaces on the first recv(), as ECONNREFUSED from an ICMP port-unreachable reply, since nothing is listening on that UDP port. Postgres never sees any connection attempt when this happens.

Explicitly requesting addrSocketType = Stream removes the UDP entries from getAddrInfo's results.

This fixes issue #28

Socket.defaultHints leaves addrSocketType as NoSocketType, so
getaddrinfo() is free to return entries in any order for the
requested host/port. On macOS this can return a SOCK_DGRAM (UDP)
entry before the SOCK_STREAM (TCP) one for the same loopback
host/port; internalConnectOrCancel takes the first result
unconditionally, so hpgsql ends up opening a UDP socket to Postgres.

connect()/send() on that UDP socket "succeed" (UDP has no handshake),
so the startup message appears to be sent successfully. The failure
only surfaces on the first recv(), as ECONNREFUSED from an ICMP
port-unreachable reply, since nothing is listening on that UDP port.
Postgres never sees any connection attempt when this happens.

Explicitly requesting addrSocketType = Stream removes the UDP entries
from getAddrInfo's results.
@mzabani

mzabani commented Jul 2, 2026

Copy link
Copy Markdown
Owner

Thank you very much for creating the ticket and even a PR. The PR's summary is well written, and the change looks good and passes CI and in my Linux computer.

Are you happy with the PR in its current state? I can approve and merge if you are.

Also, can you use hpgsql from master, or does it help if I release a new version in hackage? I can try to make a release tomorrow or next week if one is needed.

@luntain

luntain commented Jul 2, 2026 via email

Copy link
Copy Markdown
Contributor Author

@mzabani
mzabani merged commit ee80c0a into mzabani:master Jul 2, 2026
3 checks passed
@mzabani

mzabani commented Jul 2, 2026

Copy link
Copy Markdown
Owner

Good point, I will try to release a new version as soon as I can.

Thank you very much for your contribution, and feel free to keep them coming.

@mzabani

mzabani commented Jul 3, 2026

Copy link
Copy Markdown
Owner

New version with this fix just published to Hackage.

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.

2 participants