rabbitmq / rabbitmq-dotnet-client

RabbitMQ .NET client for .NET Standard 2.0+ and .NET 4.6.2+
https://www.rabbitmq.com/dotnet.html
Other
2.06k stars 574 forks source link

ConnectionFactory Authmechanisms has incorrect sharing between instances #1370

Closed evolvedlight closed 11 months ago

evolvedlight commented 11 months ago

Describe the bug

if you connect to two RabbitMq instances, one with EXTERNAL auth, one plain, and change the Auth Mechanisms, the instances change each other.

Reproduction steps

  1. Create new project with rabbitmq client dependency (latest version) Paste the following:
ConnectionFactory factory = new ConnectionFactory();
factory.AuthMechanisms.Clear();
ConnectionFactory factory2 = new ConnectionFactory();
Console.WriteLine(factory2.AuthMechanisms.Count);

Expected behavior

I expect to have a single Plain Auth mechanism Instead I have Zero auth mechanisms

Additional context

It's because the DefaultAuthMechanisms is referenced by reference rather than making a copy of it. This bug is triggered by MassTransit: https://github.com/MassTransit/MassTransit/blob/15c44ca0c26e9dfcedbdf3e770621c5f3ceb6975/src/Transports/MassTransit.RabbitMqTransport/RabbitMqTransport/RabbitMqAddressExtensions.cs#L65

evolvedlight commented 11 months ago

I plan to open a PR for this on the weekend, let me know if you don't consider this a bug.

lukebakken commented 11 months ago

Good catch! Thank you for reporting this. A PR is very welcome. If you'd like to base it on the 6.x branch I will then forward-merge your changes. main has diverged quite a bit.