Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/sim-cli/src/commands/protocol/source-dir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,32 @@ 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)

expect(await withSourceDirLock(dir, async () => 'ran')).toBe('ran')
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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ type LockSighting = Pick<Stats, 'dev' | 'ino' | 'mtimeMs'>

/**
* 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<boolean> {
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)
Expand Down