Skip to content

Commit ed9e99a

Browse files
authored
wasip3: Perform a thread yield in poll with no timeout (#783)
This commit updates the `poll` implementation on wasip3 to insert a call to the canonical `thread.yield` function in the case that there's no timeout and nothing is ready yet. This explicitly provides the host an opportunity to deliver events and resolve any potential otherwise-deadlock if the guest is spinning waiting for events to happen with a timeout of 0. This is related to bytecodealliance/wasmtime#13040 and is a partial guest-side solution for what's outlined there.
1 parent 5885dd9 commit ed9e99a

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

  • libc-bottom-half/cloudlibc/src/libc/poll

libc-bottom-half/cloudlibc/src/libc/poll/poll.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,14 @@ static int poll_impl(struct pollfd *fds, size_t nfds, int timeout) {
434434
wasip3_waitable_set_wait(state.set, &event);
435435
} else {
436436
wasip3_waitable_set_poll(state.set, &event);
437+
438+
// If nothing happened after this `poll`, then yield to the runtime to allow
439+
// harvesting any events and avoid starvation if we're being called in a
440+
// loop with a timeout of 0.
441+
if (event.event == WASIP3_EVENT_NONE) {
442+
wasip3_thread_yield();
443+
wasip3_waitable_set_poll(state.set, &event);
444+
}
437445
}
438446
while (event.event != WASIP3_EVENT_NONE) {
439447
// If this event is for the timeout subtask then that's handled directly here

0 commit comments

Comments
 (0)