wasm-micro-runtime/core/iwasm/libraries
YAMAMOTO Takashi 70c39bae77
wasi-nn: fix context lifetime issues (#4396)
* wasi-nn: fix context lifetime issues

use the module instance context api instead of trying to roll
our own with a hashmap. this fixes context lifetime problems mentioned in
https://github.com/bytecodealliance/wasm-micro-runtime/issues/4313.

namely,

* wasi-nn resources will be freed earlier now. before this change,
  they used to be kept until the runtime shutdown. (wasm_runtime_destroy)
  after this change, they will be freed together with the associated
  instances.

* wasm_module_inst_t pointer uniqueness assumption (which is wrong
  after wasm_runtime_deinstantiate) was lifted.

as a side effect, this change also makes a context shared among threads
within a cluster. note that this is a user-visible api/abi breaking change.
before this change, wasi-nn "handles" like wasi_ephemeral_nn_graph were
thread-local. after this change, they are shared among threads within
a cluster, similarly to wasi file descriptors. spec-wise, either behavior
should be ok simply because wasi officially doesn't have threads yet.
althogh i feel the latter semantics is more intuitive, if your application
depends on the thread-local behavior, this change breaks your application.

tested with wamr-wasi-extensions/samples/nn-cli, modified to
call each wasi-nn operations on different threads. (if you are
interested, you can find the modification at
https://github.com/yamt/wasm-micro-runtime/tree/yamt-nn-wip-20250619.)

cf.
https://github.com/bytecodealliance/wasm-micro-runtime/issues/4313
https://github.com/bytecodealliance/wasm-micro-runtime/issues/2430

* runtime_lib.cmake: enable WAMR_BUILD_MODULE_INST_CONTEXT for wasi-nn

as we do for wasi (WAMR_BUILD_LIBC_WASI)
2025-06-24 20:37:56 +08:00
..
debug-engine send an empty/error reply from server (#4362) 2025-06-17 13:00:58 +08:00
lib-pthread Add Windows wamrc and iwasm build in release CI (#3857) 2024-10-17 10:01:56 +08:00
lib-rats Keep fix the CMake compatibility issue (#4180) 2025-04-15 12:51:19 +08:00
lib-socket wasi_socket_ext.c: avoid tls to make this library-friendly (#4338) 2025-06-11 07:46:35 +08:00
lib-wasi-threads fix: correct typos and improve comments across multiple files by codespell (#4116) 2025-03-07 08:21:54 +08:00
libc-builtin libc-builtin: Fix function prototype for wasm_runtime_module_realloc (#3702) 2024-08-13 17:44:58 +08:00
libc-emcc libc-emcc: Use alternate method to check getrandom support (#3848) 2024-10-10 10:57:47 +08:00
libc-uvwasi Bump uvwasi to latest commit #392e1f1 (#4312) 2025-06-03 13:21:17 +08:00
libc-wasi Improve readlinkat_dup() to handle symlink size correctly (#4229) 2025-05-09 14:01:29 +08:00
shared-heap Implement shared heap for AOT (#3815) 2024-09-29 12:50:59 +08:00
simde Keep fix the CMake compatibility issue (#4180) 2025-04-15 12:51:19 +08:00
thread-mgr Implement shared heap for AOT (#3815) 2024-09-29 12:50:59 +08:00
wasi-nn wasi-nn: fix context lifetime issues (#4396) 2025-06-24 20:37:56 +08:00