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 (agy): survive an unreadable or concurrently-removed lock

[._.]/ Adam Eivyf4752f4d0d39
diff --git a/server/lib/gitStaleLock.js b/server/lib/gitStaleLock.js
index 2ab3413639..9822d9ed03 100644
--- a/server/lib/gitStaleLock.js
+++ b/server/lib/gitStaleLock.js
@@ -77,8 +77,23 @@ export function isStaleGitLock(lockPath) {
// Accept both separators: a Windows git reports backslash paths, and a POSIX
// path can reach a Windows Node through a checked-in fixture or a log.
if (!lockPath.split(/[\\/]/).includes('.git')) return false;
- if (!existsSync(lockPath)) return false;
- return Date.now() - statSync(lockPath).mtimeMs >= STALE_GIT_LOCK_MIN_AGE_MS;
+ // One guarded stat rather than existsSync + statSync: the gap between those
+ // two is a race this module actively creates — a concurrent retry (or git
+ // itself) removing the lock in the window would throw ENOENT out of a
+ // predicate whose callers are handling a git failure, replacing the real
+ // error with a confusing one. An unreadable lock answers "not stale" too:
+ // this may not throw, and nothing can be proven about a lock it can't stat.
+ const mtimeMs = statMtimeMs(lockPath);
+ return mtimeMs !== null && Date.now() - mtimeMs >= STALE_GIT_LOCK_MIN_AGE_MS;
+}
+
+/** A path's mtime, or null when it is missing or cannot be read. */
+function statMtimeMs(path) {
+ try {
+ return statSync(path).mtimeMs;
+ } catch {
+ return null;
+ }
}
/**
@@ -137,7 +152,17 @@ export function clearStaleGitLocksIn(gitDir) {
if (!root) return [];
const cleared = [];
const walk = (dir) => {
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
+ // A directory git removes mid-walk (gc pruning its temp dirs), or one this
+ // process cannot read, must not abort the whole sweep — this runs ahead of
+ // the self-update, where throwing would be a worse outcome than missing a
+ // lock. Skip the subtree and keep going.
+ let entries;
+ try {
+ entries = readdirSync(dir, { withFileTypes: true });
+ } catch {
+ return;
+ }
+ for (const entry of entries) {
const full = join(dir, entry.name);
if (entry.isDirectory()) {
if (!UNSWEPT_GIT_DIRS.has(entry.name)) walk(full);
diff --git a/server/lib/gitStaleLock.test.js b/server/lib/gitStaleLock.test.js
index 8e084c62b2..192c7bcd1c 100644
--- a/server/lib/gitStaleLock.test.js
+++ b/server/lib/gitStaleLock.test.js
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
-import { existsSync, mkdirSync, utimesSync, writeFileSync } from 'fs';
+import { chmodSync, existsSync, mkdirSync, utimesSync, writeFileSync } from 'fs';
import { mkdtemp, rm } from 'fs/promises';
import { tmpdir } from 'os';
import { join } from 'path';
@@ -74,6 +74,16 @@ describe('isStaleGitLock', () => {
it('refuses a lock that is already gone', () => {
expect(isStaleGitLock(join(dir, '.git/index.lock'))).toBe(false);
});
+
+ it('answers rather than throws when the lock cannot be stat-ed', () => {
+ // Callers run this while handling a git failure; an ENOENT/EACCES escaping
+ // here would replace the real git error with a confusing one. The ENOENT
+ // window is one this module creates itself — a concurrent retry can clear
+ // the lock between the caller's check and this stat.
+ const unreadable = join(dir, '.git/nonexistent-dir/index.lock');
+ expect(() => isStaleGitLock(unreadable)).not.toThrow();
+ expect(isStaleGitLock(unreadable)).toBe(false);
+ });
});
describe('clearStaleGitLock', () => {
@@ -126,6 +136,21 @@ describe('clearStaleGitLocksIn', () => {
expect(clearStaleGitLocksIn(join(dir, 'nope'))).toEqual([]);
});
+ it('keeps sweeping when one subtree cannot be read', () => {
+ // git prunes its own temp dirs; a directory that disappears mid-walk must
+ // not abort a sweep that runs ahead of the self-update.
+ const reachable = writeLock('.git/modules/lib/dep/index.lock', ABANDONED);
+ const locked = join(dir, '.git/unreadable');
+ mkdirSync(locked, { recursive: true });
+ chmodSync(locked, 0o000);
+
+ try {
+ expect(clearStaleGitLocksIn(join(dir, '.git'))).toEqual([reachable]);
+ } finally {
+ chmodSync(locked, 0o755);
+ }
+ });
+
// Every CoS agent runs in a worktree, where `.git` is a FILE pointing at
// `<common>/.git/worktrees/<name>` — and the lock that wedges submodule
// checkout is not in there, it is in the COMMON dir both trees share.