Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ConnectionListener.AcceptAsync() to return null #41304

Closed
halter73 opened this issue Aug 25, 2020 · 6 comments · Fixed by #41442
Closed

Allow ConnectionListener.AcceptAsync() to return null #41304

halter73 opened this issue Aug 25, 2020 · 6 comments · Fixed by #41442
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Net
Milestone

Comments

@halter73
Copy link
Member

Background and Motivation

When migrating Kestrel to the new System.Net.Connections APIs, I noticed that listeners need to throw from AcceptAsync when shutting down since AcceptAsync cannot return null (at least according to the signature).

In the case where there are ongoing AcceptAsync calls when the listener is disposed, I would prefer to be able to return null rather than throw an exception in order to avoid unnecessary first-chance exceptions when listeners are gracefully shutting down. For listeners based on managed Sockets, there's likely to be an exception either way, but other listeners my be able to avoid throwing exceptions during shutdown entirely.

null should never be returned unless the listener has been closed. null indicates to accept loops that they should exit.

Proposed API

namespace System.Net.Connections
{
    public class ConnectionListener
    {
-        public abstract System.Threading.Tasks.ValueTask<System.Net.Connections.Connection> AcceptAsync(System.Net.Connections.IConnectionProperties? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
+        public abstract System.Threading.Tasks.ValueTask<System.Net.Connections.Connection?> AcceptAsync(System.Net.Connections.IConnectionProperties? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
    }

Alternative Designs

Throwing an OperationCanceledException, ObjectDisposedException or similar instead of returning null.

cc @geoffkizer @scalablecory @davidfowl

@halter73 halter73 added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Net labels Aug 25, 2020
@ghost
Copy link

ghost commented Aug 25, 2020

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Aug 25, 2020
@davidfowl
Copy link
Member

I though we agreed to support returning null

@geoffkizer
Copy link
Contributor

In the case where there are ongoing AcceptAsync calls when the listener is disposed, I would prefer to be able to return null rather than throw an exception

It's worth pointing out that it's almost always the case that there's an outstanding AcceptAsync when the listener is disposed. In other words, this isn't some corner case, it's the common case, and that's why we want to avoid exceptions here.

@scalablecory scalablecory added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation untriaged New issue has not been triaged by the area owner labels Aug 25, 2020
@scalablecory scalablecory added this to the 5.0.0 milestone Aug 25, 2020
@scalablecory scalablecory added the blocking Marks issues that we want to fast track in order to unblock other important work label Aug 25, 2020
@terrajobst terrajobst added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation blocking Marks issues that we want to fast track in order to unblock other important work labels Aug 25, 2020
@terrajobst
Copy link
Member

terrajobst commented Aug 25, 2020

Video

  • Looks good as proposed
namespace System.Net.Connections
{
    public partial class ConnectionListener
    {
        public abstract ValueTask<Connection?> AcceptAsync(
            IConnectionProperties? options = null,
            CancellationToken cancellationToken = default);
    }
}

@karelz
Copy link
Member

karelz commented Aug 27, 2020

Reopening for 5.0 backport in #41453

@karelz karelz reopened this Aug 27, 2020
CarnaViire pushed a commit that referenced this issue Aug 28, 2020
Co-authored-by: Natalia Kondratyeva <[email protected]>

Backport of #41442 to release/5.0

As it was discussed in #41304, with current System.Net.Connections APIs, listeners need to throw from AcceptAsync when shutting down since AcceptAsync cannot return null. This PR allows AcceptAsync to return null, to avoid unnecessary first-chance exceptions when listeners are gracefully shutting down.
@karelz
Copy link
Member

karelz commented Aug 28, 2020

Fixed in 6.0/master (PR #41442) and in 5.0 (PR #41453).

@karelz karelz closed this as completed Aug 28, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Net
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants