Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
GitHub or Developer Community with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
An exception can occur when finalizing a __ComObject. It specifically occurs when shutting down the process. The __ComObject finalizer attempts to call ReleaseInterfaces on the thread it was created on using a synchronization context. If the synchronization context is a WinForms synchronization context, it uses a socket to make asynchronous calls. If the socket is finalized prior to the __ComObject, then an ObjectDisposedException or SocketException is thrown when the __ComObject tries to use the synchronization context.
All RCWs/__ComObjects are cleaned up prior to the last finalization is performed on the finalizer thread. The list of RCWs is maintained by caching all ComInteropProxy objects that are currently active. Each __ComObject should have a corresponding ComInteropProxy, which holds a reference to the __ComObject. This reference is used to clean up the __ComObject during runtime shutdown. It seems as though the garbage collector will often collect ComInteropProxy objects that still have RCWs that are in use. I believe this happens because the __ComObject does not reference the ComInteropProxy. Any __ComObjects that have no ComInteropProxy are not cleaned up during shutdown, leaving them to be finalized during the last finalization, which can happen after the synchronization context it references has been finalized.
I believe that this problem can be fixed by having the __ComObject reference its ComInteropProxy and by having the __ComObject check in the finalizer if it has already been cleaned up by checking if the itf_hash field is null.
Suggested fix in https://github.com/mono/mono/pull/2545