forked from ChainSafe/lodestar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ignore stale keystore lockfiles (ChainSafe#6363)
* fix: ignore stale keystore lockfiles * Update error message if lockfile is already acquired * Update keymanager lockfile e2e tests
- Loading branch information
Showing
6 changed files
with
55 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,45 @@ | ||
export type Lockfile = { | ||
lockSync(path: string): void; | ||
unlockSync(path: string): void; | ||
}; | ||
|
||
const lockFile: Lockfile = (await import("lockfile")) as Lockfile; | ||
|
||
function getLockFilepath(filepath: string): string { | ||
return `${filepath}.lock`; | ||
} | ||
|
||
/** | ||
* When lockfile is imported, it registers listeners to process | ||
* Since it's only used by the validator client, require lazily to not pollute | ||
* beacon_node client context | ||
*/ | ||
function getLockFile(): Lockfile { | ||
return lockFile; | ||
} | ||
import {lockSync, unlockSync} from "proper-lockfile"; | ||
|
||
/** | ||
* Creates a .lock file for `filepath`, argument passed must not be the lock path | ||
* @param filepath File to lock, i.e. `keystore_0001.json` | ||
*/ | ||
export function lockFilepath(filepath: string): void { | ||
getLockFile().lockSync(getLockFilepath(filepath)); | ||
try { | ||
lockSync(filepath, { | ||
// Allows to lock files that do not exist | ||
realpath: false, | ||
}); | ||
} catch (e) { | ||
if (isLockfileError(e) && e.code === "ELOCKED") { | ||
e.message = `${filepath} is already in use by another process`; | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
/** | ||
* Deletes a .lock file for `filepath`, argument passed must not be the lock path | ||
* @param filepath File to unlock, i.e. `keystore_0001.json` | ||
*/ | ||
export function unlockFilepath(filepath: string): void { | ||
// Does not throw if the lock file is already deleted | ||
// https://github.com/npm/lockfile/blob/6590779867ee9bdc5dbebddc962640759892bb91/lockfile.js#L68 | ||
getLockFile().unlockSync(getLockFilepath(filepath)); | ||
try { | ||
unlockSync(filepath, { | ||
// Allows to unlock files that do not exist | ||
realpath: false, | ||
}); | ||
} catch (e) { | ||
if (isLockfileError(e) && e.code === "ENOTACQUIRED") { | ||
// Do not throw if the lock file is already deleted | ||
return; | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
// https://github.com/moxystudio/node-proper-lockfile/blob/9f8c303c91998e8404a911dc11c54029812bca69/lib/lockfile.js#L53 | ||
export type LockfileError = Error & {code: "ELOCKED" | "ENOTACQUIRED"}; | ||
|
||
function isLockfileError(e: unknown): e is LockfileError { | ||
return e instanceof Error && (e as LockfileError).code !== undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters