Skip to content

Commit c092a76

Browse files
authored
SmtpClientTest Assertion Fail Fix (#76361)
* Eliminate temporary -1 on Socket.Unix.cs
1 parent bbcff6b commit c092a76

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Unix.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Win32.SafeHandles;
99
using System.Reflection;
1010
using System.Collections;
11+
using System.Threading;
1112

1213
namespace System.Net.Sockets
1314
{
@@ -108,7 +109,7 @@ internal void ReplaceHandleIfNecessaryAfterFailedConnect()
108109
SocketError errorCode = ReplaceHandle();
109110
if (errorCode != SocketError.Success)
110111
{
111-
throw new SocketException((int) errorCode);
112+
throw new SocketException((int)errorCode);
112113
}
113114

114115
_handle.LastConnectFailed = false;
@@ -137,14 +138,22 @@ internal SocketError ReplaceHandle()
137138

138139
// Then replace the handle with a new one
139140
SafeSocketHandle oldHandle = _handle;
140-
SocketError errorCode = SocketPal.CreateSocket(_addressFamily, _socketType, _protocolType, out _handle);
141+
SocketError errorCode = SocketPal.CreateSocket(_addressFamily, _socketType, _protocolType, out SafeSocketHandle newHandle);
142+
Volatile.Write(ref _handle, newHandle);
141143
oldHandle.TransferTrackedState(_handle);
142144
oldHandle.Dispose();
145+
143146
if (errorCode != SocketError.Success)
144147
{
145148
return errorCode;
146149
}
147150

151+
if (Volatile.Read(ref _disposed) != 0)
152+
{
153+
_handle.Dispose();
154+
throw new ObjectDisposedException(GetType().FullName);
155+
}
156+
148157
// And put back the copied settings. For DualMode, we use the value stored in the _handle
149158
// rather than querying the socket itself, as on Unix stacks binding a dual-mode socket to
150159
// an IPv6 address may cause the IPv6Only setting to revert to true.

0 commit comments

Comments
 (0)