refactor(skills): simplify checkpoint locking
This commit is contained in:
@@ -38,7 +38,6 @@ const nodeIo = Object.freeze({
|
||||
open: (...args) => fs.open(...args),
|
||||
readFile: (...args) => fs.readFile(...args),
|
||||
rename: (...args) => fs.rename(...args),
|
||||
stat: (...args) => fs.stat(...args),
|
||||
unlink: (...args) => fs.unlink(...args),
|
||||
randomUUID: () => randomUUID(),
|
||||
});
|
||||
@@ -173,10 +172,6 @@ async function unlinkQuietly(io, path) {
|
||||
}
|
||||
}
|
||||
|
||||
function sameFile(left, right) {
|
||||
return left.dev === right.dev && left.ino === right.ino;
|
||||
}
|
||||
|
||||
async function readExisting(io, target) {
|
||||
try {
|
||||
return { exists: true, bytes: await io.readFile(target) };
|
||||
@@ -265,7 +260,6 @@ export async function checkpointRecord(request) {
|
||||
}
|
||||
|
||||
let lockHandle = null;
|
||||
let lockIdentity = null;
|
||||
let tempHandle = null;
|
||||
let tempPath = null;
|
||||
let primaryError;
|
||||
@@ -273,14 +267,13 @@ export async function checkpointRecord(request) {
|
||||
try {
|
||||
try {
|
||||
lockHandle = await io.open(lock, "wx", FILE_MODE);
|
||||
lockIdentity = await lockHandle.stat();
|
||||
} catch (error) {
|
||||
if (isAlreadyExists(error)) {
|
||||
throw new CheckpointError("LOCK_EXISTS", "checkpoint target is locked; refusing to recover or overwrite the lock", {
|
||||
cause: error,
|
||||
});
|
||||
}
|
||||
throw new CheckpointError("LOCK_FAILED", `could not acquire and identify checkpoint lock: ${messageFor(error)}`, {
|
||||
throw new CheckpointError("LOCK_FAILED", `could not acquire checkpoint lock: ${messageFor(error)}`, {
|
||||
cause: error,
|
||||
});
|
||||
}
|
||||
@@ -376,24 +369,12 @@ export async function checkpointRecord(request) {
|
||||
cause: lockCloseError,
|
||||
});
|
||||
}
|
||||
if (lockIdentity !== null) {
|
||||
let currentLockIdentity;
|
||||
try {
|
||||
currentLockIdentity = await io.stat(lock);
|
||||
} catch (error) {
|
||||
if (!isMissing(error) && primaryError === undefined) {
|
||||
primaryError = new CheckpointError("LOCK_CLEANUP_FAILED", `could not verify checkpoint lock ownership: ${messageFor(error)}`, {
|
||||
cause: error,
|
||||
});
|
||||
}
|
||||
}
|
||||
if (currentLockIdentity !== undefined && sameFile(lockIdentity, currentLockIdentity)) {
|
||||
const lockUnlinkError = await unlinkQuietly(io, lock);
|
||||
if (lockUnlinkError !== undefined && !isMissing(lockUnlinkError) && primaryError === undefined) {
|
||||
primaryError = new CheckpointError("LOCK_CLEANUP_FAILED", `could not remove checkpoint lock: ${messageFor(lockUnlinkError)}`, {
|
||||
cause: lockUnlinkError,
|
||||
});
|
||||
}
|
||||
if (lockHandle !== null) {
|
||||
const lockUnlinkError = await unlinkQuietly(io, lock);
|
||||
if (lockUnlinkError !== undefined && !isMissing(lockUnlinkError) && primaryError === undefined) {
|
||||
primaryError = new CheckpointError("LOCK_CLEANUP_FAILED", `could not remove checkpoint lock: ${messageFor(lockUnlinkError)}`, {
|
||||
cause: lockUnlinkError,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user