From e4726f08c9c4c939bae8b32ca96d9da8e885890f Mon Sep 17 00:00:00 2001 From: prompt.ac/@jeffrey Date: Sat, 08 Aug 2026 08:42:17 +0000 Subject: [PATCH] Make piece-logs-cli exit instead of lingering forever Each invocation is a one-shot read-only lookup, but the process never exited: the Mongo driver keeps a handle alive past disconnect(), so main() resolving was not enough to drain the event loop. On lith 32 of these had piled up over three weeks — the oldest 20.6 days — holding ~890MB of a 4GB box, which is most of why its memory read as full. Exit explicitly once main resolves, and disconnect from a finally so a thrown query cannot leave the connection open either. --- system/backend/piece-logs-cli.mjs | 26 +++++++++++++++++++------- 1 file(s) changed, 19 insertion(s)(+), 7 deletion(s)(-) diff --git a/system/backend/piece-logs-cli.mjs b/system/backend/piece-logs-cli.mjs --- a/system/backend/piece-logs-cli.mjs +++ b/system/backend/piece-logs-cli.mjs @@ -91,9 +91,15 @@ if (!opts.events) projection.events = 0; async function main() { const database = await connect(); - const runs = database.db.collection("piece-runs"); - let results = await runs.find(query, { projection }).sort({ updatedAt: -1 }).limit(opts.limit).toArray(); - await database.disconnect(); + let results; + try { + const runs = database.db.collection("piece-runs"); + results = await runs.find(query, { projection }).sort({ updatedAt: -1 }).limit(opts.limit).toArray(); + } finally { + // Disconnect even if the query throws, so a failed lookup cannot leave the + // driver holding a live connection. + await database.disconnect(); + } if (opts.grep) { const re = new RegExp(opts.grep, "i"); @@ -135,7 +141,13 @@ s = String(s ?? ""); return s.length > n ? s.slice(0, n - 1) + "…" : s; } -main().catch((err) => { - console.error("piece-logs-cli failed:", err?.stack || err); - process.exit(1); -}); +// Exit explicitly rather than waiting for the event loop to drain. The Mongo +// driver can keep a handle alive past disconnect(), and this is a one-shot +// read-only query — without this, invocations linger forever. On lith, 32 of +// them had accumulated over three weeks holding ~890MB. +main() + .then(() => process.exit(0)) + .catch((err) => { + console.error("piece-logs-cli failed:", err?.stack || err); + process.exit(1); + }); -- tangled.sh