MCPcopy Create free account
hub / github.com/EvoMap/evolver / acquireStateFileLock

Function acquireStateFileLock

src/proxy/mailbox/state.js:51–82  ·  view source on GitHub ↗
(stateFile)

Source from the content-addressed store, hash-verified

49}
50
51function acquireStateFileLock(stateFile) {
52 ensurePrivateDir(path.dirname(stateFile));
53 const lockDir = `${stateFile}.lock`;
54 const startedAt = Date.now();
55 while (true) {
56 try {
57 fs.mkdirSync(lockDir, { mode: PRIVATE_DIR_MODE });
58 bestEffortChmod(lockDir, PRIVATE_DIR_MODE);
59 return function releaseStateFileLock() {
60 try { fs.rmdirSync(lockDir); } catch {}
61 };
62 } catch (e) {
63 if (!e || e.code !== 'EEXIST') throw e;
64 try {
65 const ageMs = Date.now() - fs.statSync(lockDir).mtimeMs;
66 if (ageMs > STATE_LOCK_STALE_MS) {
67 fs.rmSync(lockDir, { recursive: true, force: true });
68 continue;
69 }
70 } catch (statErr) {
71 if (statErr && statErr.code === 'ENOENT') continue;
72 throw statErr;
73 }
74 if (Date.now() - startedAt > STATE_LOCK_STALE_MS) {
75 const err = new Error('timed out waiting for mailbox state lock');
76 err.code = 'MAILBOX_STATE_LOCK_TIMEOUT';
77 throw err;
78 }
79 sleepSync(STATE_LOCK_WAIT_MS);
80 }
81 }
82}
83
84function isPlainState(value) {
85 return value && typeof value === 'object' && !Array.isArray(value);

Callers 1

Calls 3

ensurePrivateDirFunction · 0.85
bestEffortChmodFunction · 0.85
sleepSyncFunction · 0.85

Tested by

no test coverage detected