Skip to content

Add emscripten_dns_lookup_async / emscripten_dns_lookup_result - #27742

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

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

Conversation

@guybedford

Copy link
Copy Markdown
Collaborator

This adds an asynchronous getaddrinfo on top of the getaddrinfo body from #27693, so a real DNS lookup under -sNODERAWSOCKETS can be awaited without blocking from any stack. Recreates the async API from #27182 on the landed approach.

getaddrinfo() under NODERAWSOCKETS can only wait for a node:dns lookup from a sync-proxied pthread or with ASYNCIFY/JSPI; on the main thread of a plain build it returns EAI_AGAIN. Event-loop reactors need a non-blocking form.

  • emscripten_dns_lookup_async(node, service, hints) takes the same inputs and returns an fd that becomes readable (poll/select/epoll) once the lookup completes; numeric addresses and validation errors are readable on return. A pending lookup holds the runtime like a timer.
  • emscripten_dns_lookup_result(fd, &res) reads the outcome: 0 with a newly allocated addrinfo list in *res (freed with freeaddrinfo), an EAI_* code, or EAI_AGAIN while pending. Nothing is allocated until a result is read, so the fd owns no C memory: dup shares the lookup and close has nothing to free.
  • The fd is a plain FS stream with its own poll and wait-queue node, so it needs nothing from SOCKFS. Without NODERAWSOCKETS the API works the same, resolving synchronously.

The getaddrinfo body becomes $getAddrInfo, which allocates nothing and returns an EAI_* code or a descriptor (with a lookup thunk for a hostname under NODERAWSOCKETS); $writeAddrInfo mints the addrinfo list at the point ownership passes to the caller. getaddrinfo keeps its proxied / Asyncify.handleAsync / EAI_AGAIN dispatch unchanged.

Tests: sockets_node.test_noderawsockets_dns_async (with PROXY_TO_PTHREAD: numeric and error results readable at once, repeated reads each minting their own list, close without reading, dup sharing the lookup, close while pending, and a real localhost lookup awaited via non-blocking poll retries from the main thread or a blocking poll from the pthread) and other.test_dns_lookup_async for the default build.

Made with AI assistance under my review

An asynchronous getaddrinfo that completes through a pollable fd, so a real
node:dns lookup under -sNODERAWSOCKETS can be awaited without blocking from
any stack. The getaddrinfo body becomes $getAddrInfo, which allocates nothing
and returns a descriptor; $writeAddrInfo mints the addrinfo list at the point
ownership passes to the caller.
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