Stream: wasi

Topic: `wasi:io/poll` level- vs. edge-triggering


view this post on Zulip Joel Dice (Dec 01 2023 at 15:11):

So it turns out the wild success I reported earlier porting mio and tokio to WASI Preview 2 wasn't so wildly successful after all. Simple examples work, but things break when they get more complicated. After an evening of debugging, I learned a few things:

I'm going to keep cranking on this, but I could use some help answering this question: Is it correct to say that both Preview 1's poll_oneoff and Preview 2's wasi:io/poll#poll are level-triggered, i.e. if you ask either of them whether a socket is ready for writing, it will keep saying "yes" no matter how many times you ask it unless you actually do enough writing to trigger backpressure?

view this post on Zulip Alex Crichton (Dec 01 2023 at 15:13):

Is it correct to say that both Preview 1's poll_oneoff and Preview 2's wasi:io/poll#poll are level-triggered

Yes

view this post on Zulip Joel Dice (Dec 01 2023 at 15:14):

Ok, so mio's existing WASI support is buggy and I should just ignore it, sounds like.

view this post on Zulip Alex Crichton (Dec 01 2023 at 15:15):

Trying to think more on this, but yeah if mio expects edge triggered and preview1 "just" calls poll then sounds like you'll need to ignore it

view this post on Zulip Alex Crichton (Dec 01 2023 at 15:15):

I'm not sure how widely used the preview1 integration was to flesh out bugs like this

view this post on Zulip Alex Crichton (Dec 01 2023 at 15:16):

I'd have to dig more into exactly what mio wants here to help more though, I would not have expected that level-triggered would break things

view this post on Zulip Joel Dice (Dec 01 2023 at 15:35):

Alex, Lann, and I did a quick call to dig into this. It looks like mio's support for both poll_oneoff and POSIX poll are broken in subtle ways, which is what was causing my confusion. I'm going to try to fix the POSIX bits and totally replace the WASI bits.

view this post on Zulip Joel Dice (Dec 01 2023 at 16:12):

Correction: looks like mio's POSIX poll support is not broken after all. I just didn't understand how it worked in the context of how it's used. Looks like it will be a good pattern to follow for WASI Preview 2 support.

view this post on Zulip Alex Crichton (Dec 01 2023 at 16:16):

Oh? How was the interest between read/write split given what we were looking at?

view this post on Zulip Joel Dice (Dec 01 2023 at 16:22):

initially, the mio code registers both read and write interest. Then, when one of those triggers (say write readiness, for example), the selector internally deregisters write interest while keeping read interest enabled. Then it unblocks tokio to do its write, if applicable, which will cause the IoSourceState interceptor to re-register both interests, which will cause the next poll to wake immediately, at which point the selector will deregister write interest again, which gives tokio a chance to write again, but this time it doesn't need to, so we go back and poll again, this time with only read interest, and we stay there blocked until there's something to read.

view this post on Zulip Joel Dice (Dec 01 2023 at 16:23):

so technically there's still a very minor inefficiency where we poll one extra time redundantly, but only once per useful I/O operation; there's no unbounded busy waiting

view this post on Zulip Alex Crichton (Dec 01 2023 at 16:33):

ah ok I can sort of see that

view this post on Zulip Joel Dice (Dec 01 2023 at 21:20):

I've updated my mio fork with a rewrite of the WASI selector per the above discussion, and things are working nicely now. I've also added a somewhat more complicated test using tokio-postgres to the test suite.


Last updated: Dec 23 2024 at 12:05 UTC