Add cache for Socket.LocalEndPoint - #39313

Merged
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482
Sep 2, 2020
Merged

Add cache for Socket.LocalEndPoint#39313
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482

Conversation

@CarnaViire

@CarnaViireCarnaViire commented Jul 14, 2020

Copy link
Copy Markdown
Member

Added _localEndPoint field to cache LocalEndPoint value. Introduced RightEndPoint property to encapsulate clearing _localEndPoint on _rightEndPoint change.Cached value is cleared on connect, error and disconnect. Cached value is cleared on error and disconnect. If the value is a wildcard address, it is also cleared on connect and accept.

Fix#1482

@ghost

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire
CarnaViire marked this pull request as draft July 20, 2020 08:23
@CarnaViire
CarnaViire marked this pull request as ready for review July 23, 2020 15:21
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@karelzkarelz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but I let @antonfirsov make the final ok ;)

@antonfirsov

antonfirsov commented Jul 26, 2020

Copy link
Copy Markdown
Contributor

I'm concerned if we should maybe reset _localEndpoint to null in methods where we also reset _rightEndpoint:

EndPoint?oldEndPoint=_rightEndPoint;
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketErrorerrorCode=SocketError.SocketError;
try
{
if(_rightEndPoint==null)
{
_rightEndPoint=endPointSnapshot;
}
errorCode=SocketPal.SendToAsync(_handle,buffer,offset,size,socketFlags,socketAddress,asyncResult);
if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SendToAsync returns:{errorCode} size:{size} returning AsyncResult:{asyncResult}");
}
catch(ObjectDisposedException)
{
_rightEndPoint=oldEndPoint;
throw;
}
// Throw an appropriate SocketException if the native call fails synchronously.
if(!CheckErrorAndUpdateStatus(errorCode))
{
UpdateSendSocketErrorForDisposed(referrorCode);
// Update the internal state of this socket according to the error before throwing.
_rightEndPoint=oldEndPoint;

If I'm not missing anything there will be no user callback querying LocalEndpoint in case of failure, however we may end up adding internal code doing so in the future, for example in a tracing call like this:

if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SRC:{LocalEndPoint}");

Such a call may enforce caching a wrong value for LocalEndpoint permanently, on the other hand it's probably very unlikely we do such a thing by accident. I wonder if it makes sense to add tests to ensure such failing async methods will not alter the sockets internal state in an unwanted way.

@tmds maybe you have some thoughts on this?

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

@antonfirsov Here is a list of all changes of _rightEndPoint that I have found in the code (sorry for the long list, I would put it under cut if I knew how)

  1. setting to new value

    1.1. in constructor
    Socket.cs(165:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(173:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(180:25): _rightEndPoint = new UnixDomainSocketEndPoint(IPEndPointExtensions.GetNetSocketAddress(socketAddress));
    Socket.Windows.cs(69:17): _rightEndPoint = ep.Create(socketAddress);

    1.2. after non-blocking connect
    [LocalEndPoint] Socket.cs(319:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [RemoteEndPoint] Socket.cs(361:25): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [Connected] Socket.cs(472:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;

    1.3. if it was null before
    [DoBind] Socket.cs(834:17): _rightEndPoint = endPointSnapshot;
    [SendTo] Socket.cs(1329:17): _rightEndPoint = remoteEP;
    [ReceiveMessageFrom] Socket.cs(1566:21): _rightEndPoint = endPointSnapshot;
    [ReceiveFrom] Socket.cs(1650:21): _rightEndPoint = endPointSnapshot;
    [DoBeginSendTo] Socket.cs(2694:21): _rightEndPoint = endPointSnapshot;
    [BeginReceiveMessageFrom] Socket.cs(3059:21): _rightEndPoint = remoteEP;
    [DoBeginReceiveFrom] Socket.cs(3276:21): _rightEndPoint = endPointSnapshot;
    [ConnectAsync] Socket.cs(3677:21): _rightEndPoint = endPointSnapshot;
    [SendToAsync] Socket.cs(4019:17): _rightEndPoint = endPointSnapshot;
    [DoConnect] Socket.cs(4155:17): _rightEndPoint = endPointSnapshot;
    [BeginConnectEx] Socket.cs(4536:17): _rightEndPoint = endPointSnapshot;

    1.4. on inheriting other socket state
    [UpdateAcceptSocket] Socket.cs(4784:20): socket._rightEndPoint = _rightEndPoint;

  2. setting to previous value - on some error

    2.1. on ObjectDisposedException, throwing later
    [DoBeginSendTo] Socket.cs(2703:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3081:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3285:17): _rightEndPoint = oldEndPoint;

    2.2. on any exception, throwing later
    [ConnectAsync] Socket.cs(3700:21): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4547:17): _rightEndPoint = oldEndPoint;

    2.3. on false CheckErrorAndUpdateStatus, throwing later
    [DoBeginSendTo] Socket.cs(2712:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3090:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3294:17): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4563:17): _rightEndPoint = oldEndPoint;

    2.4. on false CheckErrorAndUpdateStatus, NO throwing
    [SendToAsync] Socket.cs(4037:17): _rightEndPoint = oldEndPoint;

  3. setting to null - on some error

    3.1. on any exception, throwing later
    [SendToAsync] Socket.cs(4029:17): _rightEndPoint = null;

You suggest to clear _localEndPoint on the reset of _rightEndPoint, which are items 2. and 3. on the list. If we look at subitems of item 1., we can see that most of them are if _rightEndPoint was null before or in the constructor, which means if we clear _localEndPoint at this time we won't actually change anything. And a change of _rightEndPoint after non-blocking connect will only happen once I believe.

What I am trying to say is if we want to clear _localEndPoint on the reset of _rightEndPoint, maybe we could actually return to my original proposal of clearing _localEndPoint on every _rightEndPoint change? The only place where it might possibly unnesessary clear some cached value is this non-blocking connect part (item 1.2.) and we might use _rightEndPoint there instead of new property RightEndPoint specifically in this case.

Comment threadsrc/libraries/System.Net.Sockets/tests/FunctionalTests/LocalEndPointTest.cs Outdated
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire
CarnaViire marked this pull request as draft August 21, 2020 19:44
@CarnaViire
CarnaViire marked this pull request as ready for review August 25, 2020 16:13
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

So the behavior with wildcard addresses is as follows.

  1. If a TCP server socket (listener socket) was bound to a wildcard address, then the accept socket created with accept will be assigned with a specific address based on what interface a client socket connected to. That means we can't copy cached listener's local endpoint if it is wildcard. However, if the listener was bound to a specific address, we can copy that to the accept socket freely.

  2. If a TCP client socket was bound to a wildcard address, then it will be assigned with a specific address after connect. That means we should clear the client socket's cached local endpoint if it is wildcard.

  3. If an UDP socket was bound to a wildcard address, it stays as wildcard even after sendto and other calls.

I added clearing of cached wildcard local endpoints on connect and accept with clarifying comments. Also added tests that illustrate this behavior.

cc @geoffkizer@antonfirsov

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

CarnaViire commented Aug 27, 2020

Copy link
Copy Markdown
MemberAuthor

Outerloop test failures are #40926 and #41381

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated

@antonfirsovantonfirsov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to make sure all independent implementations are covered in the tests, otherwise looks good now.

Comment on lines +63 to +111
[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before sendto

var sendToEP = new IPEndPoint(Loopback, serverPort);

client.SendTo(new byte[] { 1, 2, 3 }, sendToEP);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after sendto

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 1, 2, 3 }, buf);
}
}

[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnAsyncSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before async WSASendTo

var sendToEP = new IPEndPoint(Loopback, serverPort);

IAsyncResult sendToResult = client.BeginSendTo(new byte[] { 4, 5, 6 }, 0, 3, SocketFlags.None, sendToEP, null, null);
sendToResult.AsyncWaitHandle.WaitOne();
client.EndSendTo(sendToResult);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after async WSASendTo

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 4, 5, 6 }, buf);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These 2 tests seem to test the same thing for two different connect implementations. I suggest dedupe their code and also cover the remaining ones: ConnectAsync(SocketAsyncEventArgs) and SocketTaskExtensions.ConnectAsync(this Socket, ....). For this the best would be to utilize SocketTestHelperBase<T>.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Rewrote the tests, please check that I understood you correctly :)

@CarnaViire
CarnaViire merged commit e04ad64 into dotnet:masterSep 2, 2020
@CarnaViire
CarnaViire deleted the issue-1482 branch September 2, 2020 13:52
@ghostghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@karelzkarelz added this to the 6.0.0 milestone Jan 26, 2021
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Socket.LocalEndPoint is not cached

5 participants

@CarnaViire@antonfirsov@karelz@geoffkizer@Dotnet-GitSync-Bot
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Add cache for Socket.LocalEndPoint - #39313

Merged
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482
Sep 2, 2020
Merged

