diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs index 8d950791269880..1d2830ef845dfb 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs @@ -2173,6 +2173,7 @@ public async Task SendAsync_InvalidRequestUri_Throws() public async Task SendAsync_RequestWithDangerousControlHeaderValue_ThrowsHttpRequestException(char dangerousChar, HeaderType headerType) { TaskCompletionSource acceptConnection = new TaskCompletionSource(); + SemaphoreSlim clientFinished = new SemaphoreSlim(0); await LoopbackServerFactory.CreateClientAndServerAsync(async uri => { @@ -2216,6 +2217,7 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri => // WinHTTP validates the input before opening connection whereas SocketsHttpHandler opens connection first and validates only when writing to the wire. acceptConnection.SetResult(!IsWinHttpHandler); var ex = await Assert.ThrowsAnyAsync(() => client.SendAsync(request)); + clientFinished.Release(); var hrex = Assert.IsType(ex); if (IsWinHttpHandler) { @@ -2231,7 +2233,7 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri => { if (await acceptConnection.Task) { - await IgnoreExceptions(server.AcceptConnectionAsync(c => Task.CompletedTask)); + await IgnoreExceptions(() => server.AcceptConnectionAsync(_ => clientFinished.WaitAsync(TestHelper.PassingTestTimeout))); } }); }