@@ -290,7 +290,7 @@ internal Task<TcpServerConnection> GetServerConnection(ProxyServer proxyServer,
290290 bool prefetch , CancellationToken cancellationToken )
291291 {
292292 // deny connection to proxy end points to avoid infinite connection loop.
293- if ( Server . ProxyEndPoints . Any ( x => x . Port == remotePort )
293+ if ( Server . ProxyEndPoints . Any ( x => x . Port == remotePort )
294294 && NetworkHelper . IsLocalIpAddress ( remoteHostName ) )
295295 {
296296 throw new Exception ( $ "A client is making HTTP request to one of the listening ports of this proxy { remoteHostName } :{ remotePort } ") ;
@@ -413,7 +413,7 @@ internal Task<TcpServerConnection> GetServerConnection(ProxyServer proxyServer,
413413 }
414414
415415 Task connectTask ;
416-
416+
417417 if ( socks )
418418 {
419419 if ( externalProxy ! . ProxyDnsRequests )
@@ -629,6 +629,16 @@ internal Task<TcpServerConnection> GetServerConnection(ProxyServer proxyServer,
629629 /// <param name="close">Should we just close the connection instead of reusing?</param>
630630 internal async Task Release ( TcpServerConnection connection , bool close = false )
631631 {
632+ if ( connection == null )
633+ {
634+ return ;
635+ }
636+
637+ if ( disposalBag . Any ( x => x == connection ) )
638+ {
639+ return ;
640+ }
641+
632642 if ( close || connection . IsWinAuthenticated || ! Server . EnableConnectionPool || connection . IsClosed )
633643 {
634644 disposalBag . Add ( connection ) ;
@@ -653,6 +663,11 @@ internal async Task Release(TcpServerConnection connection, bool close = false)
653663 }
654664 }
655665
666+ if ( existingConnections . Any ( x => x == connection ) )
667+ {
668+ break ;
669+ }
670+
656671 existingConnections . Enqueue ( connection ) ;
657672 break ;
658673 }
0 commit comments