-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Support blocking socket ops via a single fd_wait primitive #27342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
guybedford
wants to merge
5
commits into
emscripten-core:main
Choose a base branch
from
guybedford:blocking-socket-ops
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1a9524a
Support pthread blocking socket ops via a single fd_wait primitive
guybedford 4430421
Declare _emscripten_fd_wait in emscripten_internal.h for gen_sig_info
guybedford b740cea
Address review: fd_wait in libfs.js, boolean result, ASYNCIFY/JSPI su…
guybedford c1a7e9e
rebaseline
guybedford 3c24e29
Keep upstream musl bodies under #else; hoist fd_wait early-outs out o…
guybedford File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Copyright 2026 The Emscripten Authors. All rights reserved. | ||
| * Emscripten is available under two separate licenses, the MIT license and the | ||
| * University of Illinois/NCSA Open Source License. Both these licenses can be | ||
| * found in the LICENSE file. | ||
| */ | ||
|
|
||
| #ifndef EMSCRIPTEN_FD_WAIT_H | ||
| #define EMSCRIPTEN_FD_WAIT_H | ||
|
|
||
| // Blocking socket data ops on emscripten: the underlying JS syscalls are | ||
| // strictly synchronous and return -EAGAIN when they would block. The network | ||
| // wrappers then wait for readiness via _emscripten_fd_wait and retry. The wait | ||
| // itself only succeeds where a stack can be suspended - a proxied pthread | ||
| // worker parked on its sync-proxy, or an ASYNCIFY/JSPI stack - and only for a | ||
| // blocking fd; otherwise it returns false and the EAGAIN surfaces unchanged. | ||
|
|
||
| #include <errno.h> | ||
| #include <poll.h> | ||
| #include "emscripten_internal.h" | ||
| #include "syscall.h" | ||
|
|
||
| // `attempt` is a strictly synchronous __socketcall_cp expression returning | ||
| // -EAGAIN when it would block; `dontwait` (MSG_DONTWAIT) suppresses the wait. | ||
| // Yields the raw syscall result; callers apply __syscall_ret. | ||
| #define __emscripten_sock_retry(fd, dontwait, attempt) ({ \ | ||
| long __r; \ | ||
| for (;;) { \ | ||
| __r = (attempt); \ | ||
| if (__r != -EAGAIN || (dontwait) || !_emscripten_fd_wait(fd, POLLIN)) \ | ||
| break; \ | ||
| } \ | ||
| __r; \ | ||
| }) | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| #include <sys/socket.h> | ||
| #include "syscall.h" | ||
| #ifdef __EMSCRIPTEN__ | ||
| #include "emscripten_fd_wait.h" | ||
| #endif | ||
|
|
||
| int accept(int fd, struct sockaddr *restrict addr, socklen_t *restrict len) | ||
| { | ||
| #ifdef __EMSCRIPTEN__ | ||
| return __syscall_ret(__emscripten_sock_retry(fd, 0, | ||
| __socketcall_cp(accept, fd, addr, len, 0, 0, 0))); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you keep use |
||
| #else | ||
| return socketcall_cp(accept, fd, addr, len, 0, 0, 0); | ||
| #endif | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| #include <sys/socket.h> | ||
| #include "syscall.h" | ||
| #ifdef __EMSCRIPTEN__ | ||
| #include "emscripten_fd_wait.h" | ||
| #endif | ||
|
|
||
| ssize_t recvfrom(int fd, void *restrict buf, size_t len, int flags, struct sockaddr *restrict addr, socklen_t *restrict alen) | ||
| { | ||
| #ifdef __EMSCRIPTEN__ | ||
| return __syscall_ret(__emscripten_sock_retry(fd, flags & MSG_DONTWAIT, | ||
| __socketcall_cp(recvfrom, fd, buf, len, flags, addr, alen))); | ||
| #else | ||
| return socketcall_cp(recvfrom, fd, buf, len, flags, addr, alen); | ||
| #endif | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * Copyright 2026 The Emscripten Authors. All rights reserved. | ||
| * Emscripten is available under two separate licenses, the MIT license and the | ||
| * University of Illinois/NCSA Open Source License. Both these licenses can be | ||
| * found in the LICENSE file. | ||
| * | ||
| * accept4(SOCK_NONBLOCK) must yield a non-blocking accepted socket even off a | ||
| * *blocking* listener: the flag is applied on top of the flags inherited from | ||
| * the listener, not dropped. A poll()-driven main loop (single-threaded, zero | ||
| * timeout) waits for the incoming connection, accept4()s it with SOCK_NONBLOCK, | ||
| * then checks F_GETFL reports O_NONBLOCK and that a data-less recv() would-block | ||
| * with EAGAIN rather than hanging. Plain POSIX, so it also runs natively. | ||
| */ | ||
|
|
||
| #include <arpa/inet.h> | ||
| #include <assert.h> | ||
| #include <errno.h> | ||
| #include <fcntl.h> | ||
| #include <netinet/in.h> | ||
| #include <poll.h> | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
| #include <sys/socket.h> | ||
| #include <unistd.h> | ||
|
|
||
| #ifdef __EMSCRIPTEN__ | ||
| #include <emscripten.h> | ||
| #endif | ||
|
|
||
| static int listen_fd = -1; | ||
| static int client_fd = -1; | ||
| static int peer_fd = -1; | ||
|
|
||
| static void finish(void) { | ||
| if (client_fd >= 0) close(client_fd); | ||
| if (peer_fd >= 0) close(peer_fd); | ||
| if (listen_fd >= 0) close(listen_fd); | ||
| printf("done\n"); | ||
| #ifdef __EMSCRIPTEN__ | ||
| emscripten_cancel_main_loop(); | ||
| #endif | ||
| } | ||
|
|
||
| static void main_loop(void) { | ||
| struct pollfd pfd = { .fd = listen_fd, .events = POLLIN }; | ||
| if (poll(&pfd, 1, 0) <= 0 || !(pfd.revents & POLLIN)) { | ||
| return; // no connection queued yet | ||
| } | ||
|
|
||
| // The listener is blocking (never marked O_NONBLOCK), so inheritance alone | ||
| // would give a blocking socket; SOCK_NONBLOCK must override that. | ||
| peer_fd = accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK); | ||
| assert(peer_fd >= 0); | ||
|
|
||
| int fl = fcntl(peer_fd, F_GETFL); | ||
| assert(fl >= 0 && (fl & O_NONBLOCK) && "accept4 SOCK_NONBLOCK not honored"); | ||
|
|
||
| // A non-blocking recv with no data pending returns EAGAIN immediately instead | ||
| // of blocking, confirming the fd is really non-blocking. | ||
| char buf[4]; | ||
| ssize_t n = recv(peer_fd, buf, sizeof(buf), 0); | ||
| assert(n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)); | ||
|
|
||
| finish(); | ||
| } | ||
|
|
||
| int main(void) { | ||
| listen_fd = socket(AF_INET, SOCK_STREAM, 0); | ||
| assert(listen_fd >= 0); | ||
|
|
||
| struct sockaddr_in addr; | ||
| memset(&addr, 0, sizeof(addr)); | ||
| addr.sin_family = AF_INET; | ||
| inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); | ||
| assert(bind(listen_fd, (struct sockaddr*)&addr, sizeof(addr)) == 0); | ||
| socklen_t l = sizeof(addr); | ||
| assert(getsockname(listen_fd, (struct sockaddr*)&addr, &l) == 0); | ||
| assert(listen(listen_fd, 4) == 0); | ||
| // Deliberately leave listen_fd blocking to prove SOCK_NONBLOCK is applied on | ||
| // top of the inherited (blocking) listener flags. | ||
|
|
||
| client_fd = socket(AF_INET, SOCK_STREAM, 0); | ||
| assert(client_fd >= 0); | ||
| fcntl(client_fd, F_SETFL, O_NONBLOCK); | ||
| int r = connect(client_fd, (struct sockaddr*)&addr, sizeof(addr)); | ||
| assert(r == 0 || errno == EINPROGRESS); | ||
|
|
||
| #ifdef __EMSCRIPTEN__ | ||
| emscripten_set_main_loop(main_loop, 0, 0); | ||
| #else | ||
| while (peer_fd < 0) { | ||
| main_loop(); | ||
| usleep(1000); | ||
| } | ||
| #endif | ||
| return 0; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.