Skip to content

Commit b1c17f3

Browse files
committed
CSHARP-1375: Fix race conditions while connecting a socket.
1 parent c47ddde commit b1c17f3

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/MongoDB.Driver.Core/Core/Connections/TcpStreamFactory.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ private void ConfigureConnectedSocket(Socket socket)
7474

7575
private void Connect(Socket socket, EndPoint endPoint, CancellationToken cancellationToken)
7676
{
77+
var connected = false;
7778
var cancelled = false;
7879
var timedOut = false;
7980

80-
using (var registration = cancellationToken.Register(() => { cancelled = true; try { socket.Close(); } catch { } }))
81-
using (var timer = new Timer(_ => { timedOut = true; try { socket.Close(); } catch { } }, null, _settings.ConnectTimeout, Timeout.InfiniteTimeSpan))
81+
using (var registration = cancellationToken.Register(() => { if (!connected) { cancelled = true; try { socket.Close(); } catch { } } }))
82+
using (var timer = new Timer(_ => { if (!connected) { timedOut = true; try { socket.Close(); } catch { } } }, null, _settings.ConnectTimeout, Timeout.InfiniteTimeSpan))
8283
{
8384
try
8485
{
@@ -92,6 +93,8 @@ private void Connect(Socket socket, EndPoint endPoint, CancellationToken cancell
9293
{
9394
socket.Connect(endPoint);
9495
}
96+
connected = true;
97+
return;
9598
}
9699
catch
97100
{
@@ -102,6 +105,11 @@ private void Connect(Socket socket, EndPoint endPoint, CancellationToken cancell
102105
}
103106
}
104107

108+
if (socket.Connected)
109+
{
110+
try { socket.Close(); } catch { }
111+
}
112+
105113
cancellationToken.ThrowIfCancellationRequested();
106114
if (timedOut)
107115
{
@@ -112,11 +120,12 @@ private void Connect(Socket socket, EndPoint endPoint, CancellationToken cancell
112120

113121
private async Task ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken)
114122
{
123+
var connected = false;
115124
var cancelled = false;
116125
var timedOut = false;
117126

118-
using (var registration = cancellationToken.Register(() => { cancelled = true; try { socket.Close(); } catch { } }))
119-
using (var timer = new Timer(_ => { timedOut = true; try { socket.Close(); } catch { } }, null, _settings.ConnectTimeout, Timeout.InfiniteTimeSpan))
127+
using (var registration = cancellationToken.Register(() => { if (!connected) { cancelled = true; try { socket.Close(); } catch { } } }))
128+
using (var timer = new Timer(_ => { if (!connected) { timedOut = true; try { socket.Close(); } catch { } } }, null, _settings.ConnectTimeout, Timeout.InfiniteTimeSpan))
120129
{
121130
try
122131
{
@@ -130,6 +139,8 @@ private async Task ConnectAsync(Socket socket, EndPoint endPoint, CancellationTo
130139
{
131140
await Task.Factory.FromAsync(socket.BeginConnect(endPoint, null, null), socket.EndConnect).ConfigureAwait(false);
132141
}
142+
connected = true;
143+
return;
133144
}
134145
catch
135146
{
@@ -140,6 +151,11 @@ private async Task ConnectAsync(Socket socket, EndPoint endPoint, CancellationTo
140151
}
141152
}
142153

154+
if (socket.Connected)
155+
{
156+
try { socket.Close(); } catch { }
157+
}
158+
143159
cancellationToken.ThrowIfCancellationRequested();
144160
if (timedOut)
145161
{

0 commit comments

Comments
 (0)