rabbitmq-dotnet-client icon indicating copy to clipboard operation
rabbitmq-dotnet-client copied to clipboard

IModel.IsClosed set to false after dispose

Open eduard-bystrov opened this issue 4 years ago • 1 comments

Example:

public static async Task Main(string[] args)
{
	var factory = new ConnectionFactory() { HostName = "localhost" };
	using var connection = factory.CreateConnection();

	var channel = connection.CreateModel();

	channel.QueueDeclare(queue: "task_queue", exclusive: false, autoDelete: false);

	var message = "Hello";
	var body = Encoding.UTF8.GetBytes(message);

	// restart rmq service here
	while (true)
	{
		try
		{
			var properties = channel.CreateBasicProperties();
			channel.BasicPublish(exchange: "", routingKey: "task_queue", basicProperties: properties, body: body);
			Console.WriteLine(" [x] Sent {0}", message);
		}
		catch (Exception ex)
		{
			Console.WriteLine(ex.ToString());
			break;
		}

		await Task.Delay(1000);
	}

	Console.WriteLine($"IsClosed:{channel.IsClosed}");
	channel.Dispose();
	Console.WriteLine($"IsClosed:{channel.IsClosed}");
}

// [x] Sent Hello
//  ...
// RabbitMQ.Client.Exceptions.AlreadyClosedException: Already closed: The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=320, text='CONNECTION_FORCED - broker forced connection closure // // with reason 'shutdown'', classId=0, methodId=0
//    at RabbitMQ.Client.Impl.SessionBase.Transmit(OutgoingCommand& cmd)
//    at RabbitMQ.Client.Impl.ModelBase.ModelSend(MethodBase method, ContentHeaderBase header, ReadOnlyMemory`1 body)
//    at RabbitMQ.Client.Framing.Impl.Model._Private_BasicPublish(String exchange, String routingKey, Boolean mandatory, IBasicProperties basicProperties, ReadOnlyMemory`1 body)
//    at RabbitMQ.Client.Impl.ModelBase.BasicPublish(String exchange, String routingKey, Boolean mandatory, IBasicProperties basicProperties, ReadOnlyMemory`1 body)
//    at RabbitMQ.Client.Impl.AutorecoveringModel.BasicPublish(String exchange, String routingKey, Boolean mandatory, IBasicProperties basicProperties, ReadOnlyMemory`1 body)
// IsClosed:True
// IsClosed:False

Is this the correct behavior?

eduard-bystrov avatar Sep 17 '21 15:09 eduard-bystrov

You are supposed to close the channel before disposing it but I guess as a usability improvement we can update the state, too.

According to the stack trace the node was forced to shut down, so there may be connection recovery side effects at play.

michaelklishin avatar Sep 17 '21 15:09 michaelklishin