Skip to content

Commit

Permalink
wasi: apply wasm_runtime_begin_blocking_op to poll as well
Browse files Browse the repository at this point in the history
While we used a different approach for poll_oneoff [1],
the implementation works only when the poll list includes
an absolute clock event. That is, if we have a thread which is
polling on descriptors without a timeout, we fail to terminate
the thread.

This commit fixes it by applying wasm_runtime_begin_blocking_op
to poll as well.

[1] #1951
  • Loading branch information
yamt committed Jan 24, 2024
1 parent 61fe78c commit 40c94f4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "ssp_config.h"
#include "blocking_op.h"
#include "libc_errno.h"

__wasi_errno_t
blocking_op_close(wasm_exec_env_t exec_env, os_file_handle handle,
Expand Down Expand Up @@ -170,3 +171,21 @@ blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle,
wasm_runtime_end_blocking_op(exec_env);
return error;
}

/* REVISIT: apply the os_file_handle style abstraction for pollfd? */
__wasi_errno_t
blocking_op_poll(wasm_exec_env_t exec_env, struct pollfd *pfds, nfds_t nfds,
int timeout_ms, int *retp)
{
int ret;
if (!wasm_runtime_begin_blocking_op(exec_env)) {
return __WASI_EINTR;
}
ret = poll(pfds, nfds, timeout_ms);
wasm_runtime_end_blocking_op(exec_env);
if (ret == -1) {
return convert_errno(errno);
}
*retp = ret;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ __wasi_errno_t
blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle,
const char *path, __wasi_oflags_t oflags,
__wasi_fdflags_t fd_flags, __wasi_lookupflags_t lookup_flags,
wasi_libc_file_access_mode access_mode, os_file_handle *out);
wasi_libc_file_access_mode access_mode, os_file_handle *out);

__wasi_errno_t
blocking_op_poll(wasm_exec_env_t exec_env, struct pollfd *pfds, nfds_t nfds,
int timeout, int *retp);
Original file line number Diff line number Diff line change
Expand Up @@ -2230,11 +2230,10 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds,
timeout = -1;
}

int ret = poll(pfds, nsubscriptions, timeout);

__wasi_errno_t error = 0;
if (ret == -1) {
error = convert_errno(errno);
int ret;
int error = blocking_op_poll(exec_env, pfds, nsubscriptions, timeout, &ret);
if (error != 0) {
/* got an error */
}
else if (ret == 0 && *nevents == 0 && clock_subscription != NULL) {
// No events triggered. Trigger the clock event.
Expand Down

0 comments on commit 40c94f4

Please sign in to comment.