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

Function saveConfigWithLock

src/utils/config.ts:1161–1337  ·  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

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

Callers 2

saveGlobalConfigFunction · 0.85
saveCurrentProjectConfigFunction · 0.85

Calls 11

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

Tested by

no test coverage detected