Skip to content

Commit

Permalink
vm: make runtime shutdown more resilient
Browse files Browse the repository at this point in the history
Close all loop handles after the JS heap has been destroyed.

Only check the loop has been properly closed on debug builds.
  • Loading branch information
saghul committed Sep 25, 2023
1 parent 8460eae commit 3b8b2ef
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ static void uv__stop(uv_async_t *handle) {
uv_stop(&qrt->loop);
}

static void uv__walk(uv_handle_t* handle, void* arg) {
if (!uv_is_closing(handle))
uv_close(handle, NULL);
}

void TJS_DefaultOptions(TJSRunOptions *options) {
static TJSRunOptions default_options = { .mem_limit = -1, .stack_size = TJS__DEFAULT_STACK_SIZE };

Expand Down Expand Up @@ -254,6 +259,8 @@ void TJS_FreeRuntime(TJSRuntime *qrt) {
/* Destroy WASM runtime. */
m3_FreeEnvironment(qrt->wasm_ctx.env);

uv_walk(&qrt->loop, uv__walk, NULL);

/* Cleanup loop. All handles should be closed. */
int closed = 0;
for (int i = 0; i < 5; i++) {
Expand All @@ -266,8 +273,8 @@ void TJS_FreeRuntime(TJSRuntime *qrt) {
#ifdef DEBUG
if (!closed)
uv_print_all_handles(&qrt->loop, stderr);
#endif
CHECK_EQ(closed, 1);
#endif

free(qrt);
}
Expand Down

0 comments on commit 3b8b2ef

Please sign in to comment.