|
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,186 @@ 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 | + |
| 623 | + [Test] |
| 624 | + public async Task PickAsync_ResolverUpdating_DoesNotDeadlock() |
| 625 | + { |
| 626 | + // Arrange |
| 627 | + var services = new ServiceCollection(); |
| 628 | + //services.AddNUnitLogger(); |
| 629 | + await using var serviceProvider = services.BuildServiceProvider(); |
| 630 | + var loggerFactory = NullLoggerFactory.Instance;// serviceProvider.GetRequiredService<ILoggerFactory>(); |
| 631 | + |
| 632 | + var resolver = new TestResolver(loggerFactory); |
| 633 | + |
| 634 | + var channelOptions = new GrpcChannelOptions(); |
| 635 | + |
| 636 | + var transportFactory = new TestSubchannelTransportFactory(); |
| 637 | + var clientChannel = CreateConnectionManager(loggerFactory, resolver, transportFactory, new[] { new PickFirstBalancerFactory() }); |
| 638 | + // Configure balancer similar to how GrpcChannel constructor does it |
| 639 | + clientChannel.ConfigureBalancer(c => new ChildHandlerLoadBalancer( |
| 640 | + c, |
| 641 | + channelOptions.ServiceConfig, |
| 642 | + clientChannel)); |
| 643 | + |
| 644 | + // Act |
| 645 | + var connectTask = clientChannel.ConnectAsync(waitForReady: true, cancellationToken: CancellationToken.None); |
| 646 | + |
| 647 | + var t = Task.Run(async () => |
| 648 | + { |
| 649 | + while (true) |
| 650 | + { |
| 651 | + await Task.Delay(1000); |
| 652 | + } |
| 653 | + }); |
| 654 | + |
| 655 | + // continiously update addresses |
| 656 | + _ = Task.Run(async () => |
| 657 | + { |
| 658 | + var a1 = new List<BalancerAddress> |
| 659 | + { |
| 660 | + new BalancerAddress("localhost", 80) |
| 661 | + }; |
| 662 | + var a2 = new List<BalancerAddress> |
| 663 | + { |
| 664 | + new BalancerAddress("localhost", 81) |
| 665 | + }; |
| 666 | + var current = a1; |
| 667 | + |
| 668 | + var count = 0; |
| 669 | + while (true) |
| 670 | + { |
| 671 | + current = count % 2 == 0 ? a1 : a2; |
| 672 | + //if (count > 10_000) |
| 673 | + //{ |
| 674 | + // updateAddressesTcs.TrySetResult(null); |
| 675 | + //} |
| 676 | + |
| 677 | + resolver.UpdateAddresses(current); |
| 678 | + count++; |
| 679 | + |
| 680 | + //updateAddressChannel.Writer.TryWrite(true); |
| 681 | + |
| 682 | + await Task.Delay(1); |
| 683 | + } |
| 684 | + }); |
| 685 | + |
| 686 | + // Simulate transport/network issue (with resolver reporting no addresses) |
| 687 | + //transportFactory.Transports.ForEach(t => t.Disconnect()); |
| 688 | + |
| 689 | + try |
| 690 | + { |
| 691 | + for (int i = 0; i < 10_000; i++) |
| 692 | + { |
| 693 | + |
| 694 | + var pickTask = clientChannel.PickAsync( |
| 695 | + new PickContext { Request = new HttpRequestMessage() }, |
| 696 | + waitForReady: true, |
| 697 | + CancellationToken.None).AsTask(); |
| 698 | + //resolver.UpdateAddresses(new List<BalancerAddress> |
| 699 | + //{ |
| 700 | + // new BalancerAddress("localhost", 80) |
| 701 | + //}); |
| 702 | + |
| 703 | + // Assert |
| 704 | + // Should not timeout (deadlock) |
| 705 | + await pickTask.DefaultTimeout(); |
| 706 | + await Task.Delay(1); |
| 707 | + } |
| 708 | + |
| 709 | + } |
| 710 | + catch (Exception ex) |
| 711 | + { |
| 712 | + _ = ex; |
| 713 | + Debugger.Launch(); |
| 714 | + throw; |
| 715 | + } |
| 716 | + |
| 717 | + await t; |
| 718 | + } |
| 719 | + |
538 | 720 | [Test] |
539 | 721 | public async Task PickAsync_ExecutionContext_DoesNotCaptureAsyncLocalsInConnect() |
540 | 722 | { |
|
0 commit comments