(stateFile)
| 49 | } |
| 50 | |
| 51 | function 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 | |
| 84 | function isPlainState(value) { |
| 85 | return value && typeof value === 'object' && !Array.isArray(value); |
no test coverage detected