Skip to content

Avoid JSPI suspend on poll()/select() with zero timeout#27100

Open
guybedford wants to merge 3 commits into
emscripten-core:mainfrom
guybedford:ready-poll
Open

Avoid JSPI suspend on poll()/select() with zero timeout#27100
guybedford wants to merge 3 commits into
emscripten-core:mainfrom
guybedford:ready-poll

Conversation

@guybedford

@guybedford guybedford commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

When using JSPI, it can be beneficial to have exported functions using poll that do not use WebAssembly.Promising wrappers. In order to achieve this, one would want access to the non-JSPI poll variant that does not use WebAssembly.Suspending.

This updates the implementation of poll to route to the non-suspending variant whenever a zero timeout is set. We could possibly guard this on JSPI only if we wish to as well.

This was useful for the Tokio prototype work to support both types of stacks at the same time.

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could just make poll with a timeout of zero never suspending?

If we did that would it address your concerns?

I think what we could need to do is have two internal functions __poll_blocking and __poll_nonblocking. Then the native __syscall_poll could call one or the other depending on the timeout value?

This would avoid declaring any new functions in the public API.

@sbc100

sbc100 commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

We do have a precedent for writing "sometimes-suspending" functions. See #26947.

@guybedford guybedford force-pushed the ready-poll branch 2 times, most recently from 5a3e1dd to 33e0232 Compare June 11, 2026 20:30
@guybedford guybedford changed the title Add emscripten_ready_poll for non-suspending readiness probes Zero-timeout polls as always non-suspending Jun 11, 2026
Comment thread test/test_poll_nonblocking.c
Comment thread test/test_other.py Outdated
Comment thread tools/emscripten.py Outdated
Comment thread src/lib/libsyscall.js

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Comment thread src/lib/libsyscall.js Outdated
Comment thread src/lib/libsyscall.js Outdated
Comment thread system/lib/libc/musl/src/select/poll.c
Comment thread test/test_other.py Outdated
Comment thread tools/emscripten.py Outdated

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm % final nits

Its a little bit hard for me to the tell the that behaviour of the normal poll is unchanged, but I we have test coverage for I think.

Comment thread test/test_other.py Outdated
@sbc100

sbc100 commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

You will probably need to rebase the codesize tests (that only one I expect to see change is that one that include all JS functions).

@sbc100

sbc100 commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

For the PR title, how about "Avoid JSPI suspend on poll()/select() with zero timeout" ?

Comment thread src/lib/libsyscall.js Outdated
Comment thread src/lib/libsyscall.js
@guybedford guybedford force-pushed the ready-poll branch 3 times, most recently from 4e14cc7 to f8232b0 Compare June 11, 2026 22:51
@guybedford guybedford changed the title Zero-timeout polls as always non-suspending Avoid JSPI suspend on poll()/select() with zero timeout Jun 11, 2026
@sbc100 sbc100 enabled auto-merge (squash) June 11, 2026 23:23
@sbc100

sbc100 commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

I enabled audo-merge so this should land once tests pass.

Comment thread src/lib/libsyscall.js Outdated
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.

2 participants