Add cache for Socket.LocalEndPoint#39313
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482

Conversation

@CarnaViire

@CarnaViireCarnaViire commented Jul 14, 2020

Copy link
Copy Markdown
Member

Added _localEndPoint field to cache LocalEndPoint value. Introduced RightEndPoint property to encapsulate clearing _localEndPoint on _rightEndPoint change.Cached value is cleared on connect, error and disconnect. Cached value is cleared on error and disconnect. If the value is a wildcard address, it is also cleared on connect and accept.

Fix#1482

@ghost

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire
CarnaViire marked this pull request as draft July 20, 2020 08:23
@CarnaViire
CarnaViire marked this pull request as ready for review July 23, 2020 15:21
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@karelzkarelz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but I let @antonfirsov make the final ok ;)

@antonfirsov

antonfirsov commented Jul 26, 2020

Copy link
Copy Markdown
Contributor

I'm concerned if we should maybe reset _localEndpoint to null in methods where we also reset _rightEndpoint:

EndPoint?oldEndPoint=_rightEndPoint;
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketErrorerrorCode=SocketError.SocketError;
try
{
if(_rightEndPoint==null)
{
_rightEndPoint=endPointSnapshot;
}
errorCode=SocketPal.SendToAsync(_handle,buffer,offset,size,socketFlags,socketAddress,asyncResult);
if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SendToAsync returns:{errorCode} size:{size} returning AsyncResult:{asyncResult}");
}
catch(ObjectDisposedException)
{
_rightEndPoint=oldEndPoint;
throw;
}
// Throw an appropriate SocketException if the native call fails synchronously.
if(!CheckErrorAndUpdateStatus(errorCode))
{
UpdateSendSocketErrorForDisposed(referrorCode);
// Update the internal state of this socket according to the error before throwing.
_rightEndPoint=oldEndPoint;

If I'm not missing anything there will be no user callback querying LocalEndpoint in case of failure, however we may end up adding internal code doing so in the future, for example in a tracing call like this:

if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SRC:{LocalEndPoint}");

Such a call may enforce caching a wrong value for LocalEndpoint permanently, on the other hand it's probably very unlikely we do such a thing by accident. I wonder if it makes sense to add tests to ensure such failing async methods will not alter the sockets internal state in an unwanted way.

@tmds maybe you have some thoughts on this?

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

@antonfirsov Here is a list of all changes of _rightEndPoint that I have found in the code (sorry for the long list, I would put it under cut if I knew how)

  1. setting to new value

    1.1. in constructor
    Socket.cs(165:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(173:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(180:25): _rightEndPoint = new UnixDomainSocketEndPoint(IPEndPointExtensions.GetNetSocketAddress(socketAddress));
    Socket.Windows.cs(69:17): _rightEndPoint = ep.Create(socketAddress);

    1.2. after non-blocking connect
    [LocalEndPoint] Socket.cs(319:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [RemoteEndPoint] Socket.cs(361:25): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [Connected] Socket.cs(472:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;

    1.3. if it was null before
    [DoBind] Socket.cs(834:17): _rightEndPoint = endPointSnapshot;
    [SendTo] Socket.cs(1329:17): _rightEndPoint = remoteEP;
    [ReceiveMessageFrom] Socket.cs(1566:21): _rightEndPoint = endPointSnapshot;
    [ReceiveFrom] Socket.cs(1650:21): _rightEndPoint = endPointSnapshot;
    [DoBeginSendTo] Socket.cs(2694:21): _rightEndPoint = endPointSnapshot;
    [BeginReceiveMessageFrom] Socket.cs(3059:21): _rightEndPoint = remoteEP;
    [DoBeginReceiveFrom] Socket.cs(3276:21): _rightEndPoint = endPointSnapshot;
    [ConnectAsync] Socket.cs(3677:21): _rightEndPoint = endPointSnapshot;
    [SendToAsync] Socket.cs(4019:17): _rightEndPoint = endPointSnapshot;
    [DoConnect] Socket.cs(4155:17): _rightEndPoint = endPointSnapshot;
    [BeginConnectEx] Socket.cs(4536:17): _rightEndPoint = endPointSnapshot;

    1.4. on inheriting other socket state
    [UpdateAcceptSocket] Socket.cs(4784:20): socket._rightEndPoint = _rightEndPoint;

  2. setting to previous value - on some error

    2.1. on ObjectDisposedException, throwing later
    [DoBeginSendTo] Socket.cs(2703:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3081:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3285:17): _rightEndPoint = oldEndPoint;

    2.2. on any exception, throwing later
    [ConnectAsync] Socket.cs(3700:21): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4547:17): _rightEndPoint = oldEndPoint;

    2.3. on false CheckErrorAndUpdateStatus, throwing later
    [DoBeginSendTo] Socket.cs(2712:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3090:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3294:17): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4563:17): _rightEndPoint = oldEndPoint;

    2.4. on false CheckErrorAndUpdateStatus, NO throwing
    [SendToAsync] Socket.cs(4037:17): _rightEndPoint = oldEndPoint;

  3. setting to null - on some error

    3.1. on any exception, throwing later
    [SendToAsync] Socket.cs(4029:17): _rightEndPoint = null;

You suggest to clear _localEndPoint on the reset of _rightEndPoint, which are items 2. and 3. on the list. If we look at subitems of item 1., we can see that most of them are if _rightEndPoint was null before or in the constructor, which means if we clear _localEndPoint at this time we won't actually change anything. And a change of _rightEndPoint after non-blocking connect will only happen once I believe.

What I am trying to say is if we want to clear _localEndPoint on the reset of _rightEndPoint, maybe we could actually return to my original proposal of clearing _localEndPoint on every _rightEndPoint change? The only place where it might possibly unnesessary clear some cached value is this non-blocking connect part (item 1.2.) and we might use _rightEndPoint there instead of new property RightEndPoint specifically in this case.

Comment threadsrc/libraries/System.Net.Sockets/tests/FunctionalTests/LocalEndPointTest.cs Outdated
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire
CarnaViire marked this pull request as draft August 21, 2020 19:44
@CarnaViire
CarnaViire marked this pull request as ready for review August 25, 2020 16:13
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

So the behavior with wildcard addresses is as follows.

  1. If a TCP server socket (listener socket) was bound to a wildcard address, then the accept socket created with accept will be assigned with a specific address based on what interface a client socket connected to. That means we can't copy cached listener's local endpoint if it is wildcard. However, if the listener was bound to a specific address, we can copy that to the accept socket freely.

  2. If a TCP client socket was bound to a wildcard address, then it will be assigned with a specific address after connect. That means we should clear the client socket's cached local endpoint if it is wildcard.

  3. If an UDP socket was bound to a wildcard address, it stays as wildcard even after sendto and other calls.

I added clearing of cached wildcard local endpoints on connect and accept with clarifying comments. Also added tests that illustrate this behavior.

cc @geoffkizer@antonfirsov

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

CarnaViire commented Aug 27, 2020

Copy link
Copy Markdown
MemberAuthor

Outerloop test failures are #40926 and #41381

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated

@antonfirsovantonfirsov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to make sure all independent implementations are covered in the tests, otherwise looks good now.

Comment on lines +63 to +111
[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before sendto

var sendToEP = new IPEndPoint(Loopback, serverPort);

client.SendTo(new byte[] { 1, 2, 3 }, sendToEP);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after sendto

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 1, 2, 3 }, buf);
}
}

[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnAsyncSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before async WSASendTo

var sendToEP = new IPEndPoint(Loopback, serverPort);

IAsyncResult sendToResult = client.BeginSendTo(new byte[] { 4, 5, 6 }, 0, 3, SocketFlags.None, sendToEP, null, null);
sendToResult.AsyncWaitHandle.WaitOne();
client.EndSendTo(sendToResult);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after async WSASendTo

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 4, 5, 6 }, buf);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These 2 tests seem to test the same thing for two different connect implementations. I suggest dedupe their code and also cover the remaining ones: ConnectAsync(SocketAsyncEventArgs) and SocketTaskExtensions.ConnectAsync(this Socket, ....). For this the best would be to utilize SocketTestHelperBase<T>.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Rewrote the tests, please check that I understood you correctly :)

@CarnaViire
CarnaViire merged commit e04ad64 into dotnet:masterSep 2, 2020
@CarnaViire
CarnaViire deleted the issue-1482 branch September 2, 2020 13:52
@ghostghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@karelzkarelz added this to the 6.0.0 milestone Jan 26, 2021
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Socket.LocalEndPoint is not cached

5 participants

@CarnaViire@antonfirsov@karelz@geoffkizer@Dotnet-GitSync-Bot
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Add cache for Socket.LocalEndPoint - #39313

Merged
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482
Sep 2, 2020
Merged

Add cache for Socket.LocalEndPoint#39313
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482

Conversation

@CarnaViire

@CarnaViireCarnaViire commented Jul 14, 2020

Copy link
Copy Markdown
Member

Added _localEndPoint field to cache LocalEndPoint value. Introduced RightEndPoint property to encapsulate clearing _localEndPoint on _rightEndPoint change.Cached value is cleared on connect, error and disconnect. Cached value is cleared on error and disconnect. If the value is a wildcard address, it is also cleared on connect and accept.

Fix#1482

@ghost

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire
CarnaViire marked this pull request as draft July 20, 2020 08:23
@CarnaViire
CarnaViire marked this pull request as ready for review July 23, 2020 15:21
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@karelzkarelz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but I let @antonfirsov make the final ok ;)

@antonfirsov

antonfirsov commented Jul 26, 2020

Copy link
Copy Markdown
Contributor

I'm concerned if we should maybe reset _localEndpoint to null in methods where we also reset _rightEndpoint:

EndPoint?oldEndPoint=_rightEndPoint;
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketErrorerrorCode=SocketError.SocketError;
try
{
if(_rightEndPoint==null)
{
_rightEndPoint=endPointSnapshot;
}
errorCode=SocketPal.SendToAsync(_handle,buffer,offset,size,socketFlags,socketAddress,asyncResult);
if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SendToAsync returns:{errorCode} size:{size} returning AsyncResult:{asyncResult}");
}
catch(ObjectDisposedException)
{
_rightEndPoint=oldEndPoint;
throw;
}
// Throw an appropriate SocketException if the native call fails synchronously.
if(!CheckErrorAndUpdateStatus(errorCode))
{
UpdateSendSocketErrorForDisposed(referrorCode);
// Update the internal state of this socket according to the error before throwing.
_rightEndPoint=oldEndPoint;

If I'm not missing anything there will be no user callback querying LocalEndpoint in case of failure, however we may end up adding internal code doing so in the future, for example in a tracing call like this:

if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SRC:{LocalEndPoint}");

Such a call may enforce caching a wrong value for LocalEndpoint permanently, on the other hand it's probably very unlikely we do such a thing by accident. I wonder if it makes sense to add tests to ensure such failing async methods will not alter the sockets internal state in an unwanted way.

@tmds maybe you have some thoughts on this?

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

@antonfirsov Here is a list of all changes of _rightEndPoint that I have found in the code (sorry for the long list, I would put it under cut if I knew how)

  1. setting to new value

    1.1. in constructor
    Socket.cs(165:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(173:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(180:25): _rightEndPoint = new UnixDomainSocketEndPoint(IPEndPointExtensions.GetNetSocketAddress(socketAddress));
    Socket.Windows.cs(69:17): _rightEndPoint = ep.Create(socketAddress);

    1.2. after non-blocking connect
    [LocalEndPoint] Socket.cs(319:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [RemoteEndPoint] Socket.cs(361:25): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [Connected] Socket.cs(472:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;

    1.3. if it was null before
    [DoBind] Socket.cs(834:17): _rightEndPoint = endPointSnapshot;
    [SendTo] Socket.cs(1329:17): _rightEndPoint = remoteEP;
    [ReceiveMessageFrom] Socket.cs(1566:21): _rightEndPoint = endPointSnapshot;
    [ReceiveFrom] Socket.cs(1650:21): _rightEndPoint = endPointSnapshot;
    [DoBeginSendTo] Socket.cs(2694:21): _rightEndPoint = endPointSnapshot;
    [BeginReceiveMessageFrom] Socket.cs(3059:21): _rightEndPoint = remoteEP;
    [DoBeginReceiveFrom] Socket.cs(3276:21): _rightEndPoint = endPointSnapshot;
    [ConnectAsync] Socket.cs(3677:21): _rightEndPoint = endPointSnapshot;
    [SendToAsync] Socket.cs(4019:17): _rightEndPoint = endPointSnapshot;
    [DoConnect] Socket.cs(4155:17): _rightEndPoint = endPointSnapshot;
    [BeginConnectEx] Socket.cs(4536:17): _rightEndPoint = endPointSnapshot;

    1.4. on inheriting other socket state
    [UpdateAcceptSocket] Socket.cs(4784:20): socket._rightEndPoint = _rightEndPoint;

  2. setting to previous value - on some error

    2.1. on ObjectDisposedException, throwing later
    [DoBeginSendTo] Socket.cs(2703:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3081:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3285:17): _rightEndPoint = oldEndPoint;

    2.2. on any exception, throwing later
    [ConnectAsync] Socket.cs(3700:21): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4547:17): _rightEndPoint = oldEndPoint;

    2.3. on false CheckErrorAndUpdateStatus, throwing later
    [DoBeginSendTo] Socket.cs(2712:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3090:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3294:17): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4563:17): _rightEndPoint = oldEndPoint;

    2.4. on false CheckErrorAndUpdateStatus, NO throwing
    [SendToAsync] Socket.cs(4037:17): _rightEndPoint = oldEndPoint;

  3. setting to null - on some error

    3.1. on any exception, throwing later
    [SendToAsync] Socket.cs(4029:17): _rightEndPoint = null;

You suggest to clear _localEndPoint on the reset of _rightEndPoint, which are items 2. and 3. on the list. If we look at subitems of item 1., we can see that most of them are if _rightEndPoint was null before or in the constructor, which means if we clear _localEndPoint at this time we won't actually change anything. And a change of _rightEndPoint after non-blocking connect will only happen once I believe.

What I am trying to say is if we want to clear _localEndPoint on the reset of _rightEndPoint, maybe we could actually return to my original proposal of clearing _localEndPoint on every _rightEndPoint change? The only place where it might possibly unnesessary clear some cached value is this non-blocking connect part (item 1.2.) and we might use _rightEndPoint there instead of new property RightEndPoint specifically in this case.

Comment threadsrc/libraries/System.Net.Sockets/tests/FunctionalTests/LocalEndPointTest.cs Outdated
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire
CarnaViire marked this pull request as draft August 21, 2020 19:44
@CarnaViire
CarnaViire marked this pull request as ready for review August 25, 2020 16:13
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

So the behavior with wildcard addresses is as follows.

  1. If a TCP server socket (listener socket) was bound to a wildcard address, then the accept socket created with accept will be assigned with a specific address based on what interface a client socket connected to. That means we can't copy cached listener's local endpoint if it is wildcard. However, if the listener was bound to a specific address, we can copy that to the accept socket freely.

  2. If a TCP client socket was bound to a wildcard address, then it will be assigned with a specific address after connect. That means we should clear the client socket's cached local endpoint if it is wildcard.

  3. If an UDP socket was bound to a wildcard address, it stays as wildcard even after sendto and other calls.

I added clearing of cached wildcard local endpoints on connect and accept with clarifying comments. Also added tests that illustrate this behavior.

cc @geoffkizer@antonfirsov

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

CarnaViire commented Aug 27, 2020

Copy link
Copy Markdown
MemberAuthor

Outerloop test failures are #40926 and #41381

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated

@antonfirsovantonfirsov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to make sure all independent implementations are covered in the tests, otherwise looks good now.

Comment on lines +63 to +111
[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before sendto

var sendToEP = new IPEndPoint(Loopback, serverPort);

client.SendTo(new byte[] { 1, 2, 3 }, sendToEP);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after sendto

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 1, 2, 3 }, buf);
}
}

[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnAsyncSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before async WSASendTo

var sendToEP = new IPEndPoint(Loopback, serverPort);

IAsyncResult sendToResult = client.BeginSendTo(new byte[] { 4, 5, 6 }, 0, 3, SocketFlags.None, sendToEP, null, null);
sendToResult.AsyncWaitHandle.WaitOne();
client.EndSendTo(sendToResult);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after async WSASendTo

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 4, 5, 6 }, buf);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These 2 tests seem to test the same thing for two different connect implementations. I suggest dedupe their code and also cover the remaining ones: ConnectAsync(SocketAsyncEventArgs) and SocketTaskExtensions.ConnectAsync(this Socket, ....). For this the best would be to utilize SocketTestHelperBase<T>.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Rewrote the tests, please check that I understood you correctly :)

@CarnaViire
CarnaViire merged commit e04ad64 into dotnet:masterSep 2, 2020
@CarnaViire
CarnaViire deleted the issue-1482 branch September 2, 2020 13:52
@ghostghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@karelzkarelz added this to the 6.0.0 milestone Jan 26, 2021
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Socket.LocalEndPoint is not cached

5 participants

@CarnaViire@antonfirsov@karelz@geoffkizer@Dotnet-GitSync-Bot
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Add cache for Socket.LocalEndPoint - #39313

Merged
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482
Sep 2, 2020
Merged

Add cache for Socket.LocalEndPoint#39313
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482

Conversation

@CarnaViire

@CarnaViireCarnaViire commented Jul 14, 2020

Copy link
Copy Markdown
Member

Added _localEndPoint field to cache LocalEndPoint value. Introduced RightEndPoint property to encapsulate clearing _localEndPoint on _rightEndPoint change.Cached value is cleared on connect, error and disconnect. Cached value is cleared on error and disconnect. If the value is a wildcard address, it is also cleared on connect and accept.

Fix#1482

@ghost

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire
CarnaViire marked this pull request as draft July 20, 2020 08:23
@CarnaViire
CarnaViire marked this pull request as ready for review July 23, 2020 15:21
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@karelzkarelz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but I let @antonfirsov make the final ok ;)

@antonfirsov

antonfirsov commented Jul 26, 2020

Copy link
Copy Markdown
Contributor

I'm concerned if we should maybe reset _localEndpoint to null in methods where we also reset _rightEndpoint:

EndPoint?oldEndPoint=_rightEndPoint;
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketErrorerrorCode=SocketError.SocketError;
try
{
if(_rightEndPoint==null)
{
_rightEndPoint=endPointSnapshot;
}
errorCode=SocketPal.SendToAsync(_handle,buffer,offset,size,socketFlags,socketAddress,asyncResult);
if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SendToAsync returns:{errorCode} size:{size} returning AsyncResult:{asyncResult}");
}
catch(ObjectDisposedException)
{
_rightEndPoint=oldEndPoint;
throw;
}
// Throw an appropriate SocketException if the native call fails synchronously.
if(!CheckErrorAndUpdateStatus(errorCode))
{
UpdateSendSocketErrorForDisposed(referrorCode);
// Update the internal state of this socket according to the error before throwing.
_rightEndPoint=oldEndPoint;

If I'm not missing anything there will be no user callback querying LocalEndpoint in case of failure, however we may end up adding internal code doing so in the future, for example in a tracing call like this:

if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SRC:{LocalEndPoint}");

Such a call may enforce caching a wrong value for LocalEndpoint permanently, on the other hand it's probably very unlikely we do such a thing by accident. I wonder if it makes sense to add tests to ensure such failing async methods will not alter the sockets internal state in an unwanted way.

@tmds maybe you have some thoughts on this?

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

@antonfirsov Here is a list of all changes of _rightEndPoint that I have found in the code (sorry for the long list, I would put it under cut if I knew how)

  1. setting to new value

    1.1. in constructor
    Socket.cs(165:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(173:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(180:25): _rightEndPoint = new UnixDomainSocketEndPoint(IPEndPointExtensions.GetNetSocketAddress(socketAddress));
    Socket.Windows.cs(69:17): _rightEndPoint = ep.Create(socketAddress);

    1.2. after non-blocking connect
    [LocalEndPoint] Socket.cs(319:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [RemoteEndPoint] Socket.cs(361:25): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [Connected] Socket.cs(472:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;

    1.3. if it was null before
    [DoBind] Socket.cs(834:17): _rightEndPoint = endPointSnapshot;
    [SendTo] Socket.cs(1329:17): _rightEndPoint = remoteEP;
    [ReceiveMessageFrom] Socket.cs(1566:21): _rightEndPoint = endPointSnapshot;
    [ReceiveFrom] Socket.cs(1650:21): _rightEndPoint = endPointSnapshot;
    [DoBeginSendTo] Socket.cs(2694:21): _rightEndPoint = endPointSnapshot;
    [BeginReceiveMessageFrom] Socket.cs(3059:21): _rightEndPoint = remoteEP;
    [DoBeginReceiveFrom] Socket.cs(3276:21): _rightEndPoint = endPointSnapshot;
    [ConnectAsync] Socket.cs(3677:21): _rightEndPoint = endPointSnapshot;
    [SendToAsync] Socket.cs(4019:17): _rightEndPoint = endPointSnapshot;
    [DoConnect] Socket.cs(4155:17): _rightEndPoint = endPointSnapshot;
    [BeginConnectEx] Socket.cs(4536:17): _rightEndPoint = endPointSnapshot;

    1.4. on inheriting other socket state
    [UpdateAcceptSocket] Socket.cs(4784:20): socket._rightEndPoint = _rightEndPoint;

  2. setting to previous value - on some error

    2.1. on ObjectDisposedException, throwing later
    [DoBeginSendTo] Socket.cs(2703:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3081:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3285:17): _rightEndPoint = oldEndPoint;

    2.2. on any exception, throwing later
    [ConnectAsync] Socket.cs(3700:21): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4547:17): _rightEndPoint = oldEndPoint;

    2.3. on false CheckErrorAndUpdateStatus, throwing later
    [DoBeginSendTo] Socket.cs(2712:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3090:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3294:17): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4563:17): _rightEndPoint = oldEndPoint;

    2.4. on false CheckErrorAndUpdateStatus, NO throwing
    [SendToAsync] Socket.cs(4037:17): _rightEndPoint = oldEndPoint;

  3. setting to null - on some error

    3.1. on any exception, throwing later
    [SendToAsync] Socket.cs(4029:17): _rightEndPoint = null;

You suggest to clear _localEndPoint on the reset of _rightEndPoint, which are items 2. and 3. on the list. If we look at subitems of item 1., we can see that most of them are if _rightEndPoint was null before or in the constructor, which means if we clear _localEndPoint at this time we won't actually change anything. And a change of _rightEndPoint after non-blocking connect will only happen once I believe.

What I am trying to say is if we want to clear _localEndPoint on the reset of _rightEndPoint, maybe we could actually return to my original proposal of clearing _localEndPoint on every _rightEndPoint change? The only place where it might possibly unnesessary clear some cached value is this non-blocking connect part (item 1.2.) and we might use _rightEndPoint there instead of new property RightEndPoint specifically in this case.

Comment threadsrc/libraries/System.Net.Sockets/tests/FunctionalTests/LocalEndPointTest.cs Outdated
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire
CarnaViire marked this pull request as draft August 21, 2020 19:44
@CarnaViire
CarnaViire marked this pull request as ready for review August 25, 2020 16:13
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

So the behavior with wildcard addresses is as follows.

  1. If a TCP server socket (listener socket) was bound to a wildcard address, then the accept socket created with accept will be assigned with a specific address based on what interface a client socket connected to. That means we can't copy cached listener's local endpoint if it is wildcard. However, if the listener was bound to a specific address, we can copy that to the accept socket freely.

  2. If a TCP client socket was bound to a wildcard address, then it will be assigned with a specific address after connect. That means we should clear the client socket's cached local endpoint if it is wildcard.

  3. If an UDP socket was bound to a wildcard address, it stays as wildcard even after sendto and other calls.

I added clearing of cached wildcard local endpoints on connect and accept with clarifying comments. Also added tests that illustrate this behavior.

cc @geoffkizer@antonfirsov

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

CarnaViire commented Aug 27, 2020

Copy link
Copy Markdown
MemberAuthor

Outerloop test failures are #40926 and #41381

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated

@antonfirsovantonfirsov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to make sure all independent implementations are covered in the tests, otherwise looks good now.

Comment on lines +63 to +111
[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before sendto

var sendToEP = new IPEndPoint(Loopback, serverPort);

client.SendTo(new byte[] { 1, 2, 3 }, sendToEP);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after sendto

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 1, 2, 3 }, buf);
}
}

[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnAsyncSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before async WSASendTo

var sendToEP = new IPEndPoint(Loopback, serverPort);

IAsyncResult sendToResult = client.BeginSendTo(new byte[] { 4, 5, 6 }, 0, 3, SocketFlags.None, sendToEP, null, null);
sendToResult.AsyncWaitHandle.WaitOne();
client.EndSendTo(sendToResult);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after async WSASendTo

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 4, 5, 6 }, buf);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These 2 tests seem to test the same thing for two different connect implementations. I suggest dedupe their code and also cover the remaining ones: ConnectAsync(SocketAsyncEventArgs) and SocketTaskExtensions.ConnectAsync(this Socket, ....). For this the best would be to utilize SocketTestHelperBase<T>.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Rewrote the tests, please check that I understood you correctly :)

@CarnaViire
CarnaViire merged commit e04ad64 into dotnet:masterSep 2, 2020
@CarnaViire
CarnaViire deleted the issue-1482 branch September 2, 2020 13:52
@ghostghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@karelzkarelz added this to the 6.0.0 milestone Jan 26, 2021
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Socket.LocalEndPoint is not cached

5 participants

@CarnaViire@antonfirsov@karelz@geoffkizer@Dotnet-GitSync-Bot
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Add cache for Socket.LocalEndPoint - #39313

Merged
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482
Sep 2, 2020
Merged

Add cache for Socket.LocalEndPoint#39313
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482

Conversation

@CarnaViire

@CarnaViireCarnaViire commented Jul 14, 2020

Copy link
Copy Markdown
Member

Added _localEndPoint field to cache LocalEndPoint value. Introduced RightEndPoint property to encapsulate clearing _localEndPoint on _rightEndPoint change.Cached value is cleared on connect, error and disconnect. Cached value is cleared on error and disconnect. If the value is a wildcard address, it is also cleared on connect and accept.

Fix#1482

@ghost

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire
CarnaViire marked this pull request as draft July 20, 2020 08:23
@CarnaViire
CarnaViire marked this pull request as ready for review July 23, 2020 15:21
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@karelzkarelz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but I let @antonfirsov make the final ok ;)

@antonfirsov

antonfirsov commented Jul 26, 2020

Copy link
Copy Markdown
Contributor

I'm concerned if we should maybe reset _localEndpoint to null in methods where we also reset _rightEndpoint:

EndPoint?oldEndPoint=_rightEndPoint;
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketErrorerrorCode=SocketError.SocketError;
try
{
if(_rightEndPoint==null)
{
_rightEndPoint=endPointSnapshot;
}
errorCode=SocketPal.SendToAsync(_handle,buffer,offset,size,socketFlags,socketAddress,asyncResult);
if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SendToAsync returns:{errorCode} size:{size} returning AsyncResult:{asyncResult}");
}
catch(ObjectDisposedException)
{
_rightEndPoint=oldEndPoint;
throw;
}
// Throw an appropriate SocketException if the native call fails synchronously.
if(!CheckErrorAndUpdateStatus(errorCode))
{
UpdateSendSocketErrorForDisposed(referrorCode);
// Update the internal state of this socket according to the error before throwing.
_rightEndPoint=oldEndPoint;

If I'm not missing anything there will be no user callback querying LocalEndpoint in case of failure, however we may end up adding internal code doing so in the future, for example in a tracing call like this:

if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SRC:{LocalEndPoint}");

Such a call may enforce caching a wrong value for LocalEndpoint permanently, on the other hand it's probably very unlikely we do such a thing by accident. I wonder if it makes sense to add tests to ensure such failing async methods will not alter the sockets internal state in an unwanted way.

@tmds maybe you have some thoughts on this?

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

@antonfirsov Here is a list of all changes of _rightEndPoint that I have found in the code (sorry for the long list, I would put it under cut if I knew how)

  1. setting to new value

    1.1. in constructor
    Socket.cs(165:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(173:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(180:25): _rightEndPoint = new UnixDomainSocketEndPoint(IPEndPointExtensions.GetNetSocketAddress(socketAddress));
    Socket.Windows.cs(69:17): _rightEndPoint = ep.Create(socketAddress);

    1.2. after non-blocking connect
    [LocalEndPoint] Socket.cs(319:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [RemoteEndPoint] Socket.cs(361:25): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [Connected] Socket.cs(472:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;

    1.3. if it was null before
    [DoBind] Socket.cs(834:17): _rightEndPoint = endPointSnapshot;
    [SendTo] Socket.cs(1329:17): _rightEndPoint = remoteEP;
    [ReceiveMessageFrom] Socket.cs(1566:21): _rightEndPoint = endPointSnapshot;
    [ReceiveFrom] Socket.cs(1650:21): _rightEndPoint = endPointSnapshot;
    [DoBeginSendTo] Socket.cs(2694:21): _rightEndPoint = endPointSnapshot;
    [BeginReceiveMessageFrom] Socket.cs(3059:21): _rightEndPoint = remoteEP;
    [DoBeginReceiveFrom] Socket.cs(3276:21): _rightEndPoint = endPointSnapshot;
    [ConnectAsync] Socket.cs(3677:21): _rightEndPoint = endPointSnapshot;
    [SendToAsync] Socket.cs(4019:17): _rightEndPoint = endPointSnapshot;
    [DoConnect] Socket.cs(4155:17): _rightEndPoint = endPointSnapshot;
    [BeginConnectEx] Socket.cs(4536:17): _rightEndPoint = endPointSnapshot;

    1.4. on inheriting other socket state
    [UpdateAcceptSocket] Socket.cs(4784:20): socket._rightEndPoint = _rightEndPoint;

  2. setting to previous value - on some error

    2.1. on ObjectDisposedException, throwing later
    [DoBeginSendTo] Socket.cs(2703:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3081:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3285:17): _rightEndPoint = oldEndPoint;

    2.2. on any exception, throwing later
    [ConnectAsync] Socket.cs(3700:21): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4547:17): _rightEndPoint = oldEndPoint;

    2.3. on false CheckErrorAndUpdateStatus, throwing later
    [DoBeginSendTo] Socket.cs(2712:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3090:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3294:17): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4563:17): _rightEndPoint = oldEndPoint;

    2.4. on false CheckErrorAndUpdateStatus, NO throwing
    [SendToAsync] Socket.cs(4037:17): _rightEndPoint = oldEndPoint;

  3. setting to null - on some error

    3.1. on any exception, throwing later
    [SendToAsync] Socket.cs(4029:17): _rightEndPoint = null;

You suggest to clear _localEndPoint on the reset of _rightEndPoint, which are items 2. and 3. on the list. If we look at subitems of item 1., we can see that most of them are if _rightEndPoint was null before or in the constructor, which means if we clear _localEndPoint at this time we won't actually change anything. And a change of _rightEndPoint after non-blocking connect will only happen once I believe.

What I am trying to say is if we want to clear _localEndPoint on the reset of _rightEndPoint, maybe we could actually return to my original proposal of clearing _localEndPoint on every _rightEndPoint change? The only place where it might possibly unnesessary clear some cached value is this non-blocking connect part (item 1.2.) and we might use _rightEndPoint there instead of new property RightEndPoint specifically in this case.

Comment threadsrc/libraries/System.Net.Sockets/tests/FunctionalTests/LocalEndPointTest.cs Outdated
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire
CarnaViire marked this pull request as draft August 21, 2020 19:44
@CarnaViire
CarnaViire marked this pull request as ready for review August 25, 2020 16:13
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

So the behavior with wildcard addresses is as follows.

  1. If a TCP server socket (listener socket) was bound to a wildcard address, then the accept socket created with accept will be assigned with a specific address based on what interface a client socket connected to. That means we can't copy cached listener's local endpoint if it is wildcard. However, if the listener was bound to a specific address, we can copy that to the accept socket freely.

  2. If a TCP client socket was bound to a wildcard address, then it will be assigned with a specific address after connect. That means we should clear the client socket's cached local endpoint if it is wildcard.

  3. If an UDP socket was bound to a wildcard address, it stays as wildcard even after sendto and other calls.

I added clearing of cached wildcard local endpoints on connect and accept with clarifying comments. Also added tests that illustrate this behavior.

cc @geoffkizer@antonfirsov

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

CarnaViire commented Aug 27, 2020

Copy link
Copy Markdown
MemberAuthor

Outerloop test failures are #40926 and #41381

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated

@antonfirsovantonfirsov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to make sure all independent implementations are covered in the tests, otherwise looks good now.

Comment on lines +63 to +111
[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before sendto

var sendToEP = new IPEndPoint(Loopback, serverPort);

client.SendTo(new byte[] { 1, 2, 3 }, sendToEP);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after sendto

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 1, 2, 3 }, buf);
}
}

[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnAsyncSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before async WSASendTo

var sendToEP = new IPEndPoint(Loopback, serverPort);

IAsyncResult sendToResult = client.BeginSendTo(new byte[] { 4, 5, 6 }, 0, 3, SocketFlags.None, sendToEP, null, null);
sendToResult.AsyncWaitHandle.WaitOne();
client.EndSendTo(sendToResult);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after async WSASendTo

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 4, 5, 6 }, buf);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These 2 tests seem to test the same thing for two different connect implementations. I suggest dedupe their code and also cover the remaining ones: ConnectAsync(SocketAsyncEventArgs) and SocketTaskExtensions.ConnectAsync(this Socket, ....). For this the best would be to utilize SocketTestHelperBase<T>.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Rewrote the tests, please check that I understood you correctly :)

@CarnaViire
CarnaViire merged commit e04ad64 into dotnet:masterSep 2, 2020
@CarnaViire
CarnaViire deleted the issue-1482 branch September 2, 2020 13:52
@ghostghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@karelzkarelz added this to the 6.0.0 milestone Jan 26, 2021
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Socket.LocalEndPoint is not cached

5 participants

@CarnaViire@antonfirsov@karelz@geoffkizer@Dotnet-GitSync-Bot
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Add cache for Socket.LocalEndPoint - #39313

Merged
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482
Sep 2, 2020
Merged

Add cache for Socket.LocalEndPoint#39313
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482

Conversation

@CarnaViire

@CarnaViireCarnaViire commented Jul 14, 2020

Copy link
Copy Markdown
Member

Added _localEndPoint field to cache LocalEndPoint value. Introduced RightEndPoint property to encapsulate clearing _localEndPoint on _rightEndPoint change.Cached value is cleared on connect, error and disconnect. Cached value is cleared on error and disconnect. If the value is a wildcard address, it is also cleared on connect and accept.

Fix#1482

@ghost

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire
CarnaViire marked this pull request as draft July 20, 2020 08:23
@CarnaViire
CarnaViire marked this pull request as ready for review July 23, 2020 15:21
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@karelzkarelz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but I let @antonfirsov make the final ok ;)

@antonfirsov

antonfirsov commented Jul 26, 2020

Copy link
Copy Markdown
Contributor

I'm concerned if we should maybe reset _localEndpoint to null in methods where we also reset _rightEndpoint:

EndPoint?oldEndPoint=_rightEndPoint;
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketErrorerrorCode=SocketError.SocketError;
try
{
if(_rightEndPoint==null)
{
_rightEndPoint=endPointSnapshot;
}
errorCode=SocketPal.SendToAsync(_handle,buffer,offset,size,socketFlags,socketAddress,asyncResult);
if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SendToAsync returns:{errorCode} size:{size} returning AsyncResult:{asyncResult}");
}
catch(ObjectDisposedException)
{
_rightEndPoint=oldEndPoint;
throw;
}
// Throw an appropriate SocketException if the native call fails synchronously.
if(!CheckErrorAndUpdateStatus(errorCode))
{
UpdateSendSocketErrorForDisposed(referrorCode);
// Update the internal state of this socket according to the error before throwing.
_rightEndPoint=oldEndPoint;

If I'm not missing anything there will be no user callback querying LocalEndpoint in case of failure, however we may end up adding internal code doing so in the future, for example in a tracing call like this:

if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SRC:{LocalEndPoint}");

Such a call may enforce caching a wrong value for LocalEndpoint permanently, on the other hand it's probably very unlikely we do such a thing by accident. I wonder if it makes sense to add tests to ensure such failing async methods will not alter the sockets internal state in an unwanted way.

@tmds maybe you have some thoughts on this?

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

@antonfirsov Here is a list of all changes of _rightEndPoint that I have found in the code (sorry for the long list, I would put it under cut if I knew how)

  1. setting to new value

    1.1. in constructor
    Socket.cs(165:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(173:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(180:25): _rightEndPoint = new UnixDomainSocketEndPoint(IPEndPointExtensions.GetNetSocketAddress(socketAddress));
    Socket.Windows.cs(69:17): _rightEndPoint = ep.Create(socketAddress);

    1.2. after non-blocking connect
    [LocalEndPoint] Socket.cs(319:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [RemoteEndPoint] Socket.cs(361:25): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [Connected] Socket.cs(472:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;

    1.3. if it was null before
    [DoBind] Socket.cs(834:17): _rightEndPoint = endPointSnapshot;
    [SendTo] Socket.cs(1329:17): _rightEndPoint = remoteEP;
    [ReceiveMessageFrom] Socket.cs(1566:21): _rightEndPoint = endPointSnapshot;
    [ReceiveFrom] Socket.cs(1650:21): _rightEndPoint = endPointSnapshot;
    [DoBeginSendTo] Socket.cs(2694:21): _rightEndPoint = endPointSnapshot;
    [BeginReceiveMessageFrom] Socket.cs(3059:21): _rightEndPoint = remoteEP;
    [DoBeginReceiveFrom] Socket.cs(3276:21): _rightEndPoint = endPointSnapshot;
    [ConnectAsync] Socket.cs(3677:21): _rightEndPoint = endPointSnapshot;
    [SendToAsync] Socket.cs(4019:17): _rightEndPoint = endPointSnapshot;
    [DoConnect] Socket.cs(4155:17): _rightEndPoint = endPointSnapshot;
    [BeginConnectEx] Socket.cs(4536:17): _rightEndPoint = endPointSnapshot;

    1.4. on inheriting other socket state
    [UpdateAcceptSocket] Socket.cs(4784:20): socket._rightEndPoint = _rightEndPoint;

  2. setting to previous value - on some error

    2.1. on ObjectDisposedException, throwing later
    [DoBeginSendTo] Socket.cs(2703:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3081:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3285:17): _rightEndPoint = oldEndPoint;

    2.2. on any exception, throwing later
    [ConnectAsync] Socket.cs(3700:21): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4547:17): _rightEndPoint = oldEndPoint;

    2.3. on false CheckErrorAndUpdateStatus, throwing later
    [DoBeginSendTo] Socket.cs(2712:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3090:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3294:17): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4563:17): _rightEndPoint = oldEndPoint;

    2.4. on false CheckErrorAndUpdateStatus, NO throwing
    [SendToAsync] Socket.cs(4037:17): _rightEndPoint = oldEndPoint;

  3. setting to null - on some error

    3.1. on any exception, throwing later
    [SendToAsync] Socket.cs(4029:17): _rightEndPoint = null;

You suggest to clear _localEndPoint on the reset of _rightEndPoint, which are items 2. and 3. on the list. If we look at subitems of item 1., we can see that most of them are if _rightEndPoint was null before or in the constructor, which means if we clear _localEndPoint at this time we won't actually change anything. And a change of _rightEndPoint after non-blocking connect will only happen once I believe.

What I am trying to say is if we want to clear _localEndPoint on the reset of _rightEndPoint, maybe we could actually return to my original proposal of clearing _localEndPoint on every _rightEndPoint change? The only place where it might possibly unnesessary clear some cached value is this non-blocking connect part (item 1.2.) and we might use _rightEndPoint there instead of new property RightEndPoint specifically in this case.

Comment threadsrc/libraries/System.Net.Sockets/tests/FunctionalTests/LocalEndPointTest.cs Outdated
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire
CarnaViire marked this pull request as draft August 21, 2020 19:44
@CarnaViire
CarnaViire marked this pull request as ready for review August 25, 2020 16:13
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

So the behavior with wildcard addresses is as follows.

  1. If a TCP server socket (listener socket) was bound to a wildcard address, then the accept socket created with accept will be assigned with a specific address based on what interface a client socket connected to. That means we can't copy cached listener's local endpoint if it is wildcard. However, if the listener was bound to a specific address, we can copy that to the accept socket freely.

  2. If a TCP client socket was bound to a wildcard address, then it will be assigned with a specific address after connect. That means we should clear the client socket's cached local endpoint if it is wildcard.

  3. If an UDP socket was bound to a wildcard address, it stays as wildcard even after sendto and other calls.

I added clearing of cached wildcard local endpoints on connect and accept with clarifying comments. Also added tests that illustrate this behavior.

cc @geoffkizer@antonfirsov

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

CarnaViire commented Aug 27, 2020

Copy link
Copy Markdown
MemberAuthor

Outerloop test failures are #40926 and #41381

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated

@antonfirsovantonfirsov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to make sure all independent implementations are covered in the tests, otherwise looks good now.

Comment on lines +63 to +111
[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before sendto

var sendToEP = new IPEndPoint(Loopback, serverPort);

client.SendTo(new byte[] { 1, 2, 3 }, sendToEP);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after sendto

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 1, 2, 3 }, buf);
}
}

[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnAsyncSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before async WSASendTo

var sendToEP = new IPEndPoint(Loopback, serverPort);

IAsyncResult sendToResult = client.BeginSendTo(new byte[] { 4, 5, 6 }, 0, 3, SocketFlags.None, sendToEP, null, null);
sendToResult.AsyncWaitHandle.WaitOne();
client.EndSendTo(sendToResult);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after async WSASendTo

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 4, 5, 6 }, buf);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These 2 tests seem to test the same thing for two different connect implementations. I suggest dedupe their code and also cover the remaining ones: ConnectAsync(SocketAsyncEventArgs) and SocketTaskExtensions.ConnectAsync(this Socket, ....). For this the best would be to utilize SocketTestHelperBase<T>.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Rewrote the tests, please check that I understood you correctly :)

@CarnaViire
CarnaViire merged commit e04ad64 into dotnet:masterSep 2, 2020
@CarnaViire
CarnaViire deleted the issue-1482 branch September 2, 2020 13:52
@ghostghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@karelzkarelz added this to the 6.0.0 milestone Jan 26, 2021
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Socket.LocalEndPoint is not cached

5 participants

@CarnaViire@antonfirsov@karelz@geoffkizer@Dotnet-GitSync-Bot
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Add cache for Socket.LocalEndPoint - #39313

Merged
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482
Sep 2, 2020
Merged

Add cache for Socket.LocalEndPoint#39313
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482

Conversation

@CarnaViire

@CarnaViireCarnaViire commented Jul 14, 2020

Copy link
Copy Markdown
Member

Added _localEndPoint field to cache LocalEndPoint value. Introduced RightEndPoint property to encapsulate clearing _localEndPoint on _rightEndPoint change.Cached value is cleared on connect, error and disconnect. Cached value is cleared on error and disconnect. If the value is a wildcard address, it is also cleared on connect and accept.

Fix#1482

@ghost

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire
CarnaViire marked this pull request as draft July 20, 2020 08:23
@CarnaViire
CarnaViire marked this pull request as ready for review July 23, 2020 15:21
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@karelzkarelz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but I let @antonfirsov make the final ok ;)

