Skip to content

Real DNS resolution for getaddrinfo under -sNODERAWSOCKETS - #27693

Open
guybedford wants to merge 1 commit into
emscripten-core:mainfrom
guybedford:noderawsockets-dns
Open

Real DNS resolution for getaddrinfo under -sNODERAWSOCKETS#27693
guybedford wants to merge 1 commit into
emscripten-core:mainfrom
guybedford:noderawsockets-dns

Conversation

@guybedford

Copy link
Copy Markdown
Collaborator

This gives getaddrinfo() real name resolution under -sNODERAWSOCKETS. Split out from #27182, which also added a new asynchronous DNS API; this PR is only the change to the existing blocking API, allowing any async API work to follow separately.

Previously every hostname resolved to a fake 172.29.x.x address, which is fine for the websocket proxy transport but not for real Node.js sockets, where the address has to be connectable.

  • /etc/hosts entries (read fresh through the emscripten FS, so MEMFS or a mounted file works) resolve synchronously.
  • Any other hostname resolves via node:dns. The lookup is asynchronous, so getaddrinfo blocks by returning a Promise where the calling stack can wait on it: a sync-proxied pthread (PROXY_SYNC_ASYNC, e.g. main() under PROXY_TO_PTHREAD), or ASYNCIFY/JSPI by suspending. Numeric and /etc/hosts results still return synchronously, so JSPI does not pay a microtask on those.
  • Where there is no stack that can wait (the event-loop thread itself), a hostname needing DNS returns EAI_AGAIN.
  • Results may now be an addrinfo linked list (multiple /etc/hosts entries, multiple DNS records, AF_UNSPEC), and freeaddrinfo walks the whole chain.

Everything is gated on NODERAWSOCKETS; without it getaddrinfo is unchanged.

Implementation-wise the existing body becomes $doGetAddrInfo, which returns either an EAI_* code or a thunk producing a Promise of one, and getaddrinfo is a thin dispatcher on that. The thunk form keeps the ASYNCIFY rewind re-invocation from starting a second lookup.

Tested with test_noderawsockets_dns (numeric, /etc/hosts aliases and multi-address lists across families, EAI_AGAIN single-threaded), test_noderawsockets_dns_blocking under PROXY_TO_PTHREAD and test_noderawsockets_dns_blocking_jspi, plus ASYNCIFY and ASYNCIFY + PROXY_TO_PTHREAD manually.

Made with AI assistance under my review

/etc/hosts entries (read through the emscripten FS) resolve synchronously,
other hostnames resolve via node:dns, blocking where the calling stack can
wait (a proxied pthread via PROXY_SYNC_ASYNC, ASYNCIFY/JSPI by suspending)
and returning EAI_AGAIN otherwise. Results may be a linked list, which
freeaddrinfo now frees in full.
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