|
17 | 17 | #endregion
|
18 | 18 |
|
19 | 19 | #if SUPPORT_LOAD_BALANCING
|
| 20 | +using System.Diagnostics; |
20 | 21 | using System.Net;
|
21 | 22 | using System.Threading.Channels;
|
22 | 23 | using Greet;
|
|
29 | 30 | using Grpc.Tests.Shared;
|
30 | 31 | using Microsoft.Extensions.DependencyInjection;
|
31 | 32 | using Microsoft.Extensions.Logging;
|
| 33 | +using Microsoft.Extensions.Logging.Abstractions; |
32 | 34 | using Microsoft.Extensions.Logging.Testing;
|
33 | 35 | using NUnit.Framework;
|
34 | 36 | using ChannelState = Grpc.Net.Client.Balancer.ChannelState;
|
@@ -535,6 +537,89 @@ public async Task PickAsync_DoesNotDeadlockAfterReconnect_WithZeroAddressResolve
|
535 | 537 | await pickTask.DefaultTimeout();
|
536 | 538 | }
|
537 | 539 |
|
| 540 | + [Test] |
| 541 | + public async Task PickAsync_UpdateAddressesWhileRequestingConnection_DoesNotDeadlock() |
| 542 | + { |
| 543 | + var services = new ServiceCollection(); |
| 544 | + services.AddNUnitLogger(); |
| 545 | + |
| 546 | + var testSink = new TestSink(); |
| 547 | + var testProvider = new TestLoggerProvider(testSink); |
| 548 | + |
| 549 | + services.AddLogging(b => |
| 550 | + { |
| 551 | + b.AddProvider(testProvider); |
| 552 | + }); |
| 553 | + |
| 554 | + await using var serviceProvider = services.BuildServiceProvider(); |
| 555 | + var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>(); |
| 556 | + |
| 557 | + var resolver = new TestResolver(loggerFactory); |
| 558 | + resolver.UpdateAddresses(new List<BalancerAddress> |
| 559 | + { |
| 560 | + new BalancerAddress("localhost", 80) |
| 561 | + }); |
| 562 | + |
| 563 | + var channelOptions = new GrpcChannelOptions(); |
| 564 | + |
| 565 | + var transportFactory = new TestSubchannelTransportFactory(); |
| 566 | + var clientChannel = CreateConnectionManager(loggerFactory, resolver, transportFactory, new[] { new PickFirstBalancerFactory() }); |
| 567 | + // Configure balancer similar to how GrpcChannel constructor does it |
| 568 | + clientChannel.ConfigureBalancer(c => new ChildHandlerLoadBalancer( |
| 569 | + c, |
| 570 | + channelOptions.ServiceConfig, |
| 571 | + clientChannel)); |
| 572 | + |
| 573 | + await clientChannel.ConnectAsync(waitForReady: true, cancellationToken: CancellationToken.None); |
| 574 | + |
| 575 | + transportFactory.Transports.ForEach(t => t.Disconnect()); |
| 576 | + |
| 577 | + var requestConnectionSyncPoint = new SyncPoint(runContinuationsAsynchronously: true); |
| 578 | + testSink.MessageLogged += (w) => |
| 579 | + { |
| 580 | + if (w.EventId.Name == "ConnectionRequested") |
| 581 | + { |
| 582 | + requestConnectionSyncPoint.WaitToContinue().Wait(); |
| 583 | + } |
| 584 | + }; |
| 585 | + |
| 586 | + // Task should pause when requesting connection because of the logger sink. |
| 587 | + var pickTask = Task.Run(() => clientChannel.PickAsync( |
| 588 | + new PickContext { Request = new HttpRequestMessage() }, |
| 589 | + waitForReady: true, |
| 590 | + CancellationToken.None).AsTask()); |
| 591 | + |
| 592 | + // Wait until we're paused on requesting a connection. |
| 593 | + await requestConnectionSyncPoint.WaitForSyncPoint().DefaultTimeout(); |
| 594 | + |
| 595 | + // Update addresses while requesting a connection. |
| 596 | + var updateAddressesTcs = new TaskCompletionSource<object?>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 597 | + var updateAddressesTask = Task.Run(() => |
| 598 | + { |
| 599 | + updateAddressesTcs.TrySetResult(null); |
| 600 | + resolver.UpdateAddresses(new List<BalancerAddress> |
| 601 | + { |
| 602 | + new BalancerAddress("localhost", 81) |
| 603 | + }); |
| 604 | + }); |
| 605 | + |
| 606 | + // There isn't a clean way to wait for UpdateAddresses to be waiting for the subchannel lock. |
| 607 | + // Use a long delay to ensure we're waiting for the lock and are in the right state. |
| 608 | + await updateAddressesTcs.Task.DefaultTimeout(); |
| 609 | + await Task.Delay(500); |
| 610 | + requestConnectionSyncPoint.Continue(); |
| 611 | + |
| 612 | + // Ensure the pick completes without deadlock. |
| 613 | + try |
| 614 | + { |
| 615 | + await pickTask.DefaultTimeout(); |
| 616 | + } |
| 617 | + catch (TimeoutException ex) |
| 618 | + { |
| 619 | + throw new InvalidOperationException("Likely deadlock when picking subchannel.", ex); |
| 620 | + } |
| 621 | + } |
| 622 | + |
538 | 623 | [Test]
|
539 | 624 | public async Task PickAsync_ExecutionContext_DoesNotCaptureAsyncLocalsInConnect()
|
540 | 625 | {
|
|
0 commit comments