{"record":{"id":"f5bce487f622be4f","repo":"stride3d/stride","slug":"sortedlist-internal-error","errorCode":null,"errorMessage":"SortedList::internal error (","messagePattern":"SortedList::internal error \\(","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"sources/core/Stride.Core/Collections/SortedList.cs","lineNumber":548,"sourceCode":"        catch (Exception)\n        {\n            throw new InvalidOperationException();\n        }\n\n        if (freeIndx >= 0)\n        {\n            if (!overwrite)\n                throw new ArgumentException(\"element already exists\");\n\n            table[freeIndx] = new KeyValuePair<TKey, TValue>(key, value);\n            ++modificationCount;\n            return;\n        }\n\n        freeIndx = ~freeIndx;\n\n        if (freeIndx > Capacity + 1)\n            throw new Exception(\"SortedList::internal error (\" + key + \", \" + value + \") at [\" + freeIndx + \"]\");\n\n\n        EnsureCapacity(Count + 1, freeIndx);\n\n        table = this.table;\n        table[freeIndx] = new KeyValuePair<TKey, TValue>(key, value);\n\n        ++inUse;\n        ++modificationCount;\n\n    }\n\n    private void Init(IComparer<TKey> comparer, int capacity, bool forceSize)\n    {\n        if (comparer == null)\n            comparer = Comparer<TKey>.Default;\n        this.comparer = comparer;\n        if (!forceSize && (capacity < defaultCapacity))","sourceCodeStart":530,"sourceCodeEnd":566,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Collections/SortedList.cs#L530-L566","documentation":"PutImpl computes the insertion position as the bitwise complement of the binary-search result; if that position exceeds Capacity + 1, the internal table state is corrupt, so it throws a generic Exception starting with \"SortedList::internal error\". This signals a broken invariant rather than a caller mistake, thrown by the constructor and Add().","triggerScenarios":"Practically only from memory corruption, a mutated internal table (e.g. via unsafe reflection/serialization tricks), or a genuine library bug in EnsureCapacity/binary-search bookkeeping during Add.","commonSituations":"Rare; typically encountered after custom serialization round-trips of the internal fields, concurrent modification without synchronization, or porting/patching the collection code.","solutions":["Treat it as a bug: report it to Stride with a repro (key, value, capacity, Count at failure).","Rebuild the SortedList from source data instead of reusing the corrupted instance.","Guard shared SortedList instances with locks or use concurrent collections if accessed from multiple threads.","Check you are not mutating internal fields (table/keys/values) via reflection or unsafe serializers."],"exampleFix":"// before\nsharedList.Add(key, value); // unsynchronized across threads\n// after\nlock (syncRoot) { sharedList.Add(key, value); }","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { sortedList.Add(key, value); }\ncatch (Exception ex) when (ex.Message.StartsWith(\"SortedList::internal error\"))\n{ sortedList = RebuildFromSource(); // recreate the collection from authoritative data }","preventionTips":["Do not mutate internal fields via reflection or unsafe serializers.","Synchronize all access when the instance is shared across threads.","Report reproducible cases to the Stride project as library bugs.","Prefer Dictionary<TKey,TValue> + explicit sorting if you push exotic usage patterns."],"tags":["collections","internal-error","invariant-violation"],"backgroundTag":"internal-invariant-violation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}