{"record":{"id":"50d70f334461e2e9","repo":"abhigyanpatwari/GitNexus","slug":"gitnexus-failed-to-release-init-lock-code","errorCode":null,"errorMessage":"GitNexus: failed to release init lock (${code ?? 'UNKNOWN'}): ${summarizeError(err)}","messagePattern":"GitNexus: failed to release init lock \\((.+?)\\): (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"gitnexus/src/core/lbug/lbug-adapter.ts","lineNumber":420,"sourceCode":"  await fs.mkdir(path.dirname(lockPath), { recursive: true });\n\n  for (let attempt = 1; attempt <= INIT_LOCK_MAX_ATTEMPTS; attempt++) {\n    try {\n      const handle = await fs.open(\n        lockPath,\n        fsConstants.O_CREAT | fsConstants.O_EXCL | fsConstants.O_WRONLY,\n      );\n      await handle.writeFile(payload);\n      await handle.close();\n\n      // Return the idempotent release function\n      return async () => {\n        try {\n          await fs.unlink(lockPath);\n        } catch (err) {\n          if (!isMissingFileError(err)) {\n            const code = extractErrnoCode(err);\n            logger.warn(\n              `GitNexus: failed to release init lock (${code ?? 'UNKNOWN'}): ${summarizeError(err)}`,\n            );\n          }\n        }\n      };\n    } catch (err) {\n      if ((err as NodeJS.ErrnoException)?.code !== 'EEXIST') {\n        throw err; // Unexpected error — propagate immediately\n      }\n\n      // Lock file exists — check if it's stale\n      const broken = await tryBreakStaleLock(lockPath);\n      if (broken && attempt < INIT_LOCK_MAX_ATTEMPTS) {\n        continue; // Stale lock removed — retry immediately\n      }\n\n      if (attempt === INIT_LOCK_MAX_ATTEMPTS) {\n        throw new Error(","sourceCodeStart":402,"sourceCodeEnd":438,"githubUrl":"https://github.com/abhigyanpatwari/GitNexus/blob/aac7515d2a8c50a1f8f923c6fb77218b333560d6/gitnexus/src/core/lbug/lbug-adapter.ts#L402-L438","documentation":"Emitted by the idempotent release function returned from the init-lock acquire: `fs.unlink(lockPath)` inside the release callback failed with something other than ENOENT. The error is swallowed after logging because release must never throw; a surviving lock file is handled later by the stale-lock breaker (PID-gone / INIT_LOCK_STALE_MS heuristics) on the next acquire.","triggerScenarios":"initLbug completes and the returned release() is invoked, but unlink hits EPERM/EACCES (file owned by another user), EBUSY (Windows/antivirus handle), or the lock was already replaced by a different process's lock file. The warn records the residue; the next open may hit EEXIST and go through the stale-lock path.","commonSituations":"Multi-process overlap (serve + analyze finishing near-simultaneously); Windows Defender or indexers holding a handle on the lock; mixed-UID container volumes; read-only or quota-exhausted filesystems.","solutions":["Check permissions on the database directory and its lock file; make sure the running user can unlink files there.","If warns recur, manually remove the leftover lock file while nothing else runs.","Serialize gitnexus invocations against one DB (one analyze or one serve at a time) to shrink release/acquire races.","Add an antivirus exclusion for the .gitnexus directory on Windows."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before releasing: only unlink a lock we still own (idempotent guard)\nconst release = await acquireInitLock(dbPath);\n// ... work ...\nawait release(); // already swallows; to verify cleanliness afterwards:\nimport { access } from 'node:fs/promises';\nconst leftover = await access(lockPath).then(() => true, () => false);\nif (leftover && noOtherGitnexusRunning()) await unlink(lockPath);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Serialize initLbug callers per DB path so release/acquire windows never overlap.","Ensure write+unlink permission on the DB directory for the service user.","Add antivirus exclusions for lock/sidecar files on Windows to avoid EBUSY on unlink."],"tags":["file-lock","cleanup","permissions","ladybugdb","windows"],"backgroundTag":"lock-file-release-failed","analyzedSha":"aac7515d2a8c50a1f8f923c6fb77218b333560d6","analyzedAt":"2026-08-20T23:29:22.980Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}