Skip to content

fix: resolve COM memory instability and ensure graceful Excel teardown#1

Merged
mitchell-tesch merged 8 commits into
mitchell-tesch:mainfrom
eli-lindner-sj:fix/com-memory-stability
Jun 8, 2026
Merged

fix: resolve COM memory instability and ensure graceful Excel teardown#1
mitchell-tesch merged 8 commits into
mitchell-tesch:mainfrom
eli-lindner-sj:fix/com-memory-stability

Conversation

@eli-lindner-sj

@eli-lindner-sj eli-lindner-sj commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

This PR implements a scope-isolated cleanup pattern to ensure reliable Excel lifecycle management:

Key Changes:

  • Scope Isolation: Refactored ExcelWorker.Run() to move core processing into a nested ExecuteRunLoop. This ensures that all local COM references naturally go out of scope and become eligible for collection before the "double-tap" GC collection (GC.Collect() + GC.WaitForPendingFinalizers()) is triggered in the finally block.
  • Removed Manual Release: Eliminated all calls to Marshal.FinalReleaseComObject in favor of the more stable scope-isolated GC pattern, preventing DLR-related interop exceptions.
  • Process Abstraction: Introduced IProcessInterop to abstract OS-level process interactions. This allowed for the replacement of "hollow" tests with deterministic unit tests that verify tracking and fallback-kill logic without requiring a real Excel instance.
  • Resilient PID Tracking: Improved GetExcelProcessId to handle failures gracefully, ensuring that valid PIDs are always tracked for cleanup even if initial resolution is delayed.

eli-lindner-sj and others added 8 commits June 1, 2026 13:53
Removed manual FinalReleaseComObject calls on dynamic COM objects to prevent InvalidComObjectException. Implemented a double-tap GC collection pattern in ExcelProcessTracker to ensure reliable RCW release and graceful Excel shutdown.
Fixed a 64-bit HWND casting bug in ExcelProcessTracker and optimized COM lifecycle management by clearing local dynamic references before triggering the GC double-tap to ensure RCWs are not rooted during collection.
…' into fork/eli-lindner-sj/fix/com-memory-stability
@mitchell-tesch mitchell-tesch marked this pull request as ready for review June 8, 2026 11:12
@mitchell-tesch mitchell-tesch merged commit 2e805f5 into mitchell-tesch:main Jun 8, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants