-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Cancellable SocketAsyncEventArgs methods #48477
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsBackground and MotivationWe already have (or will likely have, for UDP) a cancellable implementation of these methods, but they are not public. I propose making them public. There are some scenarios where using SocketAsyncEventArgs will be more performant than the Task-based methods, but you must currently trade off cancellation support or get at the private methods via reflection. I'd like to not depend on reflection anymore. Proposed APIclass Socket
{
// existing.
public bool SendAsync(SocketAsyncEventArgs e);
public bool ReceiveAsync(SocketAsyncEventArgs e);
public bool SendToAsync(SocketAsyncEventArgs e);
public bool SendPacketsAsync(SocketAsyncEventArgs e);
public bool ReceiveFromAsync(SocketAsyncEventArgs e);
public bool ReceiveMessageFromAsync(SocketAsyncEventArgs e);
// new.
public bool SendAsync(SocketAsyncEventArgs e, CancellationToken cancellationToken);
public bool ReceiveAsync(SocketAsyncEventArgs e, CancellationToken cancellationToken);
public bool SendToAsync(SocketAsyncEventArgs e, CancellationToken cancellationToken);
public bool SendPacketsAsync(SocketAsyncEventArgs e, CancellationToken cancellationToken);
public bool ReceiveFromAsync(SocketAsyncEventArgs e, CancellationToken cancellationToken);
public bool ReceiveMessageFromAsync(SocketAsyncEventArgs e, CancellationToken cancellationToken);
}
|
This is a weird API that deviates from typical async patterns, has a very strong hack-taste, and is unprecedented in the BCL. I'm not sure if exposing stuff like this is a good tradeoff. Can't we instead introduce optimized |
I disagree re: API being unprecedented-- Are you proposing that |
I'm talking about specific API models.
No, but it has different patterns for cancellation (eg. My understanding is that we are committed to prefer Task-based stuff over EAP, when it comes to adding new API-s. If I'm not missing anything, the following addition should be sufficient to support the performance requirements of the "Flexible HTTP" stuff: public class Socket
{
public ValueTask SendAsync(IReadOnlyList<ReadOnlyMemory<byte>> buffers, CancellationToken cancellationToken = default);
public ValueTask<int> ReceiveAsync(IReadOnlyList<Memory<byte>> buffers, CancellationToken cancellationToken= default);
} |
"Dead" is probably a little too strong. I think the Task based APIs are our primary and preferred API, and our long-term goal should be to ensure that users never need to use SAEA instead of Task APIs due to missing features or perf. And yeah, that means we should (a) track issues re missing functionality in the Task APIs vs SAEA. and (b) do perf evaluation to ensure there's no perf gap here. The main gap I'm aware of today is scatter/gather support, but that's sort of tracked via the issue of whether/how to do scatter/gather in Stream and derived classes. |
I assume you mean #25344. That does not include the missing async socket API-s (two methods from #48477 (comment)), and AFAIK there is no tracking issue for those. |
Yeah, that's what I meant. |
Triage:
|
Idea: Create replacement issue to better capture the final intent. |
Hi, is this issue still active? runtime/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs Line 195 in 1e3544e
|
The overloads proposed here would not get rid of that CT registration, since we would be still passing the token. AFAIK most if not all CT based IO operations in .NET create a registration internally, but there several optimizations under the hood to make it as cheap as possible. Eg. in socket PAL in the cases where the socket operation completes synchronously,
Is it an assumption, or do you have data proving this? |
I am not proposing overloads with
I don't have any data, and I'd be happy to create a benchmark to test this. My thinking is that |
The standard way to achieve this would be to |
I've managed to create a benchmark. This is just a basic test, a simplified echo server: https://github.com/dhhoang/dotnet-socket-cancellation
Yes, that is indeed our use case :) |
Background and Motivation
We already have (or will likely have, for UDP) a cancellable implementation of these methods, but they are not public. I propose making them public.
There are some scenarios where using SocketAsyncEventArgs will be more performant than the Task-based methods, but you must currently trade off cancellation support or get at the private methods via reflection. I'd like to not depend on reflection anymore.
Proposed API
CC @antonfirsov @geoffkizer
The text was updated successfully, but these errors were encountered: