Skip to content

Commit 6eb581f

Browse files
authored
Fix issues in the libc-wasi poll_oneoff when thread-mgr is enabled (bytecodealliance#1980)
Fix issues in the libc-wasi `poll_oneoff` when thread manager is enabled: - The exception of a thread may be cleared when other thread runs into `proc_exit` and then calls `clear_wasi_proc_exit_exception`, so should not use `wasm_runtime_get_exception` to check whether an exception was thrown, use `wasm_cluster_is_thread_terminated` instead - We divided one time poll_oneoff into many times poll_oneoff to check the exception to avoid long time waiting in previous PR, but if all events returned by one time poll are all waiting events, we need to continue to wait but not return directly. Follow-up on bytecodealliance#1951. Tested with multiple timeout values, with and without interruption and measured the time spent sleeping.
1 parent f4107a9 commit 6eb581f

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
#include "libc_wasi_wrapper.h"
77
#include "bh_platform.h"
88
#include "wasm_export.h"
9+
#include "wasm_runtime_common.h"
10+
11+
#if WASM_ENABLE_THREAD_MGR != 0
12+
#include "../../../thread-mgr/thread_manager.h"
13+
#endif
914

1015
void
1116
wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
@@ -46,19 +51,7 @@ typedef struct iovec_app {
4651
uint32 buf_len;
4752
} iovec_app_t;
4853

49-
typedef struct WASIContext {
50-
struct fd_table *curfds;
51-
struct fd_prestats *prestats;
52-
struct argv_environ_values *argv_environ;
53-
struct addr_pool *addr_pool;
54-
char *ns_lookup_buf;
55-
char **ns_lookup_list;
56-
char *argv_buf;
57-
char **argv_list;
58-
char *env_buf;
59-
char **env_list;
60-
uint32_t exit_code;
61-
} * wasi_ctx_t;
54+
typedef struct WASIContext *wasi_ctx_t;
6255

6356
wasi_ctx_t
6457
wasm_runtime_get_wasi_ctx(wasm_module_inst_t module_inst);
@@ -989,13 +982,12 @@ update_clock_subscription_data(wasi_subscription_t *in, uint32 nsubscriptions,
989982
}
990983

991984
static wasi_errno_t
992-
execute_interruptible_poll_oneoff(wasm_module_inst_t module_inst,
985+
execute_interruptible_poll_oneoff(
993986
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
994-
struct fd_table *curfds,
987+
struct fd_table *curfds,
995988
#endif
996-
const __wasi_subscription_t *in,
997-
__wasi_event_t *out, size_t nsubscriptions,
998-
size_t *nevents)
989+
const __wasi_subscription_t *in, __wasi_event_t *out, size_t nsubscriptions,
990+
size_t *nevents, wasm_exec_env_t exec_env)
999991
{
1000992
if (nsubscriptions == 0) {
1001993
*nevents = 0;
@@ -1004,6 +996,8 @@ execute_interruptible_poll_oneoff(wasm_module_inst_t module_inst,
1004996

1005997
wasi_errno_t err;
1006998
__wasi_timestamp_t elapsed = 0;
999+
bool all_outs_are_type_clock;
1000+
uint32 i;
10071001

10081002
const __wasi_timestamp_t timeout = get_timeout_for_poll_oneoff(
10091003
in, nsubscriptions),
@@ -1021,25 +1015,35 @@ execute_interruptible_poll_oneoff(wasm_module_inst_t module_inst,
10211015
bh_memcpy_s(in_copy, size_to_copy, in, size_to_copy);
10221016

10231017
while (timeout == (__wasi_timestamp_t)-1 || elapsed <= timeout) {
1024-
elapsed += time_quant;
1025-
10261018
/* update timeout for clock subscription events */
10271019
update_clock_subscription_data(in_copy, nsubscriptions,
10281020
min(time_quant, timeout - elapsed));
10291021
err = wasmtime_ssp_poll_oneoff(curfds, in_copy, out, nsubscriptions,
10301022
nevents);
1023+
elapsed += time_quant;
1024+
10311025
if (err) {
10321026
wasm_runtime_free(in_copy);
10331027
return err;
10341028
}
10351029

1036-
if (wasm_runtime_get_exception(module_inst) || *nevents > 0) {
1030+
if (wasm_cluster_is_thread_terminated(exec_env)) {
10371031
wasm_runtime_free(in_copy);
1032+
return EINTR;
1033+
}
1034+
else if (*nevents > 0) {
1035+
all_outs_are_type_clock = true;
1036+
for (i = 0; i < *nevents; i++) {
1037+
if (out[i].type != __WASI_EVENTTYPE_CLOCK) {
1038+
all_outs_are_type_clock = false;
1039+
break;
1040+
}
1041+
}
10381042

1039-
if (*nevents) {
1043+
if (!all_outs_are_type_clock) {
1044+
wasm_runtime_free(in_copy);
10401045
return __WASI_ESUCCESS;
10411046
}
1042-
return EINTR;
10431047
}
10441048
}
10451049

@@ -1069,8 +1073,8 @@ wasi_poll_oneoff(wasm_exec_env_t exec_env, const wasi_subscription_t *in,
10691073
#if WASM_ENABLE_THREAD_MGR == 0
10701074
err = wasmtime_ssp_poll_oneoff(curfds, in, out, nsubscriptions, &nevents);
10711075
#else
1072-
err = execute_interruptible_poll_oneoff(module_inst, curfds, in, out,
1073-
nsubscriptions, &nevents);
1076+
err = execute_interruptible_poll_oneoff(curfds, in, out, nsubscriptions,
1077+
&nevents, exec_env);
10741078
#endif
10751079
if (err)
10761080
return err;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,6 +2613,8 @@ wasmtime_ssp_poll_oneoff(
26132613
}
26142614
#endif
26152615
*nevents = 1;
2616+
if (out[0].error != 0)
2617+
return convert_errno(out[0].error);
26162618
return 0;
26172619
}
26182620

core/iwasm/libraries/thread-mgr/thread_manager.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,3 +1198,9 @@ wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
11981198
os_mutex_unlock(&cluster->lock);
11991199
}
12001200
}
1201+
1202+
bool
1203+
wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env)
1204+
{
1205+
return (exec_env->suspend_flags.flags & 0x01) ? true : false;
1206+
}

core/iwasm/libraries/thread-mgr/thread_manager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ void
146146
wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
147147
void *custom_data);
148148

149+
bool
150+
wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env);
151+
149152
#if WASM_ENABLE_DEBUG_INTERP != 0
150153
#define WAMR_SIG_TRAP (5)
151154
#define WAMR_SIG_STOP (19)

0 commit comments

Comments
 (0)