MCPcopy
hub / github.com/codeaashu/claude-code / saveConfigWithLock

Function saveConfigWithLock

src/utils/config.ts:1153–1329  ·  view source on GitHub ↗

* Returns true if a write was performed; false if the write was skipped * (no changes, or auth-loss guard tripped). Callers use this to decide * whether to invalidate the cache -- invalidating after a skipped write * destroys the good cached state the auth-loss guard depends on.

(
  file: string,
  createDefault: () => A,
  mergeFn: (current: A) => A,
)

Source from the content-addressed store, hash-verified

1151 * destroys the good cached state the auth-loss guard depends on.
1152 */
1153function saveConfigWithLock<A extends object>(
1154 file: string,
1155 createDefault: () => A,
1156 mergeFn: (current: A) => A,
1157): boolean {
1158 const defaultConfig = createDefault()
1159 const dir = dirname(file)
1160 const fs = getFsImplementation()
1161
1162 // Ensure directory exists (mkdirSync is already recursive in FsOperations)
1163 fs.mkdirSync(dir)
1164
1165 let release
1166 try {
1167 const lockFilePath = `${file}.lock`
1168 const startTime = Date.now()
1169 release = lockfile.lockSync(file, {
1170 lockfilePath: lockFilePath,
1171 onCompromised: err => {
1172 // Default onCompromised throws from a setTimeout callback, which
1173 // becomes an unhandled exception. Log instead -- the lock being
1174 // stolen (e.g. after a 10s event-loop stall) is recoverable.
1175 logForDebugging(`Config lock compromised: ${err}`, { level: 'error' })
1176 },
1177 })
1178 const lockTime = Date.now() - startTime
1179 if (lockTime > 100) {
1180 logForDebugging(
1181 'Lock acquisition took longer than expected - another Claude instance may be running',
1182 )
1183 logEvent('tengu_config_lock_contention', {
1184 lock_time_ms: lockTime,
1185 })
1186 }
1187
1188 // Check for stale write - file changed since we last read it
1189 // Only check for global config file since lastReadFileStats tracks that specific file
1190 if (lastReadFileStats && file === getGlobalClaudeFile()) {
1191 try {
1192 const currentStats = fs.statSync(file)
1193 if (
1194 currentStats.mtimeMs !== lastReadFileStats.mtime ||
1195 currentStats.size !== lastReadFileStats.size
1196 ) {
1197 logEvent('tengu_config_stale_write', {
1198 read_mtime: lastReadFileStats.mtime,
1199 write_mtime: currentStats.mtimeMs,
1200 read_size: lastReadFileStats.size,
1201 write_size: currentStats.size,
1202 })
1203 }
1204 } catch (e) {
1205 const code = getErrnoCode(e)
1206 if (code !== 'ENOENT') {
1207 throw e
1208 }
1209 // File doesn't exist yet, no stale check needed
1210 }

Callers 2

saveGlobalConfigFunction · 0.85
saveCurrentProjectConfigFunction · 0.85

Calls 11

getFsImplementationFunction · 0.85
logForDebuggingFunction · 0.85
logEventFunction · 0.85
getErrnoCodeFunction · 0.85
wouldLoseAuthStateFunction · 0.85
jsonStringifyFunction · 0.85
getConfigBackupDirFunction · 0.85
popMethod · 0.80
getConfigFunction · 0.70
releaseFunction · 0.50

Tested by

no test coverage detected