mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 16:01:01 +00:00
refactor(agents): distill session lock reclaim policy
This commit is contained in:
@@ -351,13 +351,12 @@ async function readLockPayload(lockPath: string): Promise<LockFilePayload | null
|
|||||||
async function resolveNormalizedSessionFile(sessionFile: string): Promise<string> {
|
async function resolveNormalizedSessionFile(sessionFile: string): Promise<string> {
|
||||||
const resolvedSessionFile = path.resolve(sessionFile);
|
const resolvedSessionFile = path.resolve(sessionFile);
|
||||||
const sessionDir = path.dirname(resolvedSessionFile);
|
const sessionDir = path.dirname(resolvedSessionFile);
|
||||||
let normalizedDir = sessionDir;
|
|
||||||
try {
|
try {
|
||||||
normalizedDir = await fs.realpath(sessionDir);
|
const normalizedDir = await fs.realpath(sessionDir);
|
||||||
|
return path.join(normalizedDir, path.basename(resolvedSessionFile));
|
||||||
} catch {
|
} catch {
|
||||||
// Fall back to the resolved path if realpath fails (permissions, transient FS).
|
return resolvedSessionFile;
|
||||||
}
|
}
|
||||||
return path.join(normalizedDir, path.basename(resolvedSessionFile));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function inspectLockPayload(
|
function inspectLockPayload(
|
||||||
@@ -441,7 +440,7 @@ async function shouldReclaimContendedLockFile(
|
|||||||
function shouldTreatAsOrphanSelfLock(params: {
|
function shouldTreatAsOrphanSelfLock(params: {
|
||||||
payload: LockFilePayload | null;
|
payload: LockFilePayload | null;
|
||||||
normalizedSessionFile: string;
|
normalizedSessionFile: string;
|
||||||
reclaimWithoutStarttime?: boolean;
|
reclaimLockWithoutStarttime: boolean;
|
||||||
}): boolean {
|
}): boolean {
|
||||||
const pid = isValidLockNumber(params.payload?.pid) ? params.payload.pid : null;
|
const pid = isValidLockNumber(params.payload?.pid) ? params.payload.pid : null;
|
||||||
if (pid !== process.pid) {
|
if (pid !== process.pid) {
|
||||||
@@ -455,7 +454,7 @@ function shouldTreatAsOrphanSelfLock(params: {
|
|||||||
? params.payload.starttime
|
? params.payload.starttime
|
||||||
: null;
|
: null;
|
||||||
if (storedStarttime === null) {
|
if (storedStarttime === null) {
|
||||||
return params.reclaimWithoutStarttime !== false;
|
return params.reclaimLockWithoutStarttime;
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentStarttime = getProcessStartTime(process.pid);
|
const currentStarttime = getProcessStartTime(process.pid);
|
||||||
@@ -467,14 +466,14 @@ function inspectLockPayloadForSession(params: {
|
|||||||
staleMs: number;
|
staleMs: number;
|
||||||
nowMs: number;
|
nowMs: number;
|
||||||
normalizedSessionFile: string;
|
normalizedSessionFile: string;
|
||||||
reclaimWithoutStarttime?: boolean;
|
reclaimLockWithoutStarttime: boolean;
|
||||||
}): LockInspectionDetails {
|
}): LockInspectionDetails {
|
||||||
const inspected = inspectLockPayload(params.payload, params.staleMs, params.nowMs);
|
const inspected = inspectLockPayload(params.payload, params.staleMs, params.nowMs);
|
||||||
if (
|
if (
|
||||||
!shouldTreatAsOrphanSelfLock({
|
!shouldTreatAsOrphanSelfLock({
|
||||||
payload: params.payload,
|
payload: params.payload,
|
||||||
normalizedSessionFile: params.normalizedSessionFile,
|
normalizedSessionFile: params.normalizedSessionFile,
|
||||||
reclaimWithoutStarttime: params.reclaimWithoutStarttime,
|
reclaimLockWithoutStarttime: params.reclaimLockWithoutStarttime,
|
||||||
})
|
})
|
||||||
) {
|
) {
|
||||||
return inspected;
|
return inspected;
|
||||||
@@ -530,7 +529,7 @@ export async function cleanStaleLockFiles(params: {
|
|||||||
staleMs,
|
staleMs,
|
||||||
nowMs,
|
nowMs,
|
||||||
normalizedSessionFile,
|
normalizedSessionFile,
|
||||||
reclaimWithoutStarttime: false,
|
reclaimLockWithoutStarttime: false,
|
||||||
});
|
});
|
||||||
const lockInfo: SessionLockInspection = {
|
const lockInfo: SessionLockInspection = {
|
||||||
lockPath,
|
lockPath,
|
||||||
@@ -641,6 +640,7 @@ export async function acquireSessionWriteLock(params: {
|
|||||||
staleMs,
|
staleMs,
|
||||||
nowMs,
|
nowMs,
|
||||||
normalizedSessionFile,
|
normalizedSessionFile,
|
||||||
|
reclaimLockWithoutStarttime: true,
|
||||||
});
|
});
|
||||||
if (await shouldReclaimContendedLockFile(lockPath, inspected, staleMs, nowMs)) {
|
if (await shouldReclaimContendedLockFile(lockPath, inspected, staleMs, nowMs)) {
|
||||||
await fs.rm(lockPath, { force: true });
|
await fs.rm(lockPath, { force: true });
|
||||||
|
|||||||
Reference in New Issue
Block a user