Repository navigation
Add emscripten_dns_lookup_async / emscripten_dns_lookup_result - #27742
guybedford wants to merge 3 commits into
Conversation
85df785 to
1922c3d
Compare
The synchronous getaddrinfo has nothing to block on under -sNODERAWSOCKETS and fails a hostname lookup with EAI_AGAIN, so the blocking-pool resolver never resolved a name on emscripten. Its emscripten_dns_lookup_async (emscripten-core/emscripten#27742) returns an fd readable once the lookup completes, awaited through the I/O driver like any other; the ToSocketAddrs string forms route there on the target, and the localhost lookup and connect tests run again.
The synchronous getaddrinfo has nothing to block on under -sNODERAWSOCKETS and fails a hostname lookup with EAI_AGAIN, so the blocking-pool resolver never resolved a name on emscripten. Its emscripten_dns_lookup_async (emscripten-core/emscripten#27742) returns an fd readable once the lookup completes, awaited through the I/O driver like any other; the ToSocketAddrs string forms route there on the target, and the localhost lookup and connect tests run again.
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.
|
I'm not sure about using functions-that-return-file-descriptors as general model for async programming in emscripten. How about we design the API to return a promise handle? They separately we could find some way to add integrate promise handles into epoll? Actually would that even be necessary? Can't promise-resolution callback run even while waiting for epoll events? (i.e. do these DNS requests, or promise resolutions in general, have the feed trough the epoll system, or can they just work alongside it?) |
Right a promise handle can't work with epoll_callback, so must attach to thread-local promise callbacks instead. Tokio on the other hand does in fact assume DNS futures are So we would need to likely pick either of:
|
|
I've together a design for a unified async handling system in the stack of PRs ending at #27881 which then supersedes this PR. The main approach is #27880 which allows |
This adds an asynchronous
getaddrinfoon top of thegetaddrinfobody from #27693, so a real DNS lookup under-sNODERAWSOCKETScan be awaited without blocking from any stack. Recreates the async API from #27182 on the landed approach.getaddrinfo()underNODERAWSOCKETScan only wait for anode:dnslookup from a sync-proxied pthread or withASYNCIFY/JSPI; on the main thread of a plain build it returnsEAI_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:0with a newly allocatedaddrinfolist in*res(freed withfreeaddrinfo), anEAI_*code, orEAI_AGAINwhile pending. Nothing is allocated until a result is read, so the fd owns no C memory:dupshares the lookup andclosehas nothing to free.polland wait-queue node, so it needs nothing fromSOCKFS. WithoutNODERAWSOCKETSthe API works the same, resolving synchronously.The
getaddrinfobody becomes$getAddrInfo, which allocates nothing and returns anEAI_*code or a descriptor (with alookupthunk for a hostname underNODERAWSOCKETS);$writeAddrInfomints theaddrinfolist at the point ownership passes to the caller.getaddrinfokeeps its proxied /Asyncify.handleAsync/EAI_AGAINdispatch unchanged.Tests:
sockets_node.test_noderawsockets_dns_async(withPROXY_TO_PTHREAD: numeric and error results readable at once, repeated reads each minting their own list, close without reading,dupsharing the lookup, close while pending, and a reallocalhostlookup awaited via non-blockingpollretries from the main thread or a blockingpollfrom the pthread) andother.test_dns_lookup_asyncfor the default build.Made with AI assistance under my review