Bug with Open XML SDK Disposing SpreadsheetDocument
- Dominant language
- C#
- Stars
- 4.6k
- Forks
- 605
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the bug**
Reading metadata properties of an Excel file leads to a huge increase of memory while disposing SpreadsheetDocument.
**Screenshots**
Memory issue on Spreadsheetdocument disposal:

**To Reproduce**
Minimal repro:
```csharp
var openSettings = new OpenSettings
{
RelationshipErrorHandlerFactory = package =>
{
return new UriRelationshipErrorHandler();
}
};
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(fs, true, openSettings))
{
//Access main part of document
CustomFilePropertiesPart FileProperties = myDoc.CustomFilePropertiesPart;
if (FileProperties == null)
{
output = MetadataError.ThereAreNoProperties;
return output;
}
// Bug here: Executing this line leads to later issue while disposing 'myDoc'.
DocumentFormat.OpenXml.CustomProperties.Properties props = FileProperties.Properties;
output = MetadataError.Ok;
} // Issue here: this line never returns.
fs.Close(); // Line never reached.
}
```
Steps to reproduce the behavior:
1. Get an Excel file of 100Mb.
2. Add any metadata attribute: 'Example' of type 'Text' with value 'MyExampleValue'.
3. Execute above code.
4. See how memory increases until several Gb and the process hang.
**Observed behavior**
Memory increases until its limit. Dispose line never returns.
**Expected behavior**
Memory should not increase and .Disposing the SpreadsheetDocument should not hang the application.
**Desktop (please complete the following information):**
- OS: [Windows Server 2022 Datacenter Evaluation]
- Office version [N/A]
- .NET Target: reproduced with .NETStandard 2.0 and .NETFramework 4.0
- DocumentFormat.OpenXml Version: Reproduced with 2.12.0 and 2.20.0.
**Additional context**
Bug also reproducible with 3.3.0 version and next snipet:
```csharp
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(fs, true))
{
//Access main part of document
CustomFilePropertiesPart FileProperties = myDoc.CustomFilePropertiesPart;
if (FileProperties == null)
{
output = MetadataError.ThereAreNoProperties;
return output;
}
// Bug here: Executing this line leads to later issue while disposing 'myDoc'.
DocumentFormat.OpenXml.CustomProperties.Properties props = FileProperties.Properties;
} // Issue here: this line never returns.
fs.Close(); // Line never reached.
}
```
The root issue seems to be when opening the document with `isEditable = true`.
Contributor guide
Assessment
This issue has not been assessed yet.