{"record":{"id":"1877dc32a71e906d","repo":"dotnet/wpf","slug":"sr-parsermultibamls","errorCode":null,"errorMessage":"SR.ParserMultiBamls","messagePattern":"SR\\.ParserMultiBamls","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlRecords.cs","lineNumber":769,"sourceCode":"        //  ReleaseWriteRecord\n        //\n        //  Frees a record originally claimed with GetWriteRecord. Note that records in the\n        //  read cache are implicitly recycled, and records in the write cache are explicitly\n        //  recycled (i.e., there's a ReleaseWriteRecord, but no ReleaseReadRecord).\n        //\n        //+---------------------------------------------------------------------------------------------\n\n        internal void ReleaseWriteRecord(BamlRecord record)\n        {\n            // Put the write record back into the cache, if we're allowed to recycle it.\n\n            if( !record.IsPinned )\n            {\n                Debug.Assert(null == _writeCache[(int)record.RecordType]);\n                if (null != _writeCache[(int)record.RecordType])\n                {\n                    // This is really an internal error.\n                    throw new InvalidOperationException(SR.ParserMultiBamls);\n                }\n                _writeCache[(int)record.RecordType] = record;\n            }\n        }\n\n\n        // Cache of BamlRecords, used during read, to avoid lots of records from being\n        // created.  If a record gets pinned (BamlRecord.IsPinned gets set), it is not re-used.\n\n#if !PBTCOMPILER\n        private BamlRecord[] _readCache = new BamlRecord[(int)BamlRecordType.LastRecordType];\n#endif\n\n        // Cache of BamlRecords, used during write, also to avoid lots of records\n        // from being created.\n\n        private BamlRecord[] _writeCache = null; //new BamlRecord[(int)BamlRecordType.LastRecordType];\n    }","sourceCodeStart":751,"sourceCodeEnd":787,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlRecords.cs#L751-L787","documentation":"BamlRecords' SetWriteCache (used by GetPropertyStartRecordType record management) throws InvalidOperationException(SR.ParserMultiBamls) when a BamlRecord that is not pinned is written to a cache slot that already holds a record — the write-cache assumes each record type appears once. The Debug.Assert shows this is an internal invariant; the throw is the release-mode fallback.","triggerScenarios":"BAML writer/record-manager encounters a second non-pinned record of the same RecordType while the cache slot is still occupied — during multi-BAML assembly/serialization bookkeeping.","commonSituations":"Writing multiple BAML documents concurrently or sequentially through shared record caches, framework-internal record-manager misuse, or custom tooling that reuses BamlRecordManager across documents without clearing it.","solutions":["Use a fresh BamlRecordManager/writer per document; do not share record caches across BAML streams","Ensure each record is Pinned before reuse across multiple writes","This is framework-internal — if hit via public APIs, file a bug with a minimal XAML-to-BAML repro","Check for reentrancy: nested/concurrent serialization using the same writer"],"exampleFix":"// before: shared record manager across documents\nvar rm = new BamlRecordManager(); WriteBaml(doc1, rm); WriteBaml(doc2, rm);\n// after: one manager per document\nWriteBaml(doc1, new BamlRecordManager()); WriteBaml(doc2, new BamlRecordManager());","handlingStrategy":"validation","validationCode":"// never share a BamlRecordManager/writer across documents\nif (recordManager.InUse) throw new InvalidOperationException(\"Record manager already in use for another BAML\");","typeGuard":null,"tryCatchPattern":"try { WriteBaml(doc, recordManager); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Baml\")) {\n    recordManager = new BamlRecordManager(); // recreate and retry once\n}","preventionTips":["One record manager per BAML document","No concurrent serialization on shared caches","Pin records before cross-document reuse"],"tags":["wpf","baml","internal-invariant","writer"],"backgroundTag":"internal-invariant-violation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}