{"record":{"id":"4bcf5aa70b720138","repo":"litedb-org/LiteDB","slug":"0-4bcf5a","errorCode":"0","errorMessage":"MemoryCache: removed in-use memory page. This situation has no way to fix (yet). Throwing exception to avoid database corruption. No other thread can read/write from database now.","messagePattern":"MemoryCache: removed in-use memory page\\. This situation has no way to fix \\(yet\\)\\. Throwing exception to avoid database corruption\\. No other thread can read/write from database now\\.","errorType":"exception","errorClass":"LiteException","httpStatus":null,"severity":"critical","filePath":"LiteDB/Engine/Disk/MemoryCache.cs","lineNumber":331,"sourceCode":"                    .Select(x => x.Key)\n                    .Take(segmentSize)\n                    .ToArray();\n\n                // move pages from readable list to free list\n                foreach (var key in readables)\n                {\n                    var removed = _readable.TryRemove(key, out var page);\n\n                    ENSURE(removed, \"page should be in readable list before moving to free list\");\n\n                    // if removed page was changed between make array and now, must add back to readable list\n                    if (page.ShareCounter > 0)\n                    {\n                        // but wait: between last \"remove\" and now, another thread can added this page\n                        if (!_readable.TryAdd(key, page))\n                        {\n                            // this is a terrible situation, to avoid memory corruption I will throw expcetion for now\n                            throw new LiteException(0, \"MemoryCache: removed in-use memory page. This situation has no way to fix (yet). Throwing exception to avoid database corruption. No other thread can read/write from database now.\");\n                        }\n                    }\n                    else\n                    {\n                        ENSURE(page.ShareCounter == 0, \"page should not be in use by anyone\");\n\n                        // clean controls\n                        page.Position = long.MaxValue;\n                        page.Origin = FileOrigin.None;\n\n                        _free.Enqueue(page);\n                    }\n                }\n\n                LOG($\"re-using cache pages (flushing {_free.Count} pages)\", \"CACHE\");\n            }\n            else\n            {","sourceCodeStart":313,"sourceCodeEnd":349,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Engine/Disk/MemoryCache.cs#L313-L349","documentation":"A critical internal engine guard in MemoryCache. During page eviction, a page that was marked for removal was found to still have a non-zero ShareCounter (in-use by a reader/writer), AND re-adding it to the readable set also failed (another thread already inserted a different page at that key). To prevent silent memory corruption, the engine throws this LiteException (code 0) and considers the database unsafe for further I/O.","triggerScenarios":"High-concurrency workloads with many simultaneous readers/writers that pressure the memory cache into aggressive eviction. A race condition between page share-count updates and the eviction loop. This is an engine-level concurrency edge case, not user-logic error.","commonSituations":"Heavy parallel read/write load exceeding the configured cache size. Bugs or version regressions in the engine's page-sharing logic. Running on hardware/OS combinations with unusual threading scheduling. Extremely large datasets with limited RAM.","solutions":["Increase the memory cache / page limit via connection string pragma (e.g., increase the size budget) to reduce eviction pressure.","Reduce concurrency or batch writes to lower simultaneous page contention.","Report this as an engine bug with reproduction details; it indicates an unresolved race in MemoryCache.","Restart the application to get a clean cache state; run a checkpoint/recovery to verify database integrity."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { /* engine operation under high concurrency */ }\ncatch (LiteException ex) when (ex.Message.Contains(\"removed in-use memory page\"))\n{ /* database is unsafe; stop all I/O, restart process, verify integrity */ }","preventionTips":["Increase the memory cache size to reduce eviction pressure under heavy load.","Limit write concurrency to reduce page-share contention.","Keep LiteDB updated; report reproducible cases as engine bugs.","Implement graceful shutdown/restart handling for this critical condition."],"tags":["engine","memory-cache","concurrency","corruption-guard","internal","lite-exception"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}