{"record":{"id":"c49f24bed59e1a30","repo":"microsoft/garnet","slug":"failed-to-acquire-inprogress-lock-at-nameof-posts","errorCode":null,"errorMessage":"Failed to acquire inProgress lock at {nameof(PostSingleKeyConsistentReadCallback)}","messagePattern":"Failed to acquire inProgress lock at (.+?)","errorType":"exception","errorClass":"GarnetException","httpStatus":null,"severity":"error","filePath":"libs/server/AOF/ReadConsistency/ReplicaReadSessionContext.cs","lineNumber":183,"sourceCode":"            try\n            {\n                appendOnlyFile.readConsistencyManager.PreSingleKeyConsistentRead(hash & long.MaxValue, ref replicaReadContext, readTimeout, consistentReadCts.Token);\n            }\n            finally\n            {\n                inProgress.ReadUnlock();\n            }\n        }\n\n        /// <summary>\n        /// Post read update maximum sequence number for a single key.\n        /// </summary>\n        /// <exception cref=\"GarnetException\"></exception>\n        [MethodImpl(MethodImplOptions.AggressiveInlining)]\n        public void PostSingleKeyConsistentReadCallback()\n        {\n            if (!inProgress.TryReadLock())\n                throw new GarnetException($\"Failed to acquire inProgress lock at {nameof(PostSingleKeyConsistentReadCallback)}\");\n            try\n            {\n                appendOnlyFile.readConsistencyManager.PostSingleKeyConsistentRead(ref replicaReadContext);\n            }\n            finally\n            {\n                inProgress.ReadUnlock();\n            }\n        }\n\n        /// <summary>\n        /// Initialize context for read key batch.\n        /// </summary>\n        /// <param name=\"parameters\"></param>\n        public void PreBatchKeyConsistentReadCallback(ReadOnlySpan<PinnedSpanByte> parameters)\n        {\n            if (!inProgress.TryReadLock())\n                throw new GarnetException($\"Failed to acquire inProgress lock at {nameof(PreBatchKeyConsistentReadCallback)}\");","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/microsoft/garnet/blob/951b0fc6838721f89d102c2bbe1b914e8d39d700/libs/server/AOF/ReadConsistency/ReplicaReadSessionContext.cs#L165-L201","documentation":"Thrown by ReplicaReadSessionContext.PostSingleKeyConsistentReadCallback when TryReadLock() on the 'inProgress' SingleWriterMultiReaderLock fails. This is the post-read bookkeeping step (updating the maximum session sequence number); it requires a read lock and fails if a writer (Dispose) holds the lock.","triggerScenarios":"The post-read callback runs after a single-key consistent read, but the session is concurrently being disposed (Dispose acquires the write lock). Since TryReadLock is non-blocking, it throws immediately.","commonSituations":"Session disposal racing with an in-flight read completion; a shutdown path that disposes sessions without draining outstanding reads; re-entrant access during dispose.","solutions":["Drain in-flight reads before disposing the replica read session.","Check a disposed flag before calling PostSingleKeyConsistentReadCallback.","Catch GarnetException at the call site and treat the read as invalidated.","Ensure the read pipeline completes (pre + post) within the session lifetime."],"exampleFix":"// before\nsession.PostSingleKeyConsistentReadCallback(); // throws if disposing\n\n// after\ntry { session.PostSingleKeyConsistentReadCallback(); }\ncatch (GarnetException) { /* session disposed mid-read; treat as stale */ }","handlingStrategy":"try-catch","validationCode":"if (session.IsDisposed) return;\nsession.PostSingleKeyConsistentReadCallback();","typeGuard":null,"tryCatchPattern":"try { session.PostSingleKeyConsistentReadCallback(); }\ncatch (GarnetException ex) when (ex.Message.Contains(\"inProgress lock\"))\n{ /* session disposed mid-read; result is stale, discard safely */ }","preventionTips":["Ensure Dispose waits for outstanding read callbacks to finish.","Check a disposed flag before post-read callbacks.","Treat lock-acquisition failure as 'session closed', not a data error."],"tags":["aof","read-consistency","locking","replica","internal"],"backgroundTag":null,"analyzedSha":"951b0fc6838721f89d102c2bbe1b914e8d39d700","analyzedAt":"2026-08-13T19:01:32.939Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}