-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Fix Asyncify.handleAsync conflict with PROXY_SYNC_ASYNC #26513
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #define _GNU_SOURCE | ||
| #include <assert.h> | ||
| #include <poll.h> | ||
| #include <pthread.h> | ||
| #include <stdio.h> | ||
| #include <time.h> | ||
| #include <unistd.h> | ||
|
|
||
| static int fds[2]; | ||
|
|
||
| static void *writer(void *arg) { | ||
| write(fds[1], "x", 1); | ||
| return NULL; | ||
| } | ||
|
|
||
| int main(void) { | ||
| pthread_t t; | ||
| char buf; | ||
| struct pollfd pfd = {.events = POLLIN}; | ||
|
|
||
| pipe(fds); | ||
| pfd.fd = fds[0]; | ||
|
|
||
| // poll should timeout on an empty pipe | ||
| assert(poll(&pfd, 1, 100) == 0); | ||
|
|
||
| // poll should return immediately when data is already available | ||
| write(fds[1], "a", 1); | ||
| assert(poll(&pfd, 1, 1000) == 1); | ||
| assert(pfd.revents & POLLIN); | ||
| assert(read(fds[0], &buf, 1) == 1 && buf == 'a'); | ||
|
|
||
| // poll should wake up from a cross-thread write | ||
| pfd.revents = 0; | ||
| pthread_create(&t, NULL, writer, NULL); | ||
| assert(poll(&pfd, 1, 5000) == 1); | ||
| assert(pfd.revents & POLLIN); | ||
| assert(read(fds[0], &buf, 1) == 1 && buf == 'x'); | ||
| pthread_join(t, NULL); | ||
|
|
||
| // ppoll should also timeout on an empty pipe | ||
| struct timespec ts = {0, 200 * 1000000L}; | ||
| struct timespec begin, end; | ||
| pfd.revents = 0; | ||
| clock_gettime(CLOCK_MONOTONIC, &begin); | ||
| assert(ppoll(&pfd, 1, &ts, NULL) == 0); | ||
| clock_gettime(CLOCK_MONOTONIC, &end); | ||
| long elapsed_ms = (end.tv_sec - begin.tv_sec) * 1000 + | ||
| (end.tv_nsec - begin.tv_nsec) / 1000000; | ||
| assert(elapsed_ms >= 195); | ||
|
|
||
| printf("done\n"); | ||
|
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. Do we need a new test here or can we just re-used the existing test If there are things we are not covering in the existing test maybe better to add to that?
Contributor
Author
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. I tried reusing test_poll_blocking_asyncify.c with -sPROXY_TO_PTHREAD but it hangs because it uses emscripten_set_timeout to schedule a write, which doesn't work when main runs on a worker thread. The pthread variant needs actual cross thread writes to exercise the proxied poll path, so I think a separate test file is needed here. Or do you have a better idea? |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this might be slightly the wrong place for this change.
I wonder if calling code should instead just not be using
handleAsyncin the case ofPROXY_SYNC_ASYNC?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing is handleAsync is auto-generated by jsifier.mjs when __async: 'auto' is set on a library function. The calling code (e.g. __syscall_poll) doesn't invoke handleAsync directly, it gets wrapped automatically by the compiler. So to avoid using handleAsync in the PROXY_SYNC_ASYNC case, I think we'd need to change jsifier.mjs to not generate that wrapper when __proxy: 'sync' is also present, which felt like a bigger change. But maybe that's actually cleaner? What do you think?