Memory leak: `taskIdentifiers` cache keeps every `run()`'s options for the life of the process
还没有人认领这个 Issue。
评估
- 难度
- 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 分钟
- 30 天内合并 PR
- 3
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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
相似的 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
维护者通常 1 天内回复
-
[Bug]: Manager UI logo fails to load — broken external URL可能已有人在做 @kavindu-udara 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 68/100