@@ -25,8 +25,11 @@ public partial class Socket : IDisposable
25
25
26
26
private SafeSocketHandle _handle ;
27
27
28
- // _rightEndPoint is null if the socket has not been bound. Otherwise, it is any EndPoint of the
29
- // correct type (IPEndPoint, etc).
28
+ // _rightEndPoint is null if the socket has not been bound. Otherwise, it is an EndPoint of the
29
+ // correct type (IPEndPoint, etc). The Bind operation sets _rightEndPoint. Other operations must only set
30
+ // it when the value is still null.
31
+ // This enables tracking the file created by UnixDomainSocketEndPoint when the Socket is bound,
32
+ // and to delete that file when the Socket gets disposed.
30
33
internal EndPoint ? _rightEndPoint ;
31
34
internal EndPoint ? _remoteEndPoint ;
32
35
@@ -284,7 +287,7 @@ public EndPoint? LocalEndPoint
284
287
{
285
288
// Update the state if we've become connected after a non-blocking connect.
286
289
_isConnected = true ;
287
- _rightEndPoint = _nonBlockingConnectRightEndPoint ;
290
+ _rightEndPoint ?? = _nonBlockingConnectRightEndPoint ;
288
291
UpdateLocalEndPointOnConnect ( ) ;
289
292
_nonBlockingConnectInProgress = false ;
290
293
}
@@ -331,7 +334,7 @@ public EndPoint? RemoteEndPoint
331
334
{
332
335
// Update the state if we've become connected after a non-blocking connect.
333
336
_isConnected = true ;
334
- _rightEndPoint = _nonBlockingConnectRightEndPoint ;
337
+ _rightEndPoint ?? = _nonBlockingConnectRightEndPoint ;
335
338
UpdateLocalEndPointOnConnect ( ) ;
336
339
_nonBlockingConnectInProgress = false ;
337
340
}
@@ -438,7 +441,7 @@ public bool Connected
438
441
{
439
442
// Update the state if we've become connected after a non-blocking connect.
440
443
_isConnected = true ;
441
- _rightEndPoint = _nonBlockingConnectRightEndPoint ;
444
+ _rightEndPoint ?? = _nonBlockingConnectRightEndPoint ;
442
445
UpdateLocalEndPointOnConnect ( ) ;
443
446
_nonBlockingConnectInProgress = false ;
444
447
}
@@ -799,11 +802,11 @@ private void DoBind(EndPoint endPointSnapshot, Internals.SocketAddress socketAdd
799
802
UpdateStatusAfterSocketErrorAndThrowException ( errorCode ) ;
800
803
}
801
804
802
- if ( _rightEndPoint == null )
803
- {
804
- // Save a copy of the EndPoint so we can use it for Create().
805
- _rightEndPoint = endPointSnapshot ;
806
- }
805
+ // Save a copy of the EndPoint so we can use it for Create().
806
+ // For UnixDomainSocketEndPoint, track the file to delete on Dispose.
807
+ _rightEndPoint = endPointSnapshot is UnixDomainSocketEndPoint unixEndPoint ?
808
+ unixEndPoint . CreateBoundEndPoint ( ) :
809
+ endPointSnapshot ;
807
810
}
808
811
809
812
// Establishes a connection to a remote system.
@@ -1357,11 +1360,8 @@ public int SendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags,
1357
1360
if ( SocketType == SocketType . Dgram ) SocketsTelemetry . Log . DatagramSent ( ) ;
1358
1361
}
1359
1362
1360
- if ( _rightEndPoint == null )
1361
- {
1362
- // Save a copy of the EndPoint so we can use it for Create().
1363
- _rightEndPoint = remoteEP ;
1364
- }
1363
+ // Save a copy of the EndPoint so we can use it for Create().
1364
+ _rightEndPoint ??= remoteEP ;
1365
1365
1366
1366
if ( NetEventSource . Log . IsEnabled ( ) ) NetEventSource . DumpBuffer ( this , buffer , offset , size ) ;
1367
1367
return bytesTransferred ;
@@ -1639,11 +1639,8 @@ public int ReceiveMessageFrom(byte[] buffer, int offset, int size, ref SocketFla
1639
1639
catch
1640
1640
{
1641
1641
}
1642
- if ( _rightEndPoint == null )
1643
- {
1644
- // Save a copy of the EndPoint so we can use it for Create().
1645
- _rightEndPoint = endPointSnapshot ;
1646
- }
1642
+ // Save a copy of the EndPoint so we can use it for Create().
1643
+ _rightEndPoint ??= endPointSnapshot ;
1647
1644
}
1648
1645
1649
1646
if ( NetEventSource . Log . IsEnabled ( ) ) NetEventSource . Error ( this , errorCode ) ;
@@ -1733,11 +1730,8 @@ public int ReceiveMessageFrom(Span<byte> buffer, ref SocketFlags socketFlags, re
1733
1730
catch
1734
1731
{
1735
1732
}
1736
- if ( _rightEndPoint == null )
1737
- {
1738
- // Save a copy of the EndPoint so we can use it for Create().
1739
- _rightEndPoint = endPointSnapshot ;
1740
- }
1733
+ // Save a copy of the EndPoint so we can use it for Create().
1734
+ _rightEndPoint ??= endPointSnapshot ;
1741
1735
}
1742
1736
1743
1737
if ( NetEventSource . Log . IsEnabled ( ) ) NetEventSource . Error ( this , errorCode ) ;
@@ -1796,11 +1790,8 @@ public int ReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFl
1796
1790
catch
1797
1791
{
1798
1792
}
1799
- if ( _rightEndPoint == null )
1800
- {
1801
- // Save a copy of the EndPoint so we can use it for Create().
1802
- _rightEndPoint = endPointSnapshot ;
1803
- }
1793
+ // Save a copy of the EndPoint so we can use it for Create().
1794
+ _rightEndPoint ??= endPointSnapshot ;
1804
1795
}
1805
1796
1806
1797
if ( socketException != null )
@@ -3121,10 +3112,7 @@ private bool SendToAsync(SocketAsyncEventArgs e, CancellationToken cancellationT
3121
3112
e . StartOperationCommon ( this , SocketAsyncOperation . SendTo ) ;
3122
3113
3123
3114
EndPoint ? oldEndPoint = _rightEndPoint ;
3124
- if ( _rightEndPoint == null )
3125
- {
3126
- _rightEndPoint = endPointSnapshot ;
3127
- }
3115
+ _rightEndPoint ??= endPointSnapshot ;
3128
3116
3129
3117
SocketError socketError ;
3130
3118
try
@@ -3133,7 +3121,7 @@ private bool SendToAsync(SocketAsyncEventArgs e, CancellationToken cancellationT
3133
3121
}
3134
3122
catch
3135
3123
{
3136
- _rightEndPoint = null ;
3124
+ _rightEndPoint = oldEndPoint ;
3137
3125
_localEndPoint = null ;
3138
3126
// Clear in-use flag on event args object.
3139
3127
e . Complete ( ) ;
@@ -3229,11 +3217,8 @@ private void DoConnect(EndPoint endPointSnapshot, Internals.SocketAddress socket
3229
3217
3230
3218
if ( SocketsTelemetry . Log . IsEnabled ( ) ) SocketsTelemetry . Log . AfterConnect ( SocketError . Success ) ;
3231
3219
3232
- if ( _rightEndPoint == null )
3233
- {
3234
- // Save a copy of the EndPoint so we can use it for Create().
3235
- _rightEndPoint = endPointSnapshot ;
3236
- }
3220
+ // Save a copy of the EndPoint so we can use it for Create().
3221
+ _rightEndPoint ??= endPointSnapshot ;
3237
3222
3238
3223
if ( NetEventSource . Log . IsEnabled ( ) ) NetEventSource . Info ( this , $ "connection to:{ endPointSnapshot } ") ;
3239
3224
@@ -3361,6 +3346,18 @@ protected virtual void Dispose(bool disposing)
3361
3346
{
3362
3347
}
3363
3348
}
3349
+
3350
+ // Delete file of bound UnixDomainSocketEndPoint.
3351
+ if ( _rightEndPoint is UnixDomainSocketEndPoint unixEndPoint &&
3352
+ unixEndPoint . BoundFileName is not null )
3353
+ {
3354
+ try
3355
+ {
3356
+ File . Delete ( unixEndPoint . BoundFileName ) ;
3357
+ }
3358
+ catch
3359
+ { }
3360
+ }
3364
3361
}
3365
3362
3366
3363
// Clean up any cached data
@@ -3615,9 +3612,20 @@ internal Socket UpdateAcceptSocket(Socket socket, EndPoint remoteEP)
3615
3612
socket . _addressFamily = _addressFamily ;
3616
3613
socket . _socketType = _socketType ;
3617
3614
socket . _protocolType = _protocolType ;
3618
- socket . _rightEndPoint = _rightEndPoint ;
3619
3615
socket . _remoteEndPoint = remoteEP ;
3620
3616
3617
+ // If the _rightEndpoint tracks a UnixDomainSocketEndPoint to delete
3618
+ // then create a new EndPoint.
3619
+ if ( _rightEndPoint is UnixDomainSocketEndPoint unixEndPoint &&
3620
+ unixEndPoint . BoundFileName is not null )
3621
+ {
3622
+ socket . _rightEndPoint = unixEndPoint . CreateUnboundEndPoint ( ) ;
3623
+ }
3624
+ else
3625
+ {
3626
+ socket . _rightEndPoint = _rightEndPoint ;
3627
+ }
3628
+
3621
3629
// If the listener socket was bound to a wildcard address, then the `accept` system call
3622
3630
// will assign a specific address to the accept socket's local endpoint instead of a
3623
3631
// wildcard address. In that case we should not copy listener's wildcard local endpoint.
0 commit comments