Skip to content

Implement DisconnectAsync method in SocketTaskExtensions #1608

Closed

Description

@gacardinal

I was using the Socket extension methods provided by the SocketTaskExtensions class in System.Net.Sockets and noticed that almost all the *Async methods from Socket that are IO bound operations seemed to have their Task returning counterpart in SocketTaskExtensions except for DisconnectAsync.

Now, I've asked around online and people seemed to have mixed feelings about the issue. Some people didn't seem to understand why one would want to wait for the IO to complete when disconnecting a Socket. It's true that in most cases it's not really useful (and as I understand it, it's probably the reason it's not yet implemented in the framework) but even if use cases are limited, I would make the argument that for the sake of consistency, it'd be better that the SocketTaskExtensions class provides a DisconnectAsync method.

Implementation

My proposition is to add a DisconnectAsync method to the SocketTaskExtensions class.

    public static class SocketTaskExtensions
    {
        // ...
        public static Task DisconnectAsync(this Socket that, bool reuseSocket)
    }

The task is intentionally not returning a bool as its result (like the Socket.DisconnectAsync() is returning a bool to indicate whether the operation completed synchronously or not) because the goal of this extension method is to abstract that away.

Usage

A developer could use the API in the following way

    class Program
    {
        static async Task Main(string[] args)
        {
            Socket dummyReceiver = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            dummyReceiver.Bind(new IPEndPoint(IPAddress.Loopback, 7777));
            dummyReceiver.Listen(1);
            
            await Task.Run(() => {
                dummyReceiver.AcceptAsync();
                byte[] buffer = new byte[1000];
                dummyReceiver.ReceiveAsync(buffer, new SocketFlags());
            });

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            s.Connect(new IPEndPoint(IPAddress.Loopback, 7777));

            await s.SendAsync(new byte[10000], new SocketFlags());
            await s.DisconnectAsync(false);
        }
    }

Additional context

Just to add some additional context, what led to me making this PR is this Stack Overflow question.

I would be happy to contribute an implementation for this feature!

Activity

  1. added
    untriaged
    on Jan 10, 2020
  2. added
    api-suggestion
    and removed
    untriaged
    on Jan 10, 2020
  3. added this to the Future milestone on Jan 14, 2020
  4. added
    untriaged
    on Feb 20, 2020
  5. karelz commented on Feb 20, 2020

    @karelz
    Member

    Triage: We already have BeginDisconnect, so adding DisconnectAsync makes sense. Low pri.

  6. removed
    untriaged
    on Feb 20, 2020
  7. scalablecory commented on Mar 10, 2020

    @scalablecory
    Contributor

    Duplicate of #33417. Not really (using Github keyword) -- just consolidating socket API proposals to streamline for focused API review.

  8. modified the milestones: Future, 5.0.0 on Aug 18, 2020
  9. geoffkizer commented on Oct 26, 2020

    @geoffkizer
    Contributor

    I'm going to reopen this so we can do API review specifically on this (as opposed to all the APIs in #33417).

    Also we should add a CancellationToken.

    Updated proposal:

        public static class SocketTaskExtensions
        {
            // Existing APIs:
            // public IAsyncResult BeginDisconnect (bool reuseSocket, AsyncCallback callback, object state);
            // public void EndDisconnect (IAsyncResult asyncResult);
    
            public static Task DisconnectAsync(this Socket that, bool reuseSocket, CancellationToken cancellationToken);
        }
  10. modified the milestones: 5.0.0, 6.0.0 on Oct 26, 2020
  11. added
    api-ready-for-review
    and removed
    api-suggestion
    on Oct 26, 2020
  12. added
    api-approved
    and removed
    api-ready-for-review
    on Oct 27, 2020
  13. terrajobst commented on Oct 27, 2020

    @terrajobst
    Contributor

    Video

    • Looks good as proposed
    • We should really consider "removing" SocketTaskExtensions
      • Merge with instance methods
      • Hide type & member
      • If we do this for .NET 6, we should delete DisconnectAsync from this type
      • @geoffkizer, could you file an issue for this?
    • We should use ValueTask
    namespace System.Net.Sockets
    {
        public static class SocketTaskExtensions
        {
            // Existing APIs:
            // public IAsyncResult BeginDisconnect (bool reuseSocket, AsyncCallback callback, object state);
            // public void EndDisconnect (IAsyncResult asyncResult);
    
            public static ValueTask DisconnectAsync(this Socket socket, bool reuseSocket, CancellationToken cancellationToken=default);
        }
    }
  14. geoffkizer commented on Oct 27, 2020

    @geoffkizer
    Contributor

    We should really consider "removing" SocketTaskExtensions

    Added #43901

  15. geoffkizer commented on Nov 10, 2020

    @geoffkizer
    Contributor

    Note, per approval of #43901, this should be added as an instance method and not added to SocketTaskExtensions.

    namespace System.Net.Sockets
    {
        public partial class Socket
        {
            public ValueTask DisconnectAsync(bool reuseSocket, CancellationToken cancellationToken=default);
        }
    }
  16. ghost added
    in-pr
    on Jan 13, 2021
  17. ghost removed
    in-pr
    on Feb 16, 2021
  18. ghost added
    in-pr
    on Apr 14, 2021
  19. ghost removed
    in-pr
    on Apr 18, 2021
  20. ghost locked as resolved and limited conversation to collaborators on May 18, 2021
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

    api-approvedarea-System.Net.Sockets

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions