From a1e9d59beb77491176f0e227605ed03c638a31cd Mon Sep 17 00:00:00 2001 From: Devpaul-01 Date: Fri, 27 Nov 2026 18:04:35 +0000 Subject: [PATCH 1/5] Release task identifier caches along with their options --- src/taskIdentifiers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/taskIdentifiers.ts b/src/taskIdentifiers.ts index eb5d630a..a7392704 100644 --- a/src/taskIdentifiers.ts +++ b/src/taskIdentifiers.ts @@ -16,7 +16,7 @@ interface Cache { lastStr: string | Promise; lastDigest: TaskDetails | Promise; } -const cacheByOptions = new Map(); +const cacheByOptions = new WeakMap(); export function getTaskDetails( compiledSharedOptions: CompiledSharedOptions, From 51fd9e64c39ccb5f215bf69be84a5aa3f76b2fa0 Mon Sep 17 00:00:00 2001 From: Devpaul-01 Date: Fri, 27 Nov 2026 18:04:36 +0000 Subject: [PATCH 2/5] Add a changeset for the task identifier cache leak --- .changeset/free-task-identifier-cache.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/free-task-identifier-cache.md diff --git a/.changeset/free-task-identifier-cache.md b/.changeset/free-task-identifier-cache.md new file mode 100644 index 00000000..84241fe3 --- /dev/null +++ b/.changeset/free-task-identifier-cache.md @@ -0,0 +1,6 @@ +--- +"graphile-worker": patch +--- + +Fix a memory leak where each `run()` left its options object (and everything it +references) in the task identifier cache after the runner had stopped. From 016ed35cbba84dd2db28023561ec900f99880dc0 Mon Sep 17 00:00:00 2001 From: Devpaul-01 Date: Sat, 28 Nov 2026 06:17:35 +0000 Subject: [PATCH 3/5] Test that the task identifier cache does not retain released options --- __tests__/taskIdentifiers.test.ts | 67 +++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 __tests__/taskIdentifiers.test.ts diff --git a/__tests__/taskIdentifiers.test.ts b/__tests__/taskIdentifiers.test.ts new file mode 100644 index 00000000..0f03921b --- /dev/null +++ b/__tests__/taskIdentifiers.test.ts @@ -0,0 +1,67 @@ +import { setFlagsFromString } from "node:v8"; +import { runInNewContext } from "node:vm"; + +import type { EnhancedWithPgClient } from "../src/interfaces.ts"; +import type { CompiledSharedOptions } from "../src/lib.ts"; +import { getTaskDetails } from "../src/taskIdentifiers.ts"; + +// Lets the test force a garbage collection without needing `--expose-gc` +setFlagsFromString("--expose-gc"); +const gc = runInNewContext("gc") as () => void; + +const tasks = { task1: async () => {} }; + +function makeWithPgClient() { + const counter = { queries: 0 }; + const withPgClient = { + withRetries: async (cb: (client: unknown) => Promise) => + cb({ + query: async () => { + counter.queries++; + return { rows: [{ id: 1, identifier: "task1" }] }; + }, + }), + } as unknown as EnhancedWithPgClient; + return { withPgClient, counter }; +} + +function makeOptions() { + return { + escapedWorkerSchema: '"graphile_worker"', + } as unknown as CompiledSharedOptions; +} + +test("getTaskDetails caches the task identifiers for the same options", async () => { + const { withPgClient, counter } = makeWithPgClient(); + const options = makeOptions(); + + const first = await getTaskDetails(options, withPgClient, tasks); + const queriesAfterFirst = counter.queries; + const second = await getTaskDetails(options, withPgClient, tasks); + + expect(first.taskIds).toEqual([1]); + expect(second).toBe(first); + expect(counter.queries).toBe(queriesAfterFirst); +}); + +test("getTaskDetails does not keep options alive once they are released", async () => { + let collected = false; + const registry = new FinalizationRegistry(() => { + collected = true; + }); + + // Keep the only reference to `options` inside this function's scope + await (async () => { + const { withPgClient } = makeWithPgClient(); + const options = makeOptions(); + registry.register(options, "options"); + await getTaskDetails(options, withPgClient, tasks); + })(); + + for (let attempt = 0; attempt < 20 && !collected; attempt++) { + gc(); + await new Promise((resolve) => setTimeout(resolve, 10)); + } + + expect(collected).toBe(true); +}); From a1d1794ff23e19b58608a9414605ba027337904c Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Wed, 2 Dec 2026 13:46:05 +0000 Subject: [PATCH 4/5] Remove unnecessary test --- __tests__/taskIdentifiers.test.ts | 67 ------------------------------- 1 file changed, 67 deletions(-) delete mode 100644 __tests__/taskIdentifiers.test.ts diff --git a/__tests__/taskIdentifiers.test.ts b/__tests__/taskIdentifiers.test.ts deleted file mode 100644 index 0f03921b..00000000 --- a/__tests__/taskIdentifiers.test.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { setFlagsFromString } from "node:v8"; -import { runInNewContext } from "node:vm"; - -import type { EnhancedWithPgClient } from "../src/interfaces.ts"; -import type { CompiledSharedOptions } from "../src/lib.ts"; -import { getTaskDetails } from "../src/taskIdentifiers.ts"; - -// Lets the test force a garbage collection without needing `--expose-gc` -setFlagsFromString("--expose-gc"); -const gc = runInNewContext("gc") as () => void; - -const tasks = { task1: async () => {} }; - -function makeWithPgClient() { - const counter = { queries: 0 }; - const withPgClient = { - withRetries: async (cb: (client: unknown) => Promise) => - cb({ - query: async () => { - counter.queries++; - return { rows: [{ id: 1, identifier: "task1" }] }; - }, - }), - } as unknown as EnhancedWithPgClient; - return { withPgClient, counter }; -} - -function makeOptions() { - return { - escapedWorkerSchema: '"graphile_worker"', - } as unknown as CompiledSharedOptions; -} - -test("getTaskDetails caches the task identifiers for the same options", async () => { - const { withPgClient, counter } = makeWithPgClient(); - const options = makeOptions(); - - const first = await getTaskDetails(options, withPgClient, tasks); - const queriesAfterFirst = counter.queries; - const second = await getTaskDetails(options, withPgClient, tasks); - - expect(first.taskIds).toEqual([1]); - expect(second).toBe(first); - expect(counter.queries).toBe(queriesAfterFirst); -}); - -test("getTaskDetails does not keep options alive once they are released", async () => { - let collected = false; - const registry = new FinalizationRegistry(() => { - collected = true; - }); - - // Keep the only reference to `options` inside this function's scope - await (async () => { - const { withPgClient } = makeWithPgClient(); - const options = makeOptions(); - registry.register(options, "options"); - await getTaskDetails(options, withPgClient, tasks); - })(); - - for (let attempt = 0; attempt < 20 && !collected; attempt++) { - gc(); - await new Promise((resolve) => setTimeout(resolve, 10)); - } - - expect(collected).toBe(true); -}); From 9aaad1fd2db43bab30b2375af9d8cfe5d708f592 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Wed, 2 Dec 2026 13:54:07 +0000 Subject: [PATCH 5/5] Update changeset --- .changeset/free-task-identifier-cache.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.changeset/free-task-identifier-cache.md b/.changeset/free-task-identifier-cache.md index 84241fe3..48ab6618 100644 --- a/.changeset/free-task-identifier-cache.md +++ b/.changeset/free-task-identifier-cache.md @@ -2,5 +2,7 @@ "graphile-worker": patch --- -Fix a memory leak where each `run()` left its options object (and everything it -references) in the task identifier cache after the runner had stopped. +Convert an internal cache from using a Map to a WeakMap, fixing a memory leak +impacting users that call Graphile Worker's methods (e.g. `run()`) many times +and provide new options objects each time. Most users will only call `run()` +once so would not be impacted by this.