Skip to content

Commit

Permalink
process: fix Process object being closed early in chained calls
Browse files Browse the repository at this point in the history
Keep the object alive until the process exit callback has been closed,
which is guaranteed.

Fixes: #471
  • Loading branch information
saghul committed Apr 2, 2024
1 parent 201d7d9 commit 1bb7db5
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/process.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* QuickJS libuv bindings
* txiki.js
*
* Copyright (c) 2019-present Saúl Ibarra Corretgé <[email protected]>
*
Expand Down Expand Up @@ -34,6 +34,7 @@ static JSClassID tjs_process_class_id;
typedef struct {
JSContext *ctx;
JSRuntime *rt;
JSValue obj;
bool closed;
bool finalized;
uv_process_t process;
Expand Down Expand Up @@ -120,6 +121,7 @@ static JSValue tjs_process_wait(JSContext *ctx, JSValueConst this_val, int argc,
TJSProcess *p = tjs_process_get(ctx, this_val);
if (!p)
return JS_EXCEPTION;
CHECK(!p->closed);

if (p->status.exited) {
JSValue obj = JS_NewObjectProto(ctx, JS_NULL);
Expand All @@ -128,8 +130,6 @@ static JSValue tjs_process_wait(JSContext *ctx, JSValueConst this_val, int argc,
p->status.term_signal == 0 ? JS_NULL : JS_NewString(ctx, tjs_getsig(p->status.term_signal));
JS_DefinePropertyValueStr(ctx, obj, "term_signal", term_signal, JS_PROP_C_W_E);
return TJS_NewResolvedPromise(ctx, 1, &obj);
} else if (p->closed) {
return JS_UNDEFINED;
} else {
return TJS_InitPromise(ctx, &p->status.result);
}
Expand All @@ -152,13 +152,13 @@ static JSValue tjs_process_stdio_get(JSContext *ctx, JSValueConst this_val, int
static void uv__exit_cb(uv_process_t *handle, int64_t exit_status, int term_signal) {
TJSProcess *p = handle->data;
CHECK_NOT_NULL(p);
JSContext *ctx = p->ctx;

p->status.exited = true;
p->status.exit_status = exit_status;
p->status.term_signal = term_signal;

if (!JS_IsUndefined(p->status.result.p)) {
JSContext *ctx = p->ctx;
JSValue arg = JS_NewObjectProto(ctx, JS_NULL);
JS_DefinePropertyValueStr(ctx, arg, "exit_status", JS_NewInt32(ctx, exit_status), JS_PROP_C_W_E);
JSValue term_signal =
Expand All @@ -169,6 +169,9 @@ static void uv__exit_cb(uv_process_t *handle, int64_t exit_status, int term_sign
TJS_ClearPromise(ctx, &p->status.result);
}

JS_FreeValue(ctx, p->obj);
p->obj = JS_UNDEFINED;

maybe_close(p);
}

Expand Down Expand Up @@ -439,6 +442,7 @@ static JSValue tjs_spawn(JSContext *ctx, JSValueConst this_val, int argc, JSValu

JS_SetOpaque(obj, p);
ret = obj;
p->obj = JS_DupValue(ctx, obj);
goto cleanup;

fail:
Expand Down

0 comments on commit 1bb7db5

Please sign in to comment.