Closed OskiKervinen-MF closed 2 months ago
@jbevain Would it be better for me to report a separate Cecil issue about this, or is it enough that I link to the Coverlet issue this bug in Cecil causes?
The whole point of Dispose is to release unmanaged resources, so I would think this Pull Request makes sense without too much bookkeeping?
Thank you for the PR!
The Problem
Accessing a PDB file edited with Cecil randomly produces errors about the file being locked. Problem observed when being used in coverlet: https://github.com/coverlet-coverage/coverlet/issues/1471
This is caused by an exclusive file handle held by the SymWriter COM object. It is not released before the COM object is released. In normal execution,
NativePdbWriter.Write
callsSymWriter.Close
at the end, which in turn callsMarshal.ReleaseComObject
, releasing the object and file handle. But if an exception causesNativePdbWriter.Write
to never be called, there is no call toSymWriter.Close
, which in turn meansMarshal.ReleaseComObject
is left uncalled.The garbage collector will eventually destroy the object and thereby release the COM object, but that happens non-deterministically. The result was random file access issues with the PDB file.
The Solution
Luckily
NativePdbWriter
isIDisposable
. I added a call towriter.Close
to theDispose
. But nowClose
could be called twice, so I had to add a boolean to SymWriter to avoid releasing the resources twice.