diff --git a/crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs b/crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs index 1af4ed0575..ddfbce0df9 100644 --- a/crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs +++ b/crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs @@ -65,11 +65,17 @@ unsafe fn test_stdin_read() { r#type: wasi::EVENTTYPE_CLOCK, u: wasi::SubscriptionU { clock }, }, + // Make sure that timeout is returned only once even if there are multiple read events wasi::Subscription { userdata: 1, r#type: wasi::EVENTTYPE_FD_READ, u: wasi::SubscriptionU { fd_readwrite }, }, + wasi::Subscription { + userdata: 42, + r#type: wasi::EVENTTYPE_FD_READ, + u: wasi::SubscriptionU { fd_readwrite }, + }, ]; let out = poll_oneoff_impl(&r#in, 1); let event = &out[0]; diff --git a/crates/wasi-common/src/sys/windows/hostcalls_impl/misc.rs b/crates/wasi-common/src/sys/windows/hostcalls_impl/misc.rs index c8c8f34ed6..e23bc6507c 100644 --- a/crates/wasi-common/src/sys/windows/hostcalls_impl/misc.rs +++ b/crates/wasi-common/src/sys/windows/hostcalls_impl/misc.rs @@ -95,7 +95,7 @@ fn make_rw_event(event: &FdEventData, nbytes: Result) -> wasi::__wasi_event } } -fn make_timeout_event(event: &FdEventData, timeout: &ClockEventData) -> wasi::__wasi_event_t { +fn make_timeout_event(timeout: &ClockEventData) -> wasi::__wasi_event_t { wasi::__wasi_event_t { userdata: timeout.userdata, r#type: wasi::__WASI_EVENTTYPE_CLOCK, @@ -125,7 +125,12 @@ pub(crate) fn poll_oneoff( // With no events to listen, poll_oneoff just becomes a sleep. if fd_events.is_empty() { match timeout_duration { - Some(t) => thread::sleep(t), + Some(t) => { + thread::sleep(t); + let timeout_event = timeout.expect("timeout should be Some"); + let new_event = make_timeout_event(&timeout_event); + events.push(new_event); + } // `poll` invoked with nfds = 0, timeout = -1 appears to be an infinite sleep // Even though the thread is not guanteed to remain parked forever, `poll(2)` // mentions that spurious readiness notifications may occur, so it's probably fine @@ -236,10 +241,8 @@ pub(crate) fn poll_oneoff( match timeout_occurred { Some(timeout_info) => { - for event in stdin_events { - let new_event = make_timeout_event(&event, &timeout_info); - events.push(new_event); - } + let new_event = make_timeout_event(&timeout_info); + events.push(new_event); } None => { // stdin became ready for reading