Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 5e5225f

Browse files
committed
#826 Add read race protection for connection cache
1 parent 845b513 commit 5e5225f

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/Titanium.Web.Proxy/Network/TcpConnection/TcpConnectionFactory.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,22 @@ internal Task<TcpServerConnection> GetServerConnection(ProxyServer proxyServer,
242242
{
243243
if (cache.TryGetValue(cacheKey, out var existingConnections))
244244
{
245-
// +3 seconds for potential delay after getting connection
246-
var cutOff = DateTime.UtcNow.AddSeconds(-proxyServer.ConnectionTimeOutSeconds + 3);
247-
while (existingConnections.Count > 0)
245+
lock (existingConnections)
248246
{
249-
if (existingConnections.TryDequeue(out var recentConnection))
247+
// +3 seconds for potential delay after getting connection
248+
var cutOff = DateTime.UtcNow.AddSeconds(-proxyServer.ConnectionTimeOutSeconds + 3);
249+
while (existingConnections.Count > 0)
250250
{
251-
if (recentConnection.LastAccess > cutOff
252-
&& recentConnection.TcpSocket.IsGoodConnection())
251+
if (existingConnections.TryDequeue(out var recentConnection))
253252
{
254-
return recentConnection;
255-
}
253+
if (recentConnection.LastAccess > cutOff
254+
&& recentConnection.TcpSocket.IsGoodConnection())
255+
{
256+
return recentConnection;
257+
}
256258

257-
disposalBag.Add(recentConnection);
259+
disposalBag.Add(recentConnection);
260+
}
258261
}
259262
}
260263
}

0 commit comments

Comments
 (0)