Azure / amqpnetlite

AMQP 1.0 .NET Library
Apache License 2.0
401 stars 143 forks source link

If creating SenderLink after ReceiverLink then unable to receive message #487

Closed vedion closed 2 years ago

vedion commented 2 years ago

Hi,

I have a problem when creating a SenderLink after the first ReceiverLink.

Steps: 1) Create first "receiverConnection", "receiverSession" and "receiverLink" 2) Create "senderConnection", "senderSession" and "senderLink" 3) Send message 4) Receive message 5) Close first "receiverConnection", "receiverSession" and "receiverLink" 6) Create second "receiverConnection", "receiverSession" and "receiverLink" 7) Send message 8) Receive message (THIS WILL FAIL)

If you swap 1) and 2) then it wont fail.

When testing with another broker (EDX toolbox) then it works fine.

This must be a bug?

Best Regards, Anders Havn

` using Amqp;

string credentials = "guest:guest"; string host = "localhost:5672"; string connectionString = "amqp://" + credentials + "@" + host;

Address address = new Address(connectionString); ConnectionFactory connectionFactory = new ConnectionFactory();

// Works when creating SenderLink before first ReceiverLink //Connection senderConnection = await connectionFactory.CreateAsync(address); //Session senderSession = new Session(senderConnection); //SenderLink senderLink = new SenderLink(senderSession, "sender-link", "testQueue");

var senderMessage = "test";

// Creating first ReceiverLink Connection firstReceiverConnection = await connectionFactory.CreateAsync(address); Session firstReceiverSession = new Session(firstReceiverConnection); ReceiverLink firstReceiverLink = new ReceiverLink(firstReceiverSession, "receiver-link", "testQueue"); Console.WriteLine("First receiver connected to broker.");

// Does not work when creating SenderLink after first ReceiverLink var senderConnection = await connectionFactory.CreateAsync(address); var senderSession = new Session(senderConnection); var senderLink = new SenderLink(senderSession, "sender-link", "testQueue");

// Send and receive message await senderLink.SendAsync(new Message(senderMessage)); Message firstMessageReceived = await firstReceiverLink.ReceiveAsync(TimeSpan.FromMilliseconds(1000)); Console.WriteLine($"First message received: {firstMessageReceived.Body}");

// Close first reveiver link await firstReceiverLink.CloseAsync(); await firstReceiverSession.CloseAsync(); await firstReceiverConnection.CloseAsync();

// Creating second ReceiverLink Connection secondReceiverConnection = await connectionFactory.CreateAsync(address); Session secondReceiverSession = new Session(secondReceiverConnection); ReceiverLink secondReceiverLink = new ReceiverLink(secondReceiverSession, "receiver-link", "testQueue"); Console.WriteLine("Second receiver connected to broker.");

// Send and receive message await senderLink.SendAsync(new Message(senderMessage)); Message message = await secondReceiverLink.ReceiveAsync(TimeSpan.FromMilliseconds(1000)); if (message == null) { Console.WriteLine("No message received"); } else { Console.WriteLine($"Second message received: {message.Body}"); secondReceiverLink.Accept(message); }

// Close second reveiver link await secondReceiverLink.CloseAsync(); await secondReceiverSession.CloseAsync(); await secondReceiverConnection.CloseAsync();

// Close sender link await senderLink.CloseAsync(); await senderSession.CloseAsync(); await senderConnection.CloseAsync(); `

xinchen10 commented 2 years ago

Which broker does not work for the above code?

vedion commented 2 years ago

TestAmqpBroker.exe https://github.com/Azure/amqpnetlite/blob/master/docs/articles/test_amqp_broker.md

On 9 Feb 2022, at 22.26, Xin Chen @.***> wrote:

 Which broker does not work for the above code?

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you authored the thread.

xinchen10 commented 2 years ago

Did you start the broker with the "/queues" option? With this option, your code works regardless of the sender/receiver order.

Without this option the test broker creates an implicit queue for the first link attach request and deletes that queue when that link's connection is closed. Obviously there is a bug when you have multiple links/connections for the same address. The order of the link creation affects when the queue is deleted. We can fix this issue by removing the queue when the last link detaches.

vedion commented 2 years ago

Hi again,

Thank you very much for your quick responses! I have not tested with "/queues" because we don't know the queue names upfront when starting the broker :/ How easy is it to fix: "We can fix this issue by removing the queue when the last link detaches." And do you have a guess when that will be fixed?

Best Regards, Anders Havn

vedion commented 2 years ago

Thank you for the quick fix!