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

Commit 649ddbc

Browse files
authored
Dispose of the SocketsHttpHandler used in WebSocketHandle.Managed (#27900)
1 parent 70e85dc commit 649ddbc

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string statusDesc
7171
public async Task ConnectAsyncCore(Uri uri, CancellationToken cancellationToken, ClientWebSocketOptions options)
7272
{
7373
HttpResponseMessage response = null;
74+
SocketsHttpHandler handler = null;
7475
try
7576
{
7677
// Create the request message, including a uri with ws{s} switched to http{s}.
@@ -89,7 +90,7 @@ public async Task ConnectAsyncCore(Uri uri, CancellationToken cancellationToken,
8990
AddWebSocketHeaders(request, secKeyAndSecWebSocketAccept.Key, options);
9091

9192
// Create the handler for this request and populate it with all of the options.
92-
var handler = new SocketsHttpHandler();
93+
handler = new SocketsHttpHandler();
9394
handler.Credentials = options.Credentials;
9495
handler.Proxy = options.Proxy;
9596
handler.CookieContainer = options.Cookies;
@@ -189,6 +190,11 @@ public async Task ConnectAsyncCore(Uri uri, CancellationToken cancellationToken,
189190
}
190191
throw new WebSocketException(SR.net_webstatus_ConnectFailure, exc);
191192
}
193+
finally
194+
{
195+
// Disposing the handler will not affect any active stream wrapped in the WebSocket.
196+
handler?.Dispose();
197+
}
192198
}
193199

194200
/// <param name="secKey">The generated security key to send in the Sec-WebSocket-Key header.</param>

0 commit comments

Comments
 (0)