Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poll() with timeout fails with "timer subscriptions only support monotonic timer" #318

Closed
tiran opened this issue Aug 24, 2022 · 1 comment

Comments

@tiran
Copy link

tiran commented Aug 24, 2022

poll() third's argument is timeout in milliseconds. Any attempt to set a timeout other than -1 (wait forever) or very large value fails. I have discussed the matter with @sunfishcode on Discord. He said:

That appears to be a bug in wasi-libc. It's translating the poll timeout into a request using the REALTIME (wall-clock) clock. It should use the MONOTONIC clock.

reproducer

#define _POSIX_C_SOURCE 200809L

#include <stdio.h>
#include <unistd.h>
#include <poll.h>

int main(void) {
    struct pollfd pfds[1];
    pfds[0].fd = 0; // stdin
    pfds[0].events = POLLIN;
    pfds[0].revents = 0;

    int timeout_ms = -1;
    timeout_ms = 1000;

    int ready = poll(pfds, 1, timeout_ms);
    if (ready == -1) {
        perror("poll failed");
        return 2;
    }
    printf("poll %i\n", ready);
    return 0;
}

/opt/wasi-sdk/bin/clang poll.c -o poll.wasm && RUST_LOG=wasi_common wasmtime run poll.wasm

 TRACE wasi_common::snapshots::preview_1::wasi_snapshot_preview1 > wiggle abi; module="wasi_snapshot_preview1" function="poll_oneoff"
 TRACE wasi_common::snapshots::preview_1::wasi_snapshot_preview1 > in_=*guest 0x111e0 out=*guest 0x111a0 nsubscriptions=2
 TRACE wasi_common::snapshots::preview_1::wasi_snapshot_preview1 > result=Err(timer subscriptions only support monotonic timer

Caused by:
    Inval: Invalid argument)
 DEBUG wasi_common::snapshots::preview_1                         > Error: timer subscriptions only support monotonic timer

Caused by:
    Inval: Invalid argument
@sunfishcode
Copy link
Member

Thanks for the report! Looking into it more, POSIX does specify that things like nanosleep use the REALTIME timer, however Linux uses the MONOTONIC timer. POSIX also says that these functions aren't affected by clock_settime, so in practice there's very little difference. I think it makes sense to change Wasmtime here to accept either form of timer, and I've now implemented this in bytecodealliance/wasmtime#4777.

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

No branches or pull requests

2 participants