Skip to content

Commit 712b01d

Browse files
authored
Prevent blocking threads (#6190)
- Call StopAsync and StartAsync on the Host and TestServer in more places
1 parent 010c1f0 commit 712b01d

File tree

9 files changed

+105
-33
lines changed

9 files changed

+105
-33
lines changed

src/Servers/Kestrel/test/BindTests/AddressRegistrationTests.cs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ private async Task RegisterIPEndPoint_Success(IPEndPoint endPoint, string testUr
297297

298298
using (var host = hostBuilder.Build())
299299
{
300-
host.Start();
300+
await host.StartAsync();
301301

302302
var testUrlWithPort = $"{testUrl}:{(testPort == 0 ? host.GetPort() : testPort)}";
303303

@@ -309,6 +309,8 @@ private async Task RegisterIPEndPoint_Success(IPEndPoint endPoint, string testUr
309309
// Compare the response with Uri.ToString(), rather than testUrl directly.
310310
// Required to handle IPv6 addresses with zone index, like "fe80::3%1"
311311
Assert.Equal(new Uri(testUrlWithPort).ToString(), response);
312+
313+
await host.StopAsync();
312314
}
313315
}
314316

@@ -348,7 +350,7 @@ private async Task ListenAnyIP_Success(string[] testUrls, int testPort = 0)
348350

349351
using (var host = hostBuilder.Build())
350352
{
351-
host.Start();
353+
await host.StartAsync();
352354

353355
foreach (var testUrl in testUrls.Select(testUrl => $"{testUrl}:{(testPort == 0 ? host.GetPort() : testPort)}"))
354356
{
@@ -358,6 +360,8 @@ private async Task ListenAnyIP_Success(string[] testUrls, int testPort = 0)
358360
// Required to handle IPv6 addresses with zone index, like "fe80::3%1"
359361
Assert.Equal(new Uri(testUrl).ToString(), response);
360362
}
363+
364+
await host.StopAsync();
361365
}
362366
}
363367

@@ -389,7 +393,7 @@ private async Task ListenLocalhost_Success(string[] testUrls, int testPort = 0)
389393

390394
using (var host = hostBuilder.Build())
391395
{
392-
host.Start();
396+
await host.StartAsync();
393397

394398
foreach (var testUrl in testUrls.Select(testUrl => $"{testUrl}:{(testPort == 0 ? host.GetPort() : testPort)}"))
395399
{
@@ -399,6 +403,8 @@ private async Task ListenLocalhost_Success(string[] testUrls, int testPort = 0)
399403
// Required to handle IPv6 addresses with zone index, like "fe80::3%1"
400404
Assert.Equal(new Uri(testUrl).ToString(), response);
401405
}
406+
407+
await host.StopAsync();
402408
}
403409
}
404410

@@ -468,7 +474,7 @@ private async Task RegisterDefaultServerAddresses_Success(IEnumerable<string> ad
468474

469475
using (var host = hostBuilder.Build())
470476
{
471-
host.Start();
477+
await host.StartAsync();
472478

473479
Assert.Equal(5000, host.GetPort());
474480

@@ -485,6 +491,8 @@ private async Task RegisterDefaultServerAddresses_Success(IEnumerable<string> ad
485491
{
486492
Assert.Equal(new Uri(address).ToString(), await HttpClientSlim.GetStringAsync(address, validateCertificate: false));
487493
}
494+
495+
await host.StopAsync();
488496
}
489497
}
490498

@@ -555,7 +563,7 @@ public async Task OverrideDirectConfigurationWithIServerAddressesFeature_Succeed
555563

556564
using (var host = hostBuilder.Build())
557565
{
558-
host.Start();
566+
await host.StartAsync();
559567

560568
var port = host.GetPort();
561569

@@ -571,6 +579,8 @@ public async Task OverrideDirectConfigurationWithIServerAddressesFeature_Succeed
571579
log.Message, StringComparison.Ordinal));
572580

573581
Assert.Equal(new Uri(useUrlsAddressWithPort).ToString(), await HttpClientSlim.GetStringAsync(useUrlsAddressWithPort));
582+
583+
await host.StopAsync();
574584
}
575585
}
576586

@@ -594,7 +604,7 @@ public async Task DoesNotOverrideDirectConfigurationWithIServerAddressesFeature_
594604

