Skip to content

Remove Socket finalizer and ensure SafeSocketHandle finalization works correctly #43383

Closed

Description

@geoffkizer

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

Activity

  1. added this to the 6.0.0 milestone on Oct 14, 2020
  2. tmds commented on Oct 29, 2020

    @tmds
    Member

    It would be nice if we can get rid of the finalizer on Socket. It is not causing any issues afaik.

  3. added
    enhancementProduct code improvement that does NOT require public API changes/additions
    on May 13, 2021
  4. modified the milestones: 6.0.0, Future on May 13, 2021
  5. dotnet-policy-service commented on Nov 12, 2025

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  6. dotnet-policy-service commented on Nov 26, 2025

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

  7. removed this from the Future milestone on Nov 26, 2025
  8. locked and limited conversation to collaborators on Dec 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Net.SocketsenhancementProduct code improvement that does NOT require public API changes/additions

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions