Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/lib/libasync.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,17 @@ addToLibrary({
//
// This is particularly useful for native JS `async` functions where the
// returned value will "just work" and be passed back to C++.
handleAsync: (startAsync) => Asyncify.handleSleep(async (wakeUp) => {
// TODO: add error handling as a second param when handleSleep implements it.
wakeUp(await startAsync());
}),
handleAsync: (startAsync) => {
#if PTHREADS
// When called from a proxied function (PROXY_SYNC_ASYNC), the proxy
// mechanism handles the async return. Skip the Asyncify unwind.
if (PThread.currentProxiedOperationCallerThread) return startAsync();
Copy link
Collaborator

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 handleAsync in the case of PROXY_SYNC_ASYNC?

Copy link
Contributor Author

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?

#endif
return Asyncify.handleSleep(async (wakeUp) => {
// TODO: add error handling as a second param when handleSleep implements it.
wakeUp(await startAsync());
});
},

#elif ASYNCIFY == 2
//
Expand Down
60 changes: 60 additions & 0 deletions test/core/test_poll_blocking_asyncify_pthread.c
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");
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 test_poll_blocking_asyncify.c but with -sPROXY_TO_PTHREAD?

If there are things we are not covering in the existing test maybe better to add to that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

}
6 changes: 6 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9645,6 +9645,12 @@ def test_poll_blocking_asyncify(self):
self.skipTest('test requires setTimeout which is not supported under v8')
self.do_runf('core/test_poll_blocking_asyncify.c', 'done\n')

@with_asyncify_and_jspi
@requires_pthreads
def test_poll_blocking_asyncify_pthread(self):
self.do_runf('core/test_poll_blocking_asyncify_pthread.c', 'done\n',
cflags=['-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'])

@parameterized({
'': ([],),
'pthread': (['-pthread'],),
Expand Down
Loading