595605
using (var host = hostBuilder.Build())
596606
{
597-
host.Start();
607+
await host.StartAsync();
598608

599609
var port = host.GetPort();
600610

@@ -610,6 +620,7 @@ public async Task DoesNotOverrideDirectConfigurationWithIServerAddressesFeature_
610620
log.Message, StringComparison.Ordinal));
611621

612622
Assert.Equal(new Uri(endPointAddress).ToString(), await HttpClientSlim.GetStringAsync(endPointAddress, validateCertificate: false));
623+
await host.StopAsync();
613624
}
614625
}
615626

@@ -630,7 +641,7 @@ public async Task DoesNotOverrideDirectConfigurationWithIServerAddressesFeature_
630641

631642
using (var host = hostBuilder.Build())
632643
{
633-
host.Start();
644+
await host.StartAsync();
634645

635646
var port = host.GetPort();
636647

@@ -641,6 +652,8 @@ public async Task DoesNotOverrideDirectConfigurationWithIServerAddressesFeature_
641652
Assert.Equal(serverAddresses.First(), endPointAddress);
642653

643654
Assert.Equal(new Uri(endPointAddress).ToString(), await HttpClientSlim.GetStringAsync(endPointAddress, validateCertificate: false));
655+
656+
await host.StopAsync();
644657
}
645658
}
646659

@@ -709,9 +722,11 @@ public async Task CanRebindToEndPoint()
709722

710723
using (var host = hostBuilder.Build())
711724
{
712-
host.Start();
725+
await host.StartAsync();
713726

714727
Assert.Equal(endPointAddress, await HttpClientSlim.GetStringAsync(endPointAddress));
728+
729+
await host.StopAsync();
715730
}
716731

717732
hostBuilder = TransportSelector.GetWebHostBuilder()
@@ -723,9 +738,11 @@ public async Task CanRebindToEndPoint()
723738

724739
using (var host = hostBuilder.Build())
725740
{
726-
host.Start();
741+
await host.StartAsync();
727742

728743
Assert.Equal(endPointAddress, await HttpClientSlim.GetStringAsync(endPointAddress));
744+
745+
await host.StopAsync();
729746
}
730747
}
731748

@@ -748,10 +765,12 @@ public async Task CanRebindToMultipleEndPoints()
748765

749766
using (var host = hostBuilder.Build())
750767
{
751-
host.Start();
768+
await host.StartAsync();
752769

753770
Assert.Equal(ipv4endPointAddress, await HttpClientSlim.GetStringAsync(ipv4endPointAddress));
754771
Assert.Equal(ipv6endPointAddress, await HttpClientSlim.GetStringAsync(ipv6endPointAddress));
772+
773+
await host.StopAsync();
755774
}
756775

757776
hostBuilder = TransportSelector.GetWebHostBuilder()
@@ -764,18 +783,20 @@ public async Task CanRebindToMultipleEndPoints()
764783

765784
using (var host = hostBuilder.Build())
766785
{
767-
host.Start();
786+
await host.StartAsync();
768787

769788
Assert.Equal(ipv4endPointAddress, await HttpClientSlim.GetStringAsync(ipv4endPointAddress));
770789
Assert.Equal(ipv6endPointAddress, await HttpClientSlim.GetStringAsync(ipv6endPointAddress));
790+
791+
await host.StopAsync();
771792
}
772793
}
773794

774795
[Theory]
775796
[InlineData("http1", HttpProtocols.Http1)]
776797
[InlineData("http2", HttpProtocols.Http2)]
777798
[InlineData("http1AndHttp2", HttpProtocols.Http1AndHttp2)]
778-
public void EndpointDefaultsConfig_CanSetProtocolForUrlsConfig(string input, HttpProtocols expected)
799+
public async Task EndpointDefaultsConfig_CanSetProtocolForUrlsConfig(string input, HttpProtocols expected)
779800
{
780801
KestrelServerOptions capturedOptions = null;
781802
var hostBuilder = TransportSelector.GetWebHostBuilder()
@@ -795,9 +816,10 @@ public void EndpointDefaultsConfig_CanSetProtocolForUrlsConfig(string input, Htt
795816

796817
using (var host = hostBuilder.Build())
797818
{
798-
host.Start();
819+
await host.StartAsync();
799820
Assert.Single(capturedOptions.ListenOptions);
800821
Assert.Equal(expected, capturedOptions.ListenOptions[0].Protocols);
822+
await host.StopAsync();
801823
}
802824
}
803825

src/Servers/Kestrel/test/FunctionalTests/ConnectionAdapterTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ await connection.Send(
4444
}
4545
});
4646
}
47+
await server.StopAsync();
4748
}
4849
}
4950

