lapin icon indicating copy to clipboard operation
lapin copied to clipboard

basic_publish hangs upon server shutdown or connection closed by server

Open alloncm opened this issue 1 month ago • 0 comments

Hi, this might be connected to #389 but my issue is that the process hangs instead of returning error on the operation (instead of not reconnecting on shutdown), let me know if you want this issue as another comment in that issue.

I have an example that sometimes get error (as expected) and sometimes just hangs forever.

use lapin::{options::{BasicPublishOptions, ExchangeDeclareOptions}, types::FieldTable, BasicProperties, Connection, ConnectionProperties, Error, ExchangeKind};

const DATA:[u8; 100_000] = [0;100_000];

fn main() -> lapin::Result<()> {
    async_global_executor::block_on(rabbit_main())
}

async fn rabbit_main()->Result<(), Error>{
    let addr:String = "amqp://127.0.0.1:5672/%2f".into();
    let conn = Connection::connect(&addr, ConnectionProperties::default()).await?;
    let channel = conn.create_channel().await?;
    channel.exchange_declare("test", ExchangeKind::Fanout, ExchangeDeclareOptions::default(), FieldTable::default()).await?;

    loop{
        let result = channel.basic_publish("test", "/", BasicPublishOptions::default(), 
            &DATA, BasicProperties::default()).await;
        match result{
            Ok(res) => {
                if res.await?.is_nack() {
                    println!("Error received nack");
                }
                else{
                    println!("Send message succesfully");
                }
            }
            Err(err) => println!("{err}"),
        }
    }
}

The expected behavior (that sometimes happens, mostly in debug builds) is that upon server shutdown Ill receive an error from the basic_publish function (the error I sometimes receive is invalid channel state: Error) and print it.

I'm running rabbitmq with the following docker command - docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.13-management and either terminating the container or closing the connection from the management web UI.

I have also tried it with smol runtime and even with just futures_lite::future::block_on (async_global_executor is used in the example so I assumed it should work with it) in case it is something with the async runtime.

Disclaimer: I'm not really experienced with async rust so I might be missing something very obvious here.

Thanks in advance!

alloncm avatar May 25 '24 17:05 alloncm