Skip to content

Commit 976e3c3

Browse files
authored
fix(sim-cli): take over a stale source lock whose owner file was left empty or cut short (#10300)
isStaleLock now treats a malformed or empty owner.json as an unrecorded owner, so the 60-second age rule decides: a stale lock left by a command that died while writing its owner is taken over, and a fresh lock read mid-write is refused as busy instead of crashing with a SyntaxError.
1 parent 29b63df commit 976e3c3

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

‎packages/sim-cli/src/commands/protocol/source-dir.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,32 @@ describe('withSourceDirLock', () => {
138138
expect(await withSourceDirLock(dir, async () => 'ran')).toBe('ran')
139139
})
140140

141-
it('takes over a lock whose command died before recording its owner', async () => {
141+
it.each([
142+
['no owner file', undefined],
143+
['an empty owner file', ''],
144+
['an owner file cut short', '{"pid": 12'],
145+
])('takes over a lock whose command died before recording its owner: %s', async (_, owner) => {
142146
const lock = join(dir, SOURCE_DIR_LOCK)
143147
mkdirSync(lock, { recursive: true })
148+
if (owner !== undefined) writeFileSync(join(lock, 'owner.json'), owner)
144149
const twoMinutesAgo = Date.now() / 1000 - 120
145150
utimesSync(lock, twoMinutesAgo, twoMinutesAgo)
146151

147152
expect(await withSourceDirLock(dir, async () => 'ran')).toBe('ran')
148153
expect(existsSync(lock)).toBe(false)
149154
})
150155

156+
it('refuses as busy a fresh lock whose owner is still being written', async () => {
157+
const lock = join(dir, SOURCE_DIR_LOCK)
158+
mkdirSync(lock, { recursive: true })
159+
writeFileSync(join(lock, 'owner.json'), '')
160+
161+
await expect(withSourceDirLock(dir, async () => 'ran')).rejects.toMatchObject({
162+
code: 'SOURCE_DIR_BUSY',
163+
})
164+
expect(existsSync(join(lock, 'owner.json'))).toBe(true)
165+
})
166+
151167
it('a failed command on a missing nested directory leaves none of the folders the lock created', async () => {
152168
mkdirSync(join(dir, 'out'))
153169
writeFileSync(join(dir, 'out', 'kept.txt'), 'kept')

‎packages/sim-cli/src/commands/protocol/source-dir/command-lock.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ type LockSighting = Pick<Stats, 'dev' | 'ino' | 'mtimeMs'>
6060

6161
/**
6262
* Whether the lock at `lock`, as `seen`, was left by a command that is gone: its process on this
63-
* machine no longer runs, or it never recorded an owner and had not changed for a minute when it
64-
* was seen. The age is the sighting's, not the folder's now, because a takeover's own claim
65-
* changes the folder. A lock another machine holds (a shared folder) is never taken over.
63+
* machine no longer runs, or it never recorded an owner (no owner file, or one `writeFile` created
64+
* but had not yet filled) and had not changed for a minute when it was seen. The age is the
65+
* sighting's, not the folder's now, because a takeover's own claim changes the folder. A lock
66+
* another machine holds (a shared folder) is never taken over.
6667
*/
6768
async function isStaleLock(lock: string, seen: LockSighting): Promise<boolean> {
6869
const owner = toRecord(
69-
await readFile(join(lock, LOCK_OWNER_FILE), 'utf8').then(
70-
(text): unknown => JSON.parse(text),
71-
() => null
72-
)
70+
await readFile(join(lock, LOCK_OWNER_FILE), 'utf8')
71+
.then((text): unknown => JSON.parse(text))
72+
.catch(() => null)
7373
)
7474
if (typeof owner.pid === 'number' && typeof owner.host === 'string') {
7575
return owner.host === hostname() && !isAlive(owner.pid)

0 commit comments

Comments
 (0)