fix: take over a lock only from a dead holder, one taker at a time

Step 11 voices: a late stale-lock mv could evict a fresh owner, a failed mv spun past the timeout, and the 10 s give-up sat under the 30 s stale age. Takeover now checks the holder's pid, re-reads the owner under a takeover lock, falls through to the give-up on failure, and reports an unwritable state directory at once.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
garrytanandcapy-ai[bot] committed 2026-10-31 19:19:13 +00:00
1 parent af0526778a
commit baebb453ab
2 files changed
+90 -22

No files matched your search

+41 -2
View File
@@ -792,7 +792,7 @@ describe('gstack-developer-profile concurrent writers (#3437)', () => {
expect(runDev('--derive').status).toBe(0);
const before = fs.readFileSync(path.join(tmpHome, 'developer-profile.json'), 'utf-8');
fs.mkdirSync(lockDir());
fs.writeFileSync(path.join(lockDir(), 'owner'), 'another-writer\n');
fs.writeFileSync(path.join(lockDir(), 'owner'), process.pid + '-held\n');
const env = { ...process.env, GSTACK_HOME: tmpHome, GSTACK_PROFILE_LOCK_TIMEOUT_MS: '300' };
for (const args of [['--derive'], ['--log-session', JSON.stringify({ date: '2026-10-31', mode: 'builder' })]]) {
const res = spawnSync(BIN_DEV, args, { env, encoding: 'utf-8', cwd: ROOT, timeout: 30_000 });
@@ -800,7 +800,7 @@ describe('gstack-developer-profile concurrent writers (#3437)', () => {
expect(res.stderr).toContain('developer-profile.json.lock');
}
expect(fs.readFileSync(path.join(tmpHome, 'developer-profile.json'), 'utf-8')).toBe(before);
expect(fs.readFileSync(path.join(lockDir(), 'owner'), 'utf-8')).toBe('another-writer\n');
expect(fs.readFileSync(path.join(lockDir(), 'owner'), 'utf-8')).toBe(process.pid + '-held\n');
});
test('a stale lock left by a crashed writer is taken over', () => {
@@ -813,6 +813,45 @@ describe('gstack-developer-profile concurrent writers (#3437)', () => {
expect(fs.existsSync(lockDir())).toBe(false);
});
test('a lock whose holder process is gone is taken over at once', () => {
const gone = spawnSync('true', [], { timeout: 10_000 }).pid;
fs.mkdirSync(lockDir());
fs.writeFileSync(path.join(lockDir(), 'owner'), gone + '-crashed\n');
const env = { ...process.env, GSTACK_HOME: tmpHome, GSTACK_PROFILE_LOCK_TIMEOUT_MS: '300' };
const res = spawnSync(BIN_DEV, ['--log-session', JSON.stringify({ date: '2026-10-31', mode: 'builder', project_slug: 'p' })], { env, encoding: 'utf-8', cwd: ROOT, timeout: 30_000 });
expect(res.stderr).toBe('');
expect(res.status).toBe(0);
expect((readProfile() as { sessions: unknown[] }).sessions.length).toBe(1);
expect(fs.existsSync(lockDir())).toBe(false);
});
test('a takeover already in progress is not repeated', () => {
const gone = spawnSync('true', [], { timeout: 10_000 }).pid;
fs.mkdirSync(lockDir());
fs.writeFileSync(path.join(lockDir(), 'owner'), gone + '-crashed\n');
fs.mkdirSync(lockDir() + '.takeover');
const env = { ...process.env, GSTACK_HOME: tmpHome, GSTACK_PROFILE_LOCK_TIMEOUT_MS: '300' };
const res = spawnSync(BIN_DEV, ['--log-session', JSON.stringify({ date: '2026-10-31', mode: 'builder' })], { env, encoding: 'utf-8', cwd: ROOT, timeout: 30_000 });
expect(res.status).toBe(5);
expect(fs.readFileSync(path.join(lockDir(), 'owner'), 'utf-8')).toBe(gone + '-crashed\n');
});
test('a takeover that cannot remove the lock still gives up', () => {
expect(runDev('--derive').status).toBe(0);
fs.mkdirSync(lockDir());
const old = new Date(Date.now() - 120_000);
fs.utimesSync(lockDir(), old, old);
fs.chmodSync(tmpHome, 0o555);
try {
const env = { ...process.env, GSTACK_HOME: tmpHome, GSTACK_PROFILE_LOCK_TIMEOUT_MS: '300' };
const res = spawnSync(BIN_DEV, ['--derive'], { env, encoding: 'utf-8', cwd: ROOT, timeout: 10_000 });
expect(res.signal).toBeNull();
expect([0, 5]).toContain(res.status);
} finally {
fs.chmodSync(tmpHome, 0o755);
}
}, 20_000);
test('a relative state root does not leave the lock behind', () => {
const env = { ...process.env, GSTACK_HOME: 'state' };
for (const args of [['--derive'], ['--log-session', JSON.stringify({ date: '2026-10-31', mode: 'builder', project_slug: 'p' })]]) {