{"record":{"id":"c8cdd00f7d590bbf","repo":"microsoft/FASTER","slug":"unsupported-number-of-simultaneous-instances","errorCode":null,"errorMessage":"Unsupported number of simultaneous instances","messagePattern":"Unsupported number of simultaneous instances","errorType":"exception","errorClass":"FasterException","httpStatus":null,"severity":"error","filePath":"cs/src/core/Epochs/FastThreadLocal.cs","lineNumber":41,"sourceCode":"        private readonly int offset;\n        private readonly int iid;\n\n        private static readonly int[] instances = new int[kMaxInstances];\n        private static int instanceId = 0;\n\n        public FastThreadLocal()\n        {\n            iid = Interlocked.Increment(ref instanceId);\n\n            for (int i = 0; i < kMaxInstances; i++)\n            {\n                if (0 == Interlocked.CompareExchange(ref instances[i], iid, 0))\n                {\n                    offset = i;\n                    return;\n                }\n            }\n            throw new FasterException(\"Unsupported number of simultaneous instances\");\n        }\n\n        public void InitializeThread()\n        {\n            if (tl_values == null)\n            {\n                tl_values = new T[kMaxInstances];\n                tl_iid = new int[kMaxInstances];\n            }\n            if (tl_iid[offset] != iid)\n            {\n                tl_iid[offset] = iid;\n                tl_values[offset] = default(T);\n            }\n        }\n\n        public void DisposeThread()\n        {","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/microsoft/FASTER/blob/321d872eabda6a0345c8bd76419f89723ed864ae/cs/src/core/Epochs/FastThreadLocal.cs#L23-L59","documentation":"FastThreadLocal allocates a fixed-size slot array (shared across all FASTER instances in the process) and assigns each new instance a unique instance id via Interlocked CAS. When all slots are taken, the instance limit has been exceeded and the library throws FasterException. This guards a compile-time/process-wide maximum number of simultaneous FasterKV/FasterLog instances backed by this fast TLB mechanism.","triggerScenarios":"Constructing more FasterKV/FasterLog (or other epoch-backed) instances concurrently than the fixed slot count allows, without disposing/releasing earlier instances.","commonSituations":"Service code creating a FASTER store per request or per tenant without lifecycle management; leaking store instances in tests or DI containers; long-running processes that churn many stores.","solutions":["Dispose each FasterKV/FasterLog instance when no longer needed so slots can be reused.","Pool or singleton-cache store instances instead of creating one per operation/unit.","Split the workload across multiple processes if the fixed instance count is genuinely required.","Audit for leaked instances (finalizer warnings, untracked instances) with a memory profiler."],"exampleFix":"// before\nforeach (var tenant in tenants) stores[tenant.Id] = new FasterKV<long, byte[]>(settings); // unbounded\n// after\nusing var store = storePool.GetOrCreate(tenant.Id); // pool bounded and instances disposed on eviction","handlingStrategy":"try-catch","validationCode":"if (activeStoreCount >= FastThreadLocal.MaxInstances)\n    throw new InvalidOperationException(\"Too many live FASTER instances; dispose one first\");","typeGuard":null,"tryCatchPattern":"try\n{\n    var store = new FasterKV<long, byte[]>(settings);\n    try { /* use */ } finally { store.Dispose(); }\n}\ncatch (FasterException ex) when (ex.Message.Contains(\"Unsupported number of simultaneous instances\"))\n{\n    logger.LogError(ex, \"FASTER instance limit reached; pooling required\");\n    throw;\n}","preventionTips":["Dispose FasterKV/FasterLog instances deterministically (using/finally).","Use a bounded pool or DI singleton per store instead of per-request instances.","Monitor live instance counts in long-running services."],"tags":["resources","thread-local","instance-limit"],"backgroundTag":"value-out-of-range","analyzedSha":"321d872eabda6a0345c8bd76419f89723ed864ae","analyzedAt":"2026-09-15T22:18:00.693Z","contentChangedAt":"2026-09-15T22:18:00.693Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}