diff --git a/packages/sim-cli/src/commands/protocol/source-dir.test.ts b/packages/sim-cli/src/commands/protocol/source-dir.test.ts index a574752f667..a4ea8eec75e 100644 --- a/packages/sim-cli/src/commands/protocol/source-dir.test.ts +++ b/packages/sim-cli/src/commands/protocol/source-dir.test.ts @@ -138,9 +138,14 @@ describe('withSourceDirLock', () => { expect(await withSourceDirLock(dir, async () => 'ran')).toBe('ran') }) - it('takes over a lock whose command died before recording its owner', async () => { + it.each([ + ['no owner file', undefined], + ['an empty owner file', ''], + ['an owner file cut short', '{"pid": 12'], + ])('takes over a lock whose command died before recording its owner: %s', async (_, owner) => { const lock = join(dir, SOURCE_DIR_LOCK) mkdirSync(lock, { recursive: true }) + if (owner !== undefined) writeFileSync(join(lock, 'owner.json'), owner) const twoMinutesAgo = Date.now() / 1000 - 120 utimesSync(lock, twoMinutesAgo, twoMinutesAgo) @@ -148,6 +153,17 @@ describe('withSourceDirLock', () => { expect(existsSync(lock)).toBe(false) }) + it('refuses as busy a fresh lock whose owner is still being written', async () => { + const lock = join(dir, SOURCE_DIR_LOCK) + mkdirSync(lock, { recursive: true }) + writeFileSync(join(lock, 'owner.json'), '') + + await expect(withSourceDirLock(dir, async () => 'ran')).rejects.toMatchObject({ + code: 'SOURCE_DIR_BUSY', + }) + expect(existsSync(join(lock, 'owner.json'))).toBe(true) + }) + it('a failed command on a missing nested directory leaves none of the folders the lock created', async () => { mkdirSync(join(dir, 'out')) writeFileSync(join(dir, 'out', 'kept.txt'), 'kept') diff --git a/packages/sim-cli/src/commands/protocol/source-dir/command-lock.ts b/packages/sim-cli/src/commands/protocol/source-dir/command-lock.ts index 13661563470..77f751fd6e6 100644 --- a/packages/sim-cli/src/commands/protocol/source-dir/command-lock.ts +++ b/packages/sim-cli/src/commands/protocol/source-dir/command-lock.ts @@ -60,16 +60,16 @@ type LockSighting = Pick /** * Whether the lock at `lock`, as `seen`, was left by a command that is gone: its process on this - * machine no longer runs, or it never recorded an owner and had not changed for a minute when it - * was seen. The age is the sighting's, not the folder's now, because a takeover's own claim - * changes the folder. A lock another machine holds (a shared folder) is never taken over. + * machine no longer runs, or it never recorded an owner (no owner file, or one `writeFile` created + * but had not yet filled) and had not changed for a minute when it was seen. The age is the + * sighting's, not the folder's now, because a takeover's own claim changes the folder. A lock + * another machine holds (a shared folder) is never taken over. */ async function isStaleLock(lock: string, seen: LockSighting): Promise { const owner = toRecord( - await readFile(join(lock, LOCK_OWNER_FILE), 'utf8').then( - (text): unknown => JSON.parse(text), - () => null - ) + await readFile(join(lock, LOCK_OWNER_FILE), 'utf8') + .then((text): unknown => JSON.parse(text)) + .catch(() => null) ) if (typeof owner.pid === 'number' && typeof owner.host === 'string') { return owner.host === hostname() && !isAlive(owner.pid)