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

[Draft] Idea for detection of synchronous completions for APM over Task in sockets #51693

Closed

Conversation

antonfirsov
Copy link
Member

This is an untested demonstration for my idea to resolve #47905 for BeginSend and BeginAccept, built on top of #51212.

/cc @geoffkizer @stephentoub

@ghost
Copy link

ghost commented Apr 22, 2021

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

Issue Details

This is an untested demonstration for my idea to resolve #47905 for BeginSend and BeginAccept, built on top of #51212.

/cc @geoffkizer @stephentoub

Author: antonfirsov
Assignees: -
Labels:

area-System.Net.Sockets

Milestone: -

@antonfirsov antonfirsov marked this pull request as draft April 22, 2021 13:55
@antonfirsov antonfirsov added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Apr 22, 2021
@@ -72,8 +72,7 @@ public Task<Socket> AcceptAsync(Socket? acceptSocket)
{
// The operation completed synchronously. Get a task for it.
t = saea.SocketError == SocketError.Success ?
Task.FromResult(saea.AcceptSocket!) :
Task.FromException<Socket>(GetException(saea.SocketError));
Task.FromResult(saea.AcceptSocket!) : TaskToApm.GetSynchronousExceptionTask<Socket>(GetException(saea.SocketError));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this is making the failure path more expensive for everyone, not just code using the APM methods.

else
{
buffer = new byte[receiveSize];
bytesReceived = await s.ReceiveAsync(buffer, SocketFlags.None).ConfigureAwait(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's possible the AcceptEx could previously have completed both the accept/receive synchronously, with errors from the receive aspect propagated synchronously. Now that receive is a separate operation inside of an async method, the workaround this PR is trying to utilize doesn't achieve its goal of making everything that could have failed synchronously before still fail synchronously.


return TaskToApm.Begin(SendAsync(new ReadOnlyMemory<byte>(buffer, offset, size), socketFlags, default).AsTask(), callback, state);
var task = SendAsync(new ReadOnlyMemory<byte>(buffer, offset, size), socketFlags, default).AsTask();
task.ThrowIfFailedSynchronously();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only addresses the concern for SendAsync because it's a single operation. It doesn't address cases where we're now using an async method to compose over multiple operations, like BeginConnect(IPAddress[], ...). It used to be that this would call BeginXx on the first address in the array and propagate that exception synchronously if one occurred. But we've cleaned up all of that code, and now all of the connection attempts to each IPAddress in the array are part of a loop in an async method. This proposed workaround doesn't help with such cases.

@davidfowl
Copy link
Member

Just for my education: Why put this much effort in trying to keep the APM code compatible?

@antonfirsov
Copy link
Member Author

antonfirsov commented Apr 22, 2021

@davidfowl TBH it's rather part of my (still ongoing) education process about how we do things 😄

Personally, what I hate the most about the APM changes is, that no one seems to really care that we introduce deviations from API docs silently. I hoped that this can be avoided easily in code, which is seemingly not the case, so I'm giving up on compatibility and will create a work item in the API doc repo instead.

@karelz karelz added this to the 6.0.0 milestone May 20, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jun 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net.Sockets NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Socket.Begin*** methods wrapping Task variants do not throw SocketExceptions synchronously
4 participants