Skip to content

Commit 9afbeab

Browse files
authored
wasi: Apply wasm_runtime_begin_blocking_op to poll as well (#3080)
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
1 parent bf9fb2e commit 9afbeab

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "ssp_config.h"
99
#include "blocking_op.h"
10+
#include "libc_errno.h"
1011

1112
__wasi_errno_t
1213
blocking_op_close(wasm_exec_env_t exec_env, os_file_handle handle,
@@ -170,3 +171,23 @@ blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle,
170171
wasm_runtime_end_blocking_op(exec_env);
171172
return error;
172173
}
174+
175+
#ifndef BH_PLATFORM_WINDOWS
176+
/* REVISIT: apply the os_file_handle style abstraction for pollfd? */
177+
__wasi_errno_t
178+
blocking_op_poll(wasm_exec_env_t exec_env, struct pollfd *pfds, nfds_t nfds,
179+
int timeout_ms, int *retp)
180+
{
181+
int ret;
182+
if (!wasm_runtime_begin_blocking_op(exec_env)) {
183+
return __WASI_EINTR;
184+
}
185+
ret = poll(pfds, nfds, timeout_ms);
186+
wasm_runtime_end_blocking_op(exec_env);
187+
if (ret == -1) {
188+
return convert_errno(errno);
189+
}
190+
*retp = ret;
191+
return 0;
192+
}
193+
#endif

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,10 @@ __wasi_errno_t
5050
blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle,
5151
const char *path, __wasi_oflags_t oflags,
5252
__wasi_fdflags_t fd_flags, __wasi_lookupflags_t lookup_flags,
53-
wasi_libc_file_access_mode access_mode, os_file_handle *out);
53+
wasi_libc_file_access_mode access_mode, os_file_handle *out);
54+
55+
#ifndef BH_PLATFORM_WINDOWS
56+
__wasi_errno_t
57+
blocking_op_poll(wasm_exec_env_t exec_env, struct pollfd *pfds, nfds_t nfds,
58+
int timeout, int *retp);
59+
#endif

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,11 +2230,10 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds,
22302230
timeout = -1;
22312231
}
22322232

2233-
int ret = poll(pfds, nsubscriptions, timeout);
2234-
2235-
__wasi_errno_t error = 0;
2236-
if (ret == -1) {
2237-
error = convert_errno(errno);
2233+
int ret;
2234+
int error = blocking_op_poll(exec_env, pfds, nsubscriptions, timeout, &ret);
2235+
if (error != 0) {
2236+
/* got an error */
22382237
}
22392238
else if (ret == 0 && *nevents == 0 && clock_subscription != NULL) {
22402239
// No events triggered. Trigger the clock event.

0 commit comments

Comments
 (0)