Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FinalizationRegistry refcounting bug #656

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2107,6 +2107,8 @@ void JS_FreeRuntime(JSRuntime *rt)
}
#endif

assert(list_empty(&rt->gc_obj_list));

/* free the classes */
for(i = 0; i < rt->class_count; i++) {
JSClass *cl = &rt->class_array[i];
Expand Down Expand Up @@ -2239,9 +2241,6 @@ void JS_FreeRuntime(JSRuntime *rt)
}
#endif

// FinalizationRegistry finalizers have run, no objects should remain
assert(list_empty(&rt->gc_obj_list));

{
JSMallocState *ms = &rt->malloc_state;
rt->mf.js_free(ms->opaque, rt);
Expand Down Expand Up @@ -54985,9 +54984,7 @@ static const JSClassShortDef js_finrec_class_def[] = {

static JSValue js_finrec_job(JSContext *ctx, int argc, JSValue *argv)
{
JSValue ret = JS_Call(ctx, argv[0], JS_UNDEFINED, 1, &argv[1]);
JS_FreeValue(ctx, argv[1]);
return ret;
return JS_Call(ctx, argv[0], JS_UNDEFINED, 1, &argv[1]);
}

void JS_AddIntrinsicWeakRef(JSContext *ctx)
Expand Down Expand Up @@ -55078,6 +55075,7 @@ static void reset_weak_ref(JSRuntime *rt, JSWeakRefRecord **first_weak_ref)
args[1] = fre->held_val;
JS_EnqueueJob(frd->ctx, js_finrec_job, 2, args);
}
JS_FreeValueRT(rt, fre->held_val);
JS_FreeValueRT(rt, fre->token);
js_free_rt(rt, fre);
break;
Expand Down
13 changes: 13 additions & 0 deletions tests/bug648.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*---
negative:
phase: runtime
type: Error
---*/
let finrec = new FinalizationRegistry(v => {})
let object = {object:"object"}
finrec.register(object, {held:"held"}, {token:"token"})
object = undefined
// abrupt termination should not leak |held|
// unfortunately only shows up in qjs, not run-test262,
// but still good to have a regression test
throw Error("ok")
Loading