src/Servers/Kestrel/test/FunctionalTests/Http2/HandshakeTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public async Task TlsAlpnHandshakeSelectsHttp2From1and2()
6363
{
6464
var result = await Client.GetStringAsync($"https://localhost:{server.Port}/");
6565
Assert.Equal("hello world HTTP/2", result);
66+
67+
await server.StopAsync();
6668
}
6769
}
6870

@@ -89,6 +91,7 @@ public async Task TlsAlpnHandshakeSelectsHttp2()
8991
{
9092
var result = await Client.GetStringAsync($"https://localhost:{server.Port}/");
9193
Assert.Equal("hello world HTTP/2", result);
94+
await server.StopAsync();
9295
}
9396
}
9497
}

src/Servers/Kestrel/test/FunctionalTests/MaxRequestBufferSizeTests.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -132,7 +132,7 @@ public async Task LargeUpload(long? maxRequestBufferSize, bool connectionAdapter
132132

133133
var memoryPoolFactory = new DiagnosticMemoryPoolFactory(allowLateReturn: true);
134134

135-
using (var host = StartWebHost(maxRequestBufferSize, data, connectionAdapter, startReadingRequestBody, clientFinishedSendingRequestBody, memoryPoolFactory.Create))
135+
using (var host = await StartWebHost(maxRequestBufferSize, data, connectionAdapter, startReadingRequestBody, clientFinishedSendingRequestBody, memoryPoolFactory.Create))
136136
{
137137
var port = host.GetPort();
138138
using (var socket = CreateSocket(port))
@@ -204,6 +204,7 @@ public async Task LargeUpload(long? maxRequestBufferSize, bool connectionAdapter
204204

205205
await AssertStreamContains(stream, $"bytesRead: {data.Length}");
206206
}
207+
await host.StopAsync();
207208
}
208209

209210
await memoryPoolFactory.WhenAllBlocksReturned(TestConstants.DefaultTimeout);
@@ -224,7 +225,7 @@ public async Task ServerShutsDownGracefullyWhenMaxRequestBufferSizeExceeded()
224225

225226
var memoryPoolFactory = new DiagnosticMemoryPoolFactory(allowLateReturn: true);
226227

227-
using (var host = StartWebHost(16 * 1024, data, false, startReadingRequestBody, clientFinishedSendingRequestBody, memoryPoolFactory.Create))
228+
using (var host = await StartWebHost(16 * 1024, data, false, startReadingRequestBody, clientFinishedSendingRequestBody, memoryPoolFactory.Create))
228229
{
229230
var port = host.GetPort();
230231
using (var socket = CreateSocket(port))
@@ -278,6 +279,7 @@ public async Task ServerShutsDownGracefullyWhenMaxRequestBufferSizeExceeded()
278279

279280
// Dispose host prior to closing connection to verify the server doesn't throw during shutdown
280281
// if a connection no longer has alloc and read callbacks configured.
282+
await host.StopAsync();
281283
host.Dispose();
282284
}
283285
}
@@ -287,7 +289,7 @@ public async Task ServerShutsDownGracefullyWhenMaxRequestBufferSizeExceeded()
287289
await memoryPoolFactory.WhenAllBlocksReturned(TestConstants.DefaultTimeout);
288290
}
289291

290-
private IWebHost StartWebHost(long? maxRequestBufferSize,
292+
private async Task<IWebHost> StartWebHost(long? maxRequestBufferSize,
291293
byte[] expectedBody,
292294
bool useConnectionAdapter,
293295
TaskCompletionSource<object> startReadingRequestBody,
@@ -350,7 +352,7 @@ private IWebHost StartWebHost(long? maxRequestBufferSize,
350352
}))
351353
.Build();
352354

353-
host.Start();
355+
await host.StartAsync();
354356

355357
return host;
356358
}

0 commit comments

Comments
 (0)