dotnet/wpf · error · XpsSerializationException

SR.ReachSerialization_CannotReleaseXmlWriter

Error message

SR.ReachSerialization_CannotReleaseXmlWriter

What it means

XpsSerializationException thrown by NullPackagingPolicy.ReleaseXmlWriterForFixedDocumentSequence when internal state does not match an active fixed-document-sequence writer. The policy tracks current writer reference counts/IDs; releasing a writer outside a valid begin/release sequence leaves no matching writer to release, indicating a lifecycle invariant violation.

Solutions

  1. Ensure every ReleaseXmlWriterForFixedDocumentSequence call is paired with a prior acquire/start of a fixed-document-sequence writer, with no double release.
  2. Verify release ordering: sequence writers are released only after their contained documents/pages are finished.
  3. Wrap the serialization pipeline so exceptions do not cause a release to run without its matching acquire.
  4. Check any custom NullPackagingPolicy/BasePackagingPolicy overrides for unbalanced writer reference bookkeeping (_currentFixedDocumentSequenceWriterRef).

Example fix

// before
policy.ReleaseXmlWriterForFixedDocumentSequence(); // no sequence writer started
// after
if (sequenceWriterAcquired)
{
    policy.ReleaseXmlWriterForFixedDocumentSequence();
    sequenceWriterAcquired = false;
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (!fixedDocumentSequenceWriterActive)
    throw new InvalidOperationException("Cannot release FixedDocumentSequence writer: none acquired");

Type guard

bool CanReleaseSequenceWriter(NullPackagingPolicy p) => p.CurrentFixedDocumentSequenceWriter != null;

Try / catch

try { policy.ReleaseXmlWriterForFixedDocumentSequence(); }
catch (XpsSerializationException ex) when (ex.Message.Contains("CannotReleaseXmlWriter"))
{ /* mark lifecycle state invalid and abort/document the run */ }

Prevention

When it happens

Trigger: Calling ReleaseXmlWriterForFixedDocumentSequence (or the packaging policy API that drives it) when no FixedDocumentSequence writer was previously acquired/started, or after it was already released.

Common situations: Unbalanced acquire/release calls in custom XPS packaging code; exception paths that skip a Release or double-Release; interleaving document/page/sequence releases out of order in custom PackagingPolicy implementations.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/9dc35377c8cb8b14. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/NullPackagingPolicy.cs:118

            {
                _currentDocumentSequenceWriterRef--;

                if(_currentDocumentSequenceWriterRef == 0)
                {
                    //
                    // if any of the other low level writer exist, then
                    // throw an exception or is it better if we just close
                    // them and consider that if any additional call on them
                    // would be the one that throws the expcetion
                    //
                    //_currentFixedDocumentSequenceWriter.Commit();
                    Initialize();
                    InitializeResourceReferences();
                }
            }
            else
            {
                throw new XpsSerializationException(SR.ReachSerialization_CannotReleaseXmlWriter);
            }
        }

        /// <Summary>
        ///
        /// </Summary>
        public
        override
        XmlWriter
        AcquireXmlWriterForFixedDocument(
            )
        {
            XmlWriter xmlWriter = null;

            if(_currentFixedDocumentWriterRef == 0)
            {
                //
                // We need to create the corresponding part in the Xps package

View on GitHub (pinned to 81131a70a4)