Memory leak: `taskIdentifiers` cache keeps every `run()`'s options for the life of the process
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 88/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- node.js, postgresql, typescript
- 領域
- backend, performance
調査の方向性
src/taskIdentifiers.ts を読み、各 run() 用にオプションをコンパイルするときに cacheByOptions がどのように参照されるかを追ってください。Issue では、モジュールレベルのキャッシュを Map から WeakMap に変更することが修正案として示されています。タスク識別子に関する既存のテストを確認し、繰り返し実行してもオプションが保持されないことを確かめる回帰チェックを追加するか、実行してください。繰り返し実行してもキャッシュがヒープ増加を引き起こさなくなれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Version: graphile-worker 0.18.0 (the same code is on main), Node 24.22.3, PostgreSQL 16.14
What happens
A process that calls run() and runner.stop() repeatedly never frees what each run used. The heap grows by a fixed amount a run until the process runs out of memory.
We start a runner, let it drain the queue and stop it, on a timer, in a process that stays up. With a larger task list and a crontab we measured about 38 KB a run, and the process reached its heap limit (about 256 MB) after several thousand runs.
A process that calls run() once is not affected.
Cause
src/taskIdentifiers.ts keys a module-level Map on the compiled options and never deletes an entry:
const cacheByOptions = new Map<CompiledSharedOptions, Cache>();
Each run() compiles a new options object, so each adds an entry, which keeps the options and everything reachable from them.
Reproduction
// node --expose-gc repro.mjs (needs DATABASE_URL)
import pg from 'pg';
import { Logger, run } from 'graphile-worker';
const pgPool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
pgPool.on('error', () => {});
pgPool.on('connect', (c) => c.on('error', () => {}));
const logger = new Logger(() => () => {});
const heapKB = () => (gc(), gc(), process.memoryUsage().heapUsed / 1024);
async function once() {
const runner = await run({
pgPool, logger, noHandleSignals: true, taskList: { noop: async () => {} },
});
await new Promise((r) => setTimeout(r, 20)); // let it poll
await runner.stop();
await runner.promise;
}
for (let i = 0; i < 100; i++) await once();
let last = heapKB();
for (let block = 1; block <= 6; block++) {
for (let i = 0; i < 200; i++) await once();
const now = heapKB();
console.log(`${block * 200} runs: ${(now / 1024).toFixed(1)} MB (${((now - last) / 200).toFixed(1)} KB a run)`);
last = now;
}
await pgPool.end();
Heap after garbage collection, and growth a run over the last 200:
| Runs | 0.18.0 | with WeakMap |
|---|---|---|
| 200 | 12.4 MB (12.5 KB) | 10.8 MB (4.5 KB) |
| 400 | 14.6 MB (11.4 KB) | 10.9 MB (0.4 KB) |
| 600 | 17.0 MB (12.3 KB) | 11.1 MB (1.2 KB) |
| 800 | 19.2 MB (11.3 KB) | 11.1 MB (0.0 KB) |
| 1000 | 21.4 MB (11.3 KB) | 11.1 MB (0.1 KB) |
| 1200 | 23.6 MB (11.1 KB) | 11.2 MB (0.1 KB) |
Suggested fix
-const cacheByOptions = new Map<CompiledSharedOptions, Cache>();
+const cacheByOptions = new WeakMap<CompiledSharedOptions, Cache>();
The keys are always objects and the map is only read with get and written with set, so nothing else changes. We run 0.18.0 with this patch.
Related: #638 and #637 are about the same cache keeping a rejected lookup. This is a separate problem.
- 主要言語
- TypeScript
- スター
- 2.5k
- フォーク
- 134
- 平均マージ
- 16時間 24分
- マージ済み PR(30日)
- 3
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートなし
- コントリビューションガイドなし
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
graphile/worker のほかの issue
-
Task identifier cache keeps a rejected lookup forever, wedging the runner after a connection drop対応中かも @agustin18 が 2 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
Batched `completeJobs`/`failJobs` are never retried: the retry loop in `batch()` exits after the first failure対応中かも @ujjawal-yadav が 42 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
-
`runner.promise` resolves when the worker pool exits with an error対応中かも @Krrishkumar125 が 35 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
-
Write docs for local queue!対応中かも @Krrishkumar125 が 34 日前に担当しました。 オープンdocs
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
Unhandled rejection when recording a job's outcome fails with unbatched completeJob/failJob対応中かも @cpruijsen が 44 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 62/100
graphile/worker の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
abhigyanpatwari/GitNexus#4124 ·
メンテナーはふだん 1 日以内に返信
-
good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
laaielichter/Godotwright#250 ·
メンテナーはふだん 1 日以内に返信
-
An Antigravity tab can quote an old Assist message as the owner's words in a resumed conversationオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
Retry an empty final answer when Gemini reports MALFORMED_RESPONSE (gemini-4-hydrogen)対応中かも @waleedkadous が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
iaser-ai/ansari#444 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[Bug]: Manager UI logo fails to load — broken external URL対応中かも @kavindu-udara が今日担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100