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

fix: keep test temp cleanup from following symlink cycles

Adam Eivy48192a44c978
diff --git a/scripts/vitestTempRootSetup.js b/scripts/vitestTempRootSetup.js
index 75a0f7bc34..cda660766a 100644
--- a/scripts/vitestTempRootSetup.js
+++ b/scripts/vitestTempRootSetup.js
@@ -3,7 +3,7 @@
* redirects temp APIs before Vitest creates its module-transform cache.
* The server's exclusive capture projects share this single global owner.
*/
-import { readFileSync, readdirSync, rmSync, statSync } from 'node:fs';
+import { lstatSync, readFileSync, readdirSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import { OWNER_FILE, killTestBrowsersUnder } from './lib/vitestStaleRunRoots.js';
@@ -60,7 +60,10 @@ export function groupLeakPrefix(name) {
export function isEffectivelyEmpty(path) {
let stat;
try {
- stat = statSync(path);
+ // A leftover fixture can link outside the run root or back into itself.
+ // Inspect the link, never its target: following branching cycles can keep
+ // this synchronous scan busy indefinitely before cleanup gets a chance.
+ stat = lstatSync(path);
} catch {
return true;
}
diff --git a/scripts/vitestTempRootSetup.test.js b/scripts/vitestTempRootSetup.test.js
index 338eb7c546..3a732ccb72 100644
--- a/scripts/vitestTempRootSetup.test.js
+++ b/scripts/vitestTempRootSetup.test.js
@@ -1,4 +1,5 @@
-import { mkdirSync, mkdtempSync, rmSync, writeFileSync, existsSync } from 'node:fs';
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync, existsSync, readFileSync, symlinkSync } from 'node:fs';
+import { spawnSync } from 'node:child_process';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, describe, expect, it, vi } from 'vitest';
@@ -83,4 +84,38 @@ describe('vitestTempRootSetup teardown', () => {
]);
expect(leaked.exitCode).toBe(1);
});
+
+ it('removes leaked cyclic and dangling links without walking or changing their targets', () => {
+ const sandbox = mkdtempSync(join(tmpdir(), 'pvt-link-teardown-'));
+ const root = join(sandbox, 'run');
+ const outside = join(sandbox, 'outside');
+ const fixture = join(root, 'fixture-links');
+ const directoryLink = process.platform === 'win32' ? 'junction' : 'dir';
+ try {
+ mkdirSync(fixture, { recursive: true });
+ mkdirSync(outside);
+ writeFileSync(join(outside, 'keep'), 'host data');
+ // Two links make the old follow-symlinks walk branch until ELOOP at
+ // every leaf. Run the real teardown in a bounded child so a regression
+ // cannot block the enclosing runner's event loop and its test timeout.
+ symlinkSync(fixture, join(fixture, 'a'), directoryLink);
+ symlinkSync(fixture, join(fixture, 'b'), directoryLink);
+ symlinkSync(outside, join(root, 'outside-link'), directoryLink);
+ symlinkSync(join(sandbox, 'missing'), join(root, 'dangling-link'), directoryLink);
+ const result = spawnSync(process.execPath, ['--input-type=module', '-e',
+ `import { teardown } from ${JSON.stringify(new URL('./vitestTempRootSetup.js', import.meta.url).href)}; teardown();`], {
+ env: { ...process.env, PORTOS_TEST_TEMP_ROOT: root },
+ encoding: 'utf8', timeout: 5000,
+ });
+ expect(result.error).toBeUndefined();
+ expect(result.status, result.stderr).toBe(1);
+ for (const name of ['fixture-links', 'outside-link', 'dangling-link']) {
+ expect(result.stderr).toContain(`test temp leak: ${name} ×1`);
+ }
+ expect(existsSync(root)).toBe(false);
+ expect(readFileSync(join(outside, 'keep'), 'utf8')).toBe('host data');
+ } finally {
+ rmSync(sandbox, { recursive: true, force: true });
+ }
+ });
});