txiki.js icon indicating copy to clipboard operation
txiki.js copied to clipboard

Issue with FinalizationRegistry or gc

Open qwtel opened this issue 1 year ago • 2 comments

Either FinalizationRegistry or garbage collection is not working as expected for me.

Minimally reproducible example:

// file: test.js
(async () => {
  const loggingFinalizer = new FinalizationRegistry((x) => { console.log('FINALIZING', x) });

  {
    let obj = { name: 'John' };
    loggingFinalizer.register(obj, obj.name);
    obj = null;
  }

  if ('tjs' in globalThis) globalThis.tjs.engine.gc.run();
  if ('gc' in globalThis) globalThis.gc();

  await new Promise((r) => setTimeout(r, 1000));
})();

Should be logging FINALIZING John, but exits after 1s.

Works as expected in Deno: deno run -A --v8-flags=--expose-gc ./test.js

Commit d6f9888d671f2f729b7e6c2e92f8c67f8db38e85 on macOS arch64

qwtel avatar Jul 25 '24 11:07 qwtel

In quickjs { name: 'John'} will not be deleted by the garbage collection, but by the reference counter. Not sure if this is the reason.

KaruroChori avatar Jul 25 '24 17:07 KaruroChori

I'll take a look. I'm QuickJS GC is implemented with refcounting and cycle detection.

I wonder if what happens is that GC sees both the finrec and obj and collects them at the same time.

saghul avatar Jul 26 '24 08:07 saghul

Moved issue to QuickJS-ng.

saghul avatar Sep 03 '24 08:09 saghul