Skip to content

poll_oneoff: do not sleep when there is no clock subscription - #2540

Merged
evacchi merged 2 commits into
wazero:mainfrom
NotAFlightRisk:polloneoff-sentinel-timeout-hang
Sep 8, 2026
Merged

evacchi merged 2 commits into
wazero:mainfrom
NotAFlightRisk:polloneoff-sentinel-timeout-hang

Conversation

@NotAFlightRisk

Copy link
Copy Markdown
Contributor

wazero run never comes back from a guest that calls poll(fds, nfds, -1) on a writable fd. The events do get written back and nevents is right. Then it parks in Nanosleep for 9223372036854775807ns, which is about 292 years.

timeout starts at max Duration so the loop can take the minimum, and only a clock subscription ever lowers it. wasi-libc adds that subscription inside if (timeout) (ppoll.c), so a poll with an infinite timeout sends a list with no clock event in it, and the sentinel makes it all the way to the sleep.

The comment above that sleep already says the timeout is nonzero if there are clock subscriptions, so this is mostly just making that true.

The test passes its own Nanosleep in beacuse the default one is a no-op. Nothing in the suite ever saw the argument.


Somthing adjacent I've left alone: with a clock subscription and an fd_write subscription together, the fd_write event gets acked straight away but the whole clock timeout still gets slept. Could well be deliberate, and I didnt want to change it in here.

The timeout is initialized to the max Duration so the loop can take the
minimum across clock subscriptions. With no clock subscription that
sentinel survives, and the early return path sleeps on it, so a guest
calling poll(fds, nfds, -1) gets its events written back and then the
host sleeps for 9223372036854775807ns. wasi-libc only emits a clock
subscription when the timeout is non-negative, and `wazero run`
configures a real Nanosleep.

Only observe the timeout when a clock subscription set it.

Signed-off-by: Iain <[email protected]>
@NotAFlightRisk
NotAFlightRisk requested a review from a team as a code owner September 6, 2026 00:14

@evacchi evacchi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you might simplify to something like

const noTimeout = time.Duration(1<<63 - 1)
...
timeout := noTimeout
...
if timeout != noTimeout {
    sysCtx.Nanosleep(int64(timeout))
}

@NotAFlightRisk

Copy link
Copy Markdown
Contributor Author

Cheers @evacchi, done. I'd reached for a flag beacuse I was tracking "was there a clock sub" rather than "is the timeout still the sentinel".

I kept the timeout > 0 next to it though - processClockEvent can return a 0ns relative timeout, and the old code skipped the sleep in that case, so dropping it would start calling Nanosleep(0) where it didn't before.

One edge worth naming: a clock sub asking for exactly 1<<63-1 ns now looks identical to no clock sub, so we'd skip its sleep. Thats 292 years, so I doubt it's worth a second variable, but it is a real difference from the bool.

@evacchi
evacchi merged commit 451613c into wazero:main Sep 8, 2026
65 of 66 checks passed
samyfodil added a commit to samyfodil/wazy that referenced this pull request Oct 18, 2026
wasi-libc's poll(fds, n, -1) emits no clock subscription, so the timeout
kept its max-duration sentinel and poll_oneoff slept on it forever once
every event was already written. Only sleep when a clock lowered it.

Port of wazero/wazero#2540. Also adds wazero#2507's fd_renumber
onto stdio cases, which wazy already allows.
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