Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (claude): register one process-exit hook for all git templates

[._.]/ Adam Eivy04eb66a742be
diff --git a/server/lib/gitTestRepo.js b/server/lib/gitTestRepo.js
index b03314906e..807b8397fe 100644
--- a/server/lib/gitTestRepo.js
+++ b/server/lib/gitTestRepo.js
@@ -54,6 +54,23 @@ function wipeSync(scratch) {
try { rmSync(scratch, { recursive: true, force: true }); } catch { /* already gone */ }
}
+// Vitest isolates the module cache per test file, so `buildTemplate` can
+// run more than once in a worker. Track every template dir on globalThis
+// and register a single process-exit hook — otherwise watch-mode reruns
+// stack `process.on('exit')` listeners until MaxListenersExceededWarning.
+const TEMPLATE_DIRS_KEY = '__portosGitTemplateDirs';
+const TEMPLATE_HOOK_KEY = '__portosGitTemplateExitHooked';
+
+function rememberTemplate(scratch) {
+ const dirs = (globalThis[TEMPLATE_DIRS_KEY] ??= []);
+ dirs.push(scratch);
+ if (globalThis[TEMPLATE_HOOK_KEY]) return;
+ globalThis[TEMPLATE_HOOK_KEY] = true;
+ process.on('exit', () => {
+ for (const dir of dirs) wipeSync(dir);
+ });
+}
+
async function buildTemplate() {
const scratch = await mkdtemp(join(tmpdir(), TEMPLATE_PREFIX));
const repo = join(scratch, 'primary');
@@ -69,7 +86,7 @@ async function buildTemplate() {
// Copied sandboxes are torn down per-test; the template itself lives for
// the worker. Wipe it on process exit so watch-mode / repeated runs do
// not accumulate repos under os.tmpdir().
- process.on('exit', () => wipeSync(scratch));
+ rememberTemplate(scratch);
return { scratch, repo, origin };
}
diff --git a/server/lib/gitTestRepo.test.js b/server/lib/gitTestRepo.test.js
index b43c0e91fe..88d289faa5 100644
--- a/server/lib/gitTestRepo.test.js
+++ b/server/lib/gitTestRepo.test.js
@@ -11,6 +11,24 @@ import {
destroyGitSandbox,
} from './gitTestRepo.js';
+describe('template exit hook', () => {
+ it('registers a single process-exit listener across module re-imports', async () => {
+ // First sandbox builds the worker template and should arm the hook.
+ const box = await makeGitSandbox({ prefix: 'portos-git-fx-hook-' });
+ sandboxes.push(box.scratch);
+ const before = process.listenerCount('exit');
+ // A second build in this same module uses the cached template promise
+ // and must not add another listener. The watch-mode case (fresh module
+ // cache) is the same function: rememberTemplate no-ops once hooked.
+ const box2 = await makeGitSandbox({ prefix: 'portos-git-fx-hook2-' });
+ sandboxes.push(box2.scratch);
+ expect(process.listenerCount('exit')).toBe(before);
+ expect(globalThis.__portosGitTemplateExitHooked).toBe(true);
+ expect(Array.isArray(globalThis.__portosGitTemplateDirs)).toBe(true);
+ expect(globalThis.__portosGitTemplateDirs.length).toBeGreaterThan(0);
+ });
+});
+
const sandboxes = [];
afterEach(async () => {
await Promise.all(sandboxes.splice(0).map((s) => destroyGitSandbox(s)));