Remove Socket finalizer and ensure SafeSocketHandle finalization works correctly #43383
Labels
area-System.Net.Sockets
enhancement
Product code improvement that does NOT require public API changes/additions
Milestone
The Socket class has a finalizer, but it shouldn't need to have one. It should be able to rely on the finalizer of SafeSocketHandle to handle finalization when necessary, now that the logic in SafeSocketHandle is reliable as of 5.0.
Having both of these objects be finalizable is problematic since we can't control the order in which their finalizers are called, and this may lead to unexpected behavior. It's also just unneeded overhead in the GC.
I tested removing the finalizer, and everything works fine on Windows. However, on Linux the SafeSocketHandle is not properly collected. I believe this is because there's a reference to the SafeSocketHandle held (indirectly) by the SocketAsyncEngine dictionary. We probably need to make this a weak reference, or otherwise change the code so that the reference is not held.
@antonfirsov @stephentoub @tmds @wfurt
The text was updated successfully, but these errors were encountered: