Bug 24974 - Exception occurs in __ComObject finalizer when shutting down process
Summary: Exception occurs in __ComObject finalizer when shutting down process
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 3.4.0
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-12-02 01:32 UTC by daspits
Modified: 2016-04-04 20:18 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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.

Related Links:
Status:
RESOLVED FIXED

Description daspits 2014-12-02 01:32:40 UTC
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.
Comment 1 eb1 2016-01-29 21:53:12 UTC
Suggested fix in https://github.com/mono/mono/pull/2545