* Attempt to create the lock file atomically with O_EXCL. * Returns true on success, false if the file already exists. * Throws for other errors.
(lock: ComputerUseLock)
| 77 | * Throws for other errors. |
| 78 | */ |
| 79 | async function tryCreateExclusive(lock: ComputerUseLock): Promise<boolean> { |
| 80 | try { |
| 81 | await writeFile(getLockPath(), jsonStringify(lock), { flag: 'wx' }) |
| 82 | return true |
| 83 | } catch (e: unknown) { |
| 84 | if (getErrnoCode(e) === 'EEXIST') return false |
| 85 | throw e |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Register a shutdown cleanup handler so the lock is released even if |
no test coverage detected