@antonfirsov

antonfirsov commented Jul 26, 2020

Copy link
Copy Markdown
Contributor

I'm concerned if we should maybe reset _localEndpoint to null in methods where we also reset _rightEndpoint:

EndPoint?oldEndPoint=_rightEndPoint;
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketErrorerrorCode=SocketError.SocketError;
try
{
if(_rightEndPoint==null)
{
_rightEndPoint=endPointSnapshot;
}
errorCode=SocketPal.SendToAsync(_handle,buffer,offset,size,socketFlags,socketAddress,asyncResult);
if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SendToAsync returns:{errorCode} size:{size} returning AsyncResult:{asyncResult}");
}
catch(ObjectDisposedException)
{
_rightEndPoint=oldEndPoint;
throw;
}
// Throw an appropriate SocketException if the native call fails synchronously.
if(!CheckErrorAndUpdateStatus(errorCode))
{
UpdateSendSocketErrorForDisposed(referrorCode);
// Update the internal state of this socket according to the error before throwing.
_rightEndPoint=oldEndPoint;

If I'm not missing anything there will be no user callback querying LocalEndpoint in case of failure, however we may end up adding internal code doing so in the future, for example in a tracing call like this:

if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SRC:{LocalEndPoint}");

Such a call may enforce caching a wrong value for LocalEndpoint permanently, on the other hand it's probably very unlikely we do such a thing by accident. I wonder if it makes sense to add tests to ensure such failing async methods will not alter the sockets internal state in an unwanted way.

@tmds maybe you have some thoughts on this?

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

@antonfirsov Here is a list of all changes of _rightEndPoint that I have found in the code (sorry for the long list, I would put it under cut if I knew how)

  1. setting to new value

    1.1. in constructor
    Socket.cs(165:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(173:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(180:25): _rightEndPoint = new UnixDomainSocketEndPoint(IPEndPointExtensions.GetNetSocketAddress(socketAddress));
    Socket.Windows.cs(69:17): _rightEndPoint = ep.Create(socketAddress);

    1.2. after non-blocking connect
    [LocalEndPoint] Socket.cs(319:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [RemoteEndPoint] Socket.cs(361:25): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [Connected] Socket.cs(472:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;

    1.3. if it was null before
    [DoBind] Socket.cs(834:17): _rightEndPoint = endPointSnapshot;
    [SendTo] Socket.cs(1329:17): _rightEndPoint = remoteEP;
    [ReceiveMessageFrom] Socket.cs(1566:21): _rightEndPoint = endPointSnapshot;
    [ReceiveFrom] Socket.cs(1650:21): _rightEndPoint = endPointSnapshot;
    [DoBeginSendTo] Socket.cs(2694:21): _rightEndPoint = endPointSnapshot;
    [BeginReceiveMessageFrom] Socket.cs(3059:21): _rightEndPoint = remoteEP;
    [DoBeginReceiveFrom] Socket.cs(3276:21): _rightEndPoint = endPointSnapshot;
    [ConnectAsync] Socket.cs(3677:21): _rightEndPoint = endPointSnapshot;
    [SendToAsync] Socket.cs(4019:17): _rightEndPoint = endPointSnapshot;
    [DoConnect] Socket.cs(4155:17): _rightEndPoint = endPointSnapshot;
    [BeginConnectEx] Socket.cs(4536:17): _rightEndPoint = endPointSnapshot;

    1.4. on inheriting other socket state
    [UpdateAcceptSocket] Socket.cs(4784:20): socket._rightEndPoint = _rightEndPoint;

  2. setting to previous value - on some error

    2.1. on ObjectDisposedException, throwing later
    [DoBeginSendTo] Socket.cs(2703:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3081:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3285:17): _rightEndPoint = oldEndPoint;

    2.2. on any exception, throwing later
    [ConnectAsync] Socket.cs(3700:21): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4547:17): _rightEndPoint = oldEndPoint;

    2.3. on false CheckErrorAndUpdateStatus, throwing later
    [DoBeginSendTo] Socket.cs(2712:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3090:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3294:17): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4563:17): _rightEndPoint = oldEndPoint;

    2.4. on false CheckErrorAndUpdateStatus, NO throwing
    [SendToAsync] Socket.cs(4037:17): _rightEndPoint = oldEndPoint;

  3. setting to null - on some error

    3.1. on any exception, throwing later
    [SendToAsync] Socket.cs(4029:17): _rightEndPoint = null;

You suggest to clear _localEndPoint on the reset of _rightEndPoint, which are items 2. and 3. on the list. If we look at subitems of item 1., we can see that most of them are if _rightEndPoint was null before or in the constructor, which means if we clear _localEndPoint at this time we won't actually change anything. And a change of _rightEndPoint after non-blocking connect will only happen once I believe.

What I am trying to say is if we want to clear _localEndPoint on the reset of _rightEndPoint, maybe we could actually return to my original proposal of clearing _localEndPoint on every _rightEndPoint change? The only place where it might possibly unnesessary clear some cached value is this non-blocking connect part (item 1.2.) and we might use _rightEndPoint there instead of new property RightEndPoint specifically in this case.

Comment threadsrc/libraries/System.Net.Sockets/tests/FunctionalTests/LocalEndPointTest.cs Outdated
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire
CarnaViire marked this pull request as draft August 21, 2020 19:44
@CarnaViire
CarnaViire marked this pull request as ready for review August 25, 2020 16:13
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

So the behavior with wildcard addresses is as follows.

  1. If a TCP server socket (listener socket) was bound to a wildcard address, then the accept socket created with accept will be assigned with a specific address based on what interface a client socket connected to. That means we can't copy cached listener's local endpoint if it is wildcard. However, if the listener was bound to a specific address, we can copy that to the accept socket freely.

  2. If a TCP client socket was bound to a wildcard address, then it will be assigned with a specific address after connect. That means we should clear the client socket's cached local endpoint if it is wildcard.

  3. If an UDP socket was bound to a wildcard address, it stays as wildcard even after sendto and other calls.

I added clearing of cached wildcard local endpoints on connect and accept with clarifying comments. Also added tests that illustrate this behavior.

cc @geoffkizer@antonfirsov

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

CarnaViire commented Aug 27, 2020

Copy link
Copy Markdown
MemberAuthor

Outerloop test failures are #40926 and #41381

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated

@antonfirsovantonfirsov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to make sure all independent implementations are covered in the tests, otherwise looks good now.

Comment on lines +63 to +111
[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before sendto

var sendToEP = new IPEndPoint(Loopback, serverPort);

client.SendTo(new byte[] { 1, 2, 3 }, sendToEP);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after sendto

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 1, 2, 3 }, buf);
}
}

[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnAsyncSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before async WSASendTo

var sendToEP = new IPEndPoint(Loopback, serverPort);

IAsyncResult sendToResult = client.BeginSendTo(new byte[] { 4, 5, 6 }, 0, 3, SocketFlags.None, sendToEP, null, null);
sendToResult.AsyncWaitHandle.WaitOne();
client.EndSendTo(sendToResult);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after async WSASendTo

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 4, 5, 6 }, buf);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These 2 tests seem to test the same thing for two different connect implementations. I suggest dedupe their code and also cover the remaining ones: ConnectAsync(SocketAsyncEventArgs) and SocketTaskExtensions.ConnectAsync(this Socket, ....). For this the best would be to utilize SocketTestHelperBase<T>.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Rewrote the tests, please check that I understood you correctly :)

@CarnaViire
CarnaViire merged commit e04ad64 into dotnet:masterSep 2, 2020
@CarnaViire
CarnaViire deleted the issue-1482 branch September 2, 2020 13:52
@ghostghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@karelzkarelz added this to the 6.0.0 milestone Jan 26, 2021
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Socket.LocalEndPoint is not cached

5 participants

@CarnaViire@antonfirsov@karelz@geoffkizer@Dotnet-GitSync-Bot
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Add cache for Socket.LocalEndPoint - #39313

Merged
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482
Sep 2, 2020
Merged

Add cache for Socket.LocalEndPoint#39313
CarnaViire merged 28 commits into
dotnet:masterfrom
CarnaViire:issue-1482

Conversation

@CarnaViire

@CarnaViireCarnaViire commented Jul 14, 2020

Copy link
Copy Markdown
Member

Added _localEndPoint field to cache LocalEndPoint value. Introduced RightEndPoint property to encapsulate clearing _localEndPoint on _rightEndPoint change.Cached value is cleared on connect, error and disconnect. Cached value is cleared on error and disconnect. If the value is a wildcard address, it is also cleared on connect and accept.

Fix#1482

@ghost

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire
CarnaViire marked this pull request as draft July 20, 2020 08:23
@CarnaViire
CarnaViire marked this pull request as ready for review July 23, 2020 15:21
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@karelzkarelz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but I let @antonfirsov make the final ok ;)

