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
22 changes: 22 additions & 0 deletions packages/host-kit/src/internal/process-lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ test('acquireProcessLock creates and releases a lock directory', async () => {
assert.equal(fs.existsSync(lockDirPath), false);
});

test.skipIf(process.platform === 'win32')(
'acquireProcessLock keeps the lock owner-only under any umask',
async () => {
for (const umask of [0o200, 0o000]) {
const lockDirPath = path.join(tmpDir, `umask-${umask.toString(8)}.lock`);
const previousUmask = process.umask(umask);
let release: ProcessLockRelease;
try {
release = await acquireProcessLock({ lockDirPath, owner: currentProcessOwner() });
} finally {
process.umask(previousUmask);
}
assert.equal(fs.statSync(lockDirPath).mode & 0o777, 0o700);
for (const entry of fs.readdirSync(lockDirPath)) {
assert.equal(fs.statSync(path.join(lockDirPath, entry)).mode & 0o777, 0o600);
}
await release();
assert.equal(fs.existsSync(lockDirPath), false);
}
},
);

test('acquireProcessLock reclaims locks owned by dead processes', async () => {
const lockDirPath = path.join(tmpDir, 'stale.lock');
writeDeadLockFixture(lockDirPath, Date.now() - 10_000);
Expand Down
8 changes: 7 additions & 1 deletion packages/host-kit/src/internal/process-lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,16 @@ function publishClaim(
): GuardedAcquisitionOutcome {
const { lockDirPath, owner } = params;
try {
fs.mkdirSync(lockDirPath);
fs.mkdirSync(lockDirPath, { mode: 0o700 });
} catch (error) {
if (errorCode(error) !== 'EEXIST') throw error;
return { status: 'unproven' };
}
const claimToken = crypto.randomUUID();
const claim: ProcessLockOwnerRecord = { ...owner, claimToken, claimIssuerId: CLAIM_ISSUER_ID };
try {
// The umask narrows a created mode, so the owner-only mode is set exactly.
fs.chmodSync(lockDirPath, 0o700);
writeProcessLockOwner(ownerFilePath, claim);
} catch (error) {
try {
Expand Down Expand Up @@ -556,6 +558,8 @@ function writeProcessLockOwner(ownerFilePath: string, owner: ProcessLockOwnerRec
publishFileSync({
destination: ownerFilePath,
contents: JSON.stringify(owner),
mode: 0o600,
exactMode: true,
});
}

Expand Down Expand Up @@ -618,6 +622,8 @@ function clearLockDirectory(
publishFileSync({
destination: ownerFilePath,
contents: JSON.stringify(owner),
mode: 0o600,
exactMode: true,
publish: 'link-exclusive',
});
} catch {}
Expand Down
Loading
Loading