Collecting WeakMap keys requires multiple GC passes

オープン
#581 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

@jluxenberg がすでに取り組んでいます。

2026年10月27日 から。

  • #582 @jluxenberg による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
c

調査の方向性

Start with the attached weakmap_gc_repro.c and run it using the build commands in the issue to confirm both cases. Then trace QuickJS's WeakMap and weak-reference handling in the GC; the issue does not name source files or tests, so locate the relevant code and existing GC tests. Done means the self-referencing key is collected and the chain of keys is freed after a fixed number of GC passes.

索引モデルが issue の本文から書いたものです。

説明

Issue

I think I've found two bugs related to weak references / WeakMap handling by the GC.

(1) An entry whose value references its own key is never collected while the WeakMap is alive.
/* Case 1: the value references its own key. */
(function () {
  var key = {};
  map.set(key, { backRef: key });
  globalThis.selfRef = new WeakRef(key);
})();

Expected result:

  • key is collected after one pass of the GC

Actual result:

  • key is never collected by the GC but should be collected on the first pass.
(2) A chain of entries (each value references the next entry's key) takes n JS_RunGC() calls to free
/* Case 2: a chain of 20 entries; each value references the next key. */
(function () {
  var key = {};
  for (var i = 0; i < 20; i++) {
    var next = {};
    map.set(key, { child: next });
    key = next;
  }
  globalThis.chainTail = new WeakRef(key);
})();

Expected result:

  • All 20 keys is collected after a fixed number (ideally one) passes of the GC.

Actual result:

  • Collecting n keys appears to require n passes of the GC.
Repro

Here's a program that reproduces the issue. Compile it like this:

quickjs $ make libquickjs.a
quickjs $ cc -O2 -I. weakmap_gc_repro.c libquickjs.a -lm -lpthread -o weakmap_gc_repro
quickjs $ ./weakmap_gc_repro

weakmap_gc_repro.c

主要言語
C
スター
11.1k
フォーク
1.2k
平均マージ
11時間 20分
マージ済み PR(30日)
1

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

bellard/quickjs のほかの issue

bellard/quickjs の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。