Skip to content

wasip2: Fix zero-wait pollables#13511

Open
alexcrichton wants to merge 3 commits into
bytecodealliance:mainfrom
alexcrichton:fix-p2-zero-wait
Open

wasip2: Fix zero-wait pollables#13511
alexcrichton wants to merge 3 commits into
bytecodealliance:mainfrom
alexcrichton:fix-p2-zero-wait

Conversation

@alexcrichton
Copy link
Copy Markdown
Member

This commit fixes an accidental bug introduced in #13085 where repeatedly calling ready() on a zero-wait pollable in WASIp2 would never resolve. In #13085 zero-length waits were updated to unconditionally yield to tokio to improve fairness, but this didn't take into account where the yield was repeatedly cancelled and never completed. The fix in this commit is to attempt the yield once and then never attempt it again. If the original yield is cancelled the next check on the pollable will go through. This is sort of a cancellation-safety fix where the previous implementation wasn't necessarily cancellation safe in the sense that repeatedly checking-and-cancelling never let anything progress, which is counterintuitive.

Closes #13507

This commit fixes an accidental bug introduced in bytecodealliance#13085 where
repeatedly calling `ready()` on a zero-wait pollable in WASIp2 would
never resolve. In bytecodealliance#13085 zero-length waits were updated to
unconditionally yield to tokio to improve fairness, but this didn't take
into account where the yield was repeatedly cancelled and never
completed. The fix in this commit is to attempt the yield once and then
never attempt it again. If the original yield is cancelled the next
check on the pollable will go through. This is sort of a
cancellation-safety fix where the previous implementation wasn't
necessarily cancellation safe in the sense that repeatedly
checking-and-cancelling never let anything progress, which is
counterintuitive.

Closes bytecodealliance#13507
@alexcrichton alexcrichton requested review from a team as code owners May 29, 2026 20:40
@alexcrichton alexcrichton requested review from dicej and removed request for a team May 29, 2026 20:40
@github-actions github-actions Bot added the wasi Issues pertaining to WASI label May 29, 2026
@@ -142,6 +143,7 @@ impl Pollable for Deadline {
// are hypothetically other ways to generate a pollable that's
// always immediately ready, which this hack doesn't cover, but
// we consider this sufficient for now.
Copy link
Copy Markdown

@ignatz ignatz Jun 1, 2026

Choose a reason for hiding this comment

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

I'm primarily commenting to say: "Thank you" 🙏 and secondarily in a selfish attempt to better understand what's going on, i.e. from a place of ignorance.

I understood from your text that Pollable::ready is called multiple time and I would have absolutely written the code in the same way it was, i.e. it's surprising to me that the status quote cannot make progress. Wouldn't all futures returned by yield_now get resolved eventually even if some of them get dropped?

Since I don't even have the context why Pollable::ready gets called multiple times, I'm wondering if this can lead to some re-ordering of tasks? First call will yield, second won't which means a second continuation may get scheduled earlier than the first. This is probably not an issue since tokio doesn't give any ordering guarantees but may still be surprising 🤷.

Maybe there's some way in the downstream consuming code to address this issue rather than the conditional yielding? Then again, I've no clue and you should do what makes the most sense.

From a mere user's perspective, I would expect repeated yields from WASM to lead to repeated yields to the tokio runtime ... as long as that's the case 🤷‍♀️

EDIT: specifically await Timer(0) is a common approach in many languages (JS, Dart, ...) to yield to the runtime/event-loop.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The implementation of pollable.ready(), which is what wstd is calling, creates this Pollable::ready future defined here, polls it, then drops it. With the previous implementation it would never make progress since the yield was attempted to happen every time. It looks like wstd never actually blocked on the pollable, it only kept asking if it was ready. This patched implementation still preserves yielding behavior, but the yield is only attempted once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasi Issues pertaining to WASI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Timer regression v44 => v45 for zero-delay timers?

2 participants