@antonfirsov

antonfirsov commented Jul 26, 2020

Copy link
Copy Markdown
Contributor

I'm concerned if we should maybe reset _localEndpoint to null in methods where we also reset _rightEndpoint:

EndPoint?oldEndPoint=_rightEndPoint;
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketErrorerrorCode=SocketError.SocketError;
try
{
if(_rightEndPoint==null)
{
_rightEndPoint=endPointSnapshot;
}
errorCode=SocketPal.SendToAsync(_handle,buffer,offset,size,socketFlags,socketAddress,asyncResult);
if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SendToAsync returns:{errorCode} size:{size} returning AsyncResult:{asyncResult}");
}
catch(ObjectDisposedException)
{
_rightEndPoint=oldEndPoint;
throw;
}
// Throw an appropriate SocketException if the native call fails synchronously.
if(!CheckErrorAndUpdateStatus(errorCode))
{
UpdateSendSocketErrorForDisposed(referrorCode);
// Update the internal state of this socket according to the error before throwing.
_rightEndPoint=oldEndPoint;

If I'm not missing anything there will be no user callback querying LocalEndpoint in case of failure, however we may end up adding internal code doing so in the future, for example in a tracing call like this:

if(NetEventSource.Log.IsEnabled())NetEventSource.Info(this,$"SRC:{LocalEndPoint}");

Such a call may enforce caching a wrong value for LocalEndpoint permanently, on the other hand it's probably very unlikely we do such a thing by accident. I wonder if it makes sense to add tests to ensure such failing async methods will not alter the sockets internal state in an unwanted way.

@tmds maybe you have some thoughts on this?

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

@antonfirsov Here is a list of all changes of _rightEndPoint that I have found in the code (sorry for the long list, I would put it under cut if I knew how)

  1. setting to new value

    1.1. in constructor
    Socket.cs(165:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(173:25): _rightEndPoint = new IPEndPoint(
    Socket.cs(180:25): _rightEndPoint = new UnixDomainSocketEndPoint(IPEndPointExtensions.GetNetSocketAddress(socketAddress));
    Socket.Windows.cs(69:17): _rightEndPoint = ep.Create(socketAddress);

    1.2. after non-blocking connect
    [LocalEndPoint] Socket.cs(319:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [RemoteEndPoint] Socket.cs(361:25): _rightEndPoint = _nonBlockingConnectRightEndPoint;
    [Connected] Socket.cs(472:21): _rightEndPoint = _nonBlockingConnectRightEndPoint;

    1.3. if it was null before
    [DoBind] Socket.cs(834:17): _rightEndPoint = endPointSnapshot;
    [SendTo] Socket.cs(1329:17): _rightEndPoint = remoteEP;
    [ReceiveMessageFrom] Socket.cs(1566:21): _rightEndPoint = endPointSnapshot;
    [ReceiveFrom] Socket.cs(1650:21): _rightEndPoint = endPointSnapshot;
    [DoBeginSendTo] Socket.cs(2694:21): _rightEndPoint = endPointSnapshot;
    [BeginReceiveMessageFrom] Socket.cs(3059:21): _rightEndPoint = remoteEP;
    [DoBeginReceiveFrom] Socket.cs(3276:21): _rightEndPoint = endPointSnapshot;
    [ConnectAsync] Socket.cs(3677:21): _rightEndPoint = endPointSnapshot;
    [SendToAsync] Socket.cs(4019:17): _rightEndPoint = endPointSnapshot;
    [DoConnect] Socket.cs(4155:17): _rightEndPoint = endPointSnapshot;
    [BeginConnectEx] Socket.cs(4536:17): _rightEndPoint = endPointSnapshot;

    1.4. on inheriting other socket state
    [UpdateAcceptSocket] Socket.cs(4784:20): socket._rightEndPoint = _rightEndPoint;

  2. setting to previous value - on some error

    2.1. on ObjectDisposedException, throwing later
    [DoBeginSendTo] Socket.cs(2703:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3081:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3285:17): _rightEndPoint = oldEndPoint;

    2.2. on any exception, throwing later
    [ConnectAsync] Socket.cs(3700:21): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4547:17): _rightEndPoint = oldEndPoint;

    2.3. on false CheckErrorAndUpdateStatus, throwing later
    [DoBeginSendTo] Socket.cs(2712:17): _rightEndPoint = oldEndPoint;
    [BeginReceiveMessageFrom] Socket.cs(3090:17): _rightEndPoint = oldEndPoint;
    [DoBeginReceiveFrom] Socket.cs(3294:17): _rightEndPoint = oldEndPoint;
    [BeginConnectEx] Socket.cs(4563:17): _rightEndPoint = oldEndPoint;

    2.4. on false CheckErrorAndUpdateStatus, NO throwing
    [SendToAsync] Socket.cs(4037:17): _rightEndPoint = oldEndPoint;

  3. setting to null - on some error

    3.1. on any exception, throwing later
    [SendToAsync] Socket.cs(4029:17): _rightEndPoint = null;

You suggest to clear _localEndPoint on the reset of _rightEndPoint, which are items 2. and 3. on the list. If we look at subitems of item 1., we can see that most of them are if _rightEndPoint was null before or in the constructor, which means if we clear _localEndPoint at this time we won't actually change anything. And a change of _rightEndPoint after non-blocking connect will only happen once I believe.

What I am trying to say is if we want to clear _localEndPoint on the reset of _rightEndPoint, maybe we could actually return to my original proposal of clearing _localEndPoint on every _rightEndPoint change? The only place where it might possibly unnesessary clear some cached value is this non-blocking connect part (item 1.2.) and we might use _rightEndPoint there instead of new property RightEndPoint specifically in this case.

Comment threadsrc/libraries/System.Net.Sockets/tests/FunctionalTests/LocalEndPointTest.cs Outdated
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire
CarnaViire marked this pull request as draft August 21, 2020 19:44
@CarnaViire
CarnaViire marked this pull request as ready for review August 25, 2020 16:13
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

So the behavior with wildcard addresses is as follows.

  1. If a TCP server socket (listener socket) was bound to a wildcard address, then the accept socket created with accept will be assigned with a specific address based on what interface a client socket connected to. That means we can't copy cached listener's local endpoint if it is wildcard. However, if the listener was bound to a specific address, we can copy that to the accept socket freely.

  2. If a TCP client socket was bound to a wildcard address, then it will be assigned with a specific address after connect. That means we should clear the client socket's cached local endpoint if it is wildcard.

  3. If an UDP socket was bound to a wildcard address, it stays as wildcard even after sendto and other calls.

I added clearing of cached wildcard local endpoints on connect and accept with clarifying comments. Also added tests that illustrate this behavior.

cc @geoffkizer@antonfirsov

@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
@CarnaViire

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@CarnaViire

CarnaViire commented Aug 27, 2020

Copy link
Copy Markdown
MemberAuthor

Outerloop test failures are #40926 and #41381

Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated
Comment threadsrc/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs Outdated

@antonfirsovantonfirsov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to make sure all independent implementations are covered in the tests, otherwise looks good now.

Comment on lines +63 to +111
[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before sendto

var sendToEP = new IPEndPoint(Loopback, serverPort);

client.SendTo(new byte[] { 1, 2, 3 }, sendToEP);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after sendto

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 1, 2, 3 }, buf);
}
}

[Fact]
public void UdpSocket_ClientNotBound_LocalEPBecomesWildcardOnAsyncSendTo()
{
using (Socket server = CreateUdpSocket())
using (Socket client = CreateUdpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);

Assert.Null(client.LocalEndPoint); // null before async WSASendTo

var sendToEP = new IPEndPoint(Loopback, serverPort);

IAsyncResult sendToResult = client.BeginSendTo(new byte[] { 4, 5, 6 }, 0, 3, SocketFlags.None, sendToEP, null, null);
sendToResult.AsyncWaitHandle.WaitOne();
client.EndSendTo(sendToResult);

Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard after async WSASendTo

byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
server.ReceiveFrom(buf, ref receiveFromEP);

Assert.Equal(new byte[] { 4, 5, 6 }, buf);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These 2 tests seem to test the same thing for two different connect implementations. I suggest dedupe their code and also cover the remaining ones: ConnectAsync(SocketAsyncEventArgs) and SocketTaskExtensions.ConnectAsync(this Socket, ....). For this the best would be to utilize SocketTestHelperBase<T>.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Rewrote the tests, please check that I understood you correctly :)

@CarnaViire
CarnaViire merged commit e04ad64 into dotnet:masterSep 2, 2020
@CarnaViire
CarnaViire deleted the issue-1482 branch September 2, 2020 13:52
@ghostghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@karelzkarelz added this to the 6.0.0 milestone Jan 26, 2021
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Socket.LocalEndPoint is not cached

5 participants

@CarnaViire@antonfirsov@karelz@geoffkizer@Dotnet-GitSync-Bot