Description
Describe the bug
VacuumAsync(VacuumMode.Full) crashes with an unhandled ObjectDisposedException when the application is published with .NET 10 trimming enabled (PublishTrimmed=true).
To Reproduce
Steps to reproduce the behavior:
- Create a project referencing
SharpCoreDBv1.9.7. - Configure the project for AOT/Trimming:
<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed> - Publish and run:
dotnet publish -c Release -r win-x64 --self-contained true - Execute:
await db.VacuumAsync(VacuumMode.Full, CancellationToken.None); - See error:
[FATAL] ObjectDisposedException: Cannot access a closed file. at System.IO.FileStream.get_Length() at SharpCoreDB.Storage.SingleFileStorageProvider.VacuumAsync(...) at SingleFileStorageProvider.cs:line 1258
Expected behavior
The vacuum operation should complete successfully, rewriting the file compactly and swapping the underlying file stream without crashing.
Root Cause Analysis
VacuumFullAsync closes the current _fileStream and attempts to swap in a newly opened stream using Reflection:
_fileStream.Close();
// ... file moves ...
var newFileStream = new FileStream(...);
var fsField = typeof(SingleFileStorageProvider).GetField("_fileStream",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
fsField.SetValue(this, newFileStream);Under .NET 10 Trimming / Native AOT, metadata for private fields is aggressively trimmed. GetField("_fileStream", ...) returns null, causing fsField.SetValue(...) to throw a NullReferenceException.
This triggers a cascading failure:
- The
NullReferenceExceptionis caught by thecatchblock inVacuumFullAsync. - The catch block attempts to read
_fileStream.Lengthto build theVacuumResult. - Because the old stream was closed just prior to the reflection call, this throws
ObjectDisposedException. - This exception propagates to the
catchblock inVacuumAsync, which also attempts to read_fileStream.Length(line 1258), throwing theObjectDisposedExceptionthat ultimately crashes the application.
Desktop (please complete the following information):
- OS: Windows NT 10.0.26200.0
- Runtime: .NET 10.0.11
- Library Version: v1.9.7
Additional context
Proposed fixes:
- Avoid Reflection: The reliance on
GetFieldfor internal state mutation is fundamentally incompatible with AOT/Trimming. Consider exposing an internalReplaceFileStream(FileStream newStream)method, or refactoringVacuumFullto operate without closing and swapping the underlying stream mid-operation. - Safe Error Handling: The
catchblocks in bothVacuumAsyncandVacuumFullAsyncshould not blindly access_fileStream.Lengthif the stream may have been closed. Use a safe fallback (e.g.,FileSizeAfter = -1or0) in the error path.
Activity
fix(scdb): resolve VacuumAsync(Full) crash under .NET trimming/AOT (i…
fix(scdb): make single-file row cache JSON serialization AOT-safe (is…
fix(scdb): replace reflection+dynamic batch flushing with internal ac…
release: v1.9.8 - Native AOT-safe VacuumAsync(Full) + single-file mod…
fix(scdb): resolve VacuumAsync(Full) crash under .NET trimming/AOT (i…
aot: exercise single-file full VACUUM in the AotSmoke smoke test (issue
fix(scdb): make single-file row cache JSON serialization AOT-safe (is…
fix(scdb): replace reflection+dynamic batch flushing with internal ac…
aot: run single-file full VACUUM unconditionally in the AotSmoke (issue
docs(v2): record the Issue #343 Native AOT fix set in the unreleased …
docs: credit saltus7 for issue #343 root-cause analysis in CONTRIBUTORS
MPCoreDeveloper commentedon Aug 29, 2026
Thanks for the detailed report — this is exactly the kind of root-cause analysis that makes a fix fast. ✔️
Fixed in v1.9.8 (and carried into the upcoming v2.0)
Your diagnosis was accurate on all counts. The fix follows your
ReplaceFileStreamproposal:VacuumFullAsyncnow swaps_fileStream/_memoryMappedFilevia direct assignment in an internalSwapFileStreamhelper (the fields are no longerreadonly). No moreGetField(...)/SetValue(...).catchblocks in bothVacuumAsyncandVacuumFullAsyncnow read the file size throughGetFileSizeSafely()with a-1fallback instead of touching a possibly-closed stream, so the cascade you described (ObjectDisposedException) can no longer happen.<file>.vacuum.tmp.scdb(the provider appends the.scdbextension) but later moved as<file>.vacuum.tmp→FileNotFoundException. The temp-file name is now consistent.JsonSerializerContext+ polymorphic converter, byte-identical output so existing.scdbfiles stay readable) and the batch-flushGetField(...)+dynamiccalls (now internalTableDirectoryManager/BlockRegistryaccessors).Verified by publishing
SharpCoreDB.AotSmokewithPublishAot=true— it runs exit 0 including the single-file full-VACUUM path. Regression test:SingleFileDatabase_VacuumFull_Works_And_SurvivesReopen(100 rows, full vacuum, reopen + read-back).About the v2.0 release
We will publish a v2.0 preview shortly after this — the performance-first release (point reads beat SQLite on the default engine, batch INSERTs beat SQLite on the PageBased engine, every CRUD operation beats LiteDB), and it carries the same AOT fix set, so
VacuumAsync(Full)+ single-file mode will be AOT-safe there too.Full credit added to
CONTRIBUTORS.md— thanks again! 🙏saltus7 commentedon Aug 29, 2026
@MPCoreDeveloper Verified the fix in v1.9.8 under .NET 10 trimming.
VacuumMode.Fullnow completes successfully with noObjectDisposedException, and data integrity is perfectly maintained. I also noted the v2.0 roadmap for block-registry encryption to address the metadata leakage — excellent long-term planning. Thanks for the rapid turnaround!release: promote the .NET 11 GA line (2.1.3) to master (#503)