MCPcopy Create free account
hub / github.com/Noumena-Network/code / lockCurrentVersion

Function lockCurrentVersion

src/utils/nativeInstaller/installer.ts:1036–1144  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1034 * (unlike mtime-based locking which requires a 30-day timeout)
1035 */
1036export async function lockCurrentVersion(): Promise<void> {
1037 const dirs = getBaseDirectories()
1038
1039 // Only lock if we're running from the versions directory
1040 if (!process.execPath.includes(dirs.versions)) {
1041 return
1042 }
1043
1044 const versionPath = resolve(process.execPath)
1045 try {
1046 const lockfilePath = getLockFilePathFromVersionPath(dirs, versionPath)
1047
1048 // Ensure locks directory exists
1049 await mkdir(dirs.locks, { recursive: true })
1050
1051 if (isPidBasedLockingEnabled()) {
1052 // Acquire PID-based lock and hold it for the process lifetime
1053 // PID-based locking allows immediate detection of crashed processes
1054 // while still surviving laptop sleep (process is suspended but PID exists)
1055 const acquired = await acquireProcessLifetimeLock(
1056 versionPath,
1057 lockfilePath,
1058 )
1059
1060 if (!acquired) {
1061 logEvent('ncode_version_lock_failed', {
1062 is_pid_based: true,
1063 is_lifetime_lock: true,
1064 })
1065 logLockAcquisitionError(
1066 versionPath,
1067 new Error('Lock already held by another process'),
1068 )
1069 return
1070 }
1071
1072 logEvent('ncode_version_lock_acquired', {
1073 is_pid_based: true,
1074 is_lifetime_lock: true,
1075 })
1076 logForDebugging(`Acquired PID lock on running version: ${versionPath}`)
1077 } else {
1078 // Acquire mtime-based lock and never release it (until process exits)
1079 // Use 30 days for stale to prevent the lock from being considered stale during
1080 // normal usage. This is critical because laptop sleep suspends the process,
1081 // stopping the mtime heartbeat. 30 days is long enough for any realistic session
1082 // while still allowing eventual cleanup of abandoned locks.
1083 let release: (() => Promise<void>) | undefined
1084 try {
1085 release = await lockfile.lock(versionPath, {
1086 stale: LOCK_STALE_MS,
1087 retries: 0, // Don't retry - if we can't lock, that's fine
1088 lockfilePath,
1089 // Handle lock compromise gracefully (e.g., if another process deletes the lock directory)
1090 onCompromised: (err: Error) => {
1091 logForDebugging(
1092 `NON-FATAL: Lock on running version was compromised: ${err.message}`,
1093 { level: 'info' },

Callers 1

setupFunction · 0.85

Calls 13

getBaseDirectoriesFunction · 0.85
mkdirFunction · 0.85
isPidBasedLockingEnabledFunction · 0.85
logLockAcquisitionErrorFunction · 0.85
registerCleanupFunction · 0.85
isENOENTFunction · 0.85
resolveFunction · 0.50
logEventFunction · 0.50
logForDebuggingFunction · 0.50
releaseFunction · 0.50

Tested by

no test coverage detected