From 45c3159bc391b19ee2e7f50936e312d735148ba4 Mon Sep 17 00:00:00 2001 From: "prompt.ac/@jeffrey" Date: Wed, 16 Sep 2026 09:37:54 -0700 Subject: [PATCH] kidlisp: let the backfill exit when it is done MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The first --apply run wrote all 764 rows, printed its summary, and then sat there. Thirteen minutes later the process was still alive. `connect()` returns a pooled singleton whose `disconnect()` is a no-op unless AC_DB_CLOSE=1 โ€” deliberate, so serverless handlers reuse the pool, and database.mjs:118 already says scripts should call `closePool()` instead. This one only disconnected, so nothing ever closed the pool and the event loop stayed up. Now closes it in a `finally`, so it exits on the error path too, and uses `process.exitCode` rather than `process.exit(1)` so a failure cannot truncate output mid-flush. Dry run against production: exit 0 in one second. Co-Authored-By: Claude Opus 5 (1M context) --- system/backend/kidlisp-backfill.mjs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/system/backend/kidlisp-backfill.mjs b/system/backend/kidlisp-backfill.mjs index 750ceb6856..0a4d0f0e5c 100644 --- a/system/backend/kidlisp-backfill.mjs +++ b/system/backend/kidlisp-backfill.mjs @@ -22,7 +22,7 @@ // Safe to re-run: the projection upserts on `code` with $setOnInsert, so an // existing row is never modified and a partial run simply resumes. -import { connect } from "./database.mjs"; +import { connect, closePool } from "./database.mjs"; import { sidecar, kidlispDatomicEnabled } from "./kidlisp-sidecar.mjs"; import { projectKidlispPiece } from "./kidlisp-projection.mjs"; @@ -75,7 +75,6 @@ async function main() { if (!apply) { console.log("๐Ÿงช Dry run. Re-run with --apply to write these rows."); - await database.disconnect(); return; } @@ -101,11 +100,16 @@ async function main() { if (failed) console.log(` ${failed} failed โ€” re-run to retry, the upsert is idempotent`); console.log("\nNothing to purge: the feeds read Mongo live, and oven's thumbnails"); console.log("never depended on it."); - - await database.disconnect(); } -main().catch((err) => { - console.error("โŒ Backfill failed:", err?.message || err); - process.exit(1); -}); +// `connect()` hands back a pooled singleton and its `disconnect()` is a no-op +// unless AC_DB_CLOSE=1 (see database.mjs) โ€” deliberate, so serverless handlers +// reuse the pool. A CLI has to close it by hand or the process just sits there +// after printing its last line, which is exactly what the first run of this +// script did. +main() + .catch((err) => { + console.error("โŒ Backfill failed:", err?.message || err); + process.exitCode = 1; + }) + .finally(() => closePool()); -- 2.51.2