Amqp: Exception (504) Reason: "channel/connection is not open"

Created on 24 Jan 2014  路  12Comments  路  Source: streadway/amqp

when I publish message, the code return error that Exception (504) Reason: "channel/connection is not open". what is the error for?

Most helpful comment

Hi folks,

I have the following code...

// Connect - Connects to RabbitMQ
func (queue *Queue) Connect(args ...interface{}) {

    var uri string

    if args == nil {

        // Get from env vars
        uri = os.Getenv("RABBIT_MQ_URI")

        if uri == "" {
            log.Panic("No uri for queue given")
        }
    } else {
        uri = args[0].(string)
    }

    // Make max 5 connection attempts, with a 1 second timeout
    for i := 0; i < 5; i++ {

        log.Println("Connecting to:", uri)

        // If connection is successful, return new instance
        conn, err := amqp.Dial(uri)

        if err == nil {
            log.Println("Successfully connected to queue!")
            channel, _ := conn.Channel()
            queue.Connection = conn
            queue.Channel = channel
            return
        }

        log.Println("Failed to connect to queue, retrying...", err)

        // Wait 1 second
        time.Sleep(5 * time.Second)
    }
}

// Declare a new queue
func (queue *Queue) Declare(queueName string) (amqp.Queue, error) {
    return queue.Channel.QueueDeclare(
        queueName,
        true,
        false,
        false,
        false,
        nil,
    )
}

// Publish a message
func (queue *Queue) Publish(queueName string, payload []byte) error {
    return queue.Channel.Publish(
        "",
        queueName,
        false,
        false,
        amqp.Publishing{
            DeliveryMode: amqp.Persistent,
            ContentType:  "application/json",
            Body:         payload,
        },
    )
}

// Listen for a new message
func (queue *Queue) Listen(queueName string) (<-chan amqp.Delivery, error) {
    return queue.Channel.Consume(
        queueName,
        "",
        true,
        false,
        false,
        false,
        nil,
    )
}

Then in my main.go

        // Get queue connection
    queue := drivers.NewQueue()
    queue.Connect()

        // Declare new queue for submissions 'stuff'
    _, err = queue.Declare("submissions_queue")

    if err != nil {
        log.Panic(err)
    }

    // We need to declare the responses queue even though we're not actually
    // going to use it on the bot side as an emitter.
    _, err = queue.Declare("submissions_responses")

    // Log out any queue errors
    if err != nil {
        log.Panic(err)
    }

These queue then interact with a legacy PHP application (Laravel). Which runs perfectly for a day or two, then after about a day, sometimes less, sometimes longer, I start getting this Exception (504) Reason: "channel/connection is not open". Any clues? Am I doing something dumb?

Thanks in advance!

All 12 comments

Likely the channel you are using is closed, explicitly or due to a channel exception. Inspect RabbitMQ log to find out more.

@eimugray if you provide a reproduction case, then we can help. The most common error is that you have tried to declare a queue or exchange differently than what currently exists in the server.

Like Michael says, check the server logs.

Hi folks,

I have the following code...

// Connect - Connects to RabbitMQ
func (queue *Queue) Connect(args ...interface{}) {

    var uri string

    if args == nil {

        // Get from env vars
        uri = os.Getenv("RABBIT_MQ_URI")

        if uri == "" {
            log.Panic("No uri for queue given")
        }
    } else {
        uri = args[0].(string)
    }

    // Make max 5 connection attempts, with a 1 second timeout
    for i := 0; i < 5; i++ {

        log.Println("Connecting to:", uri)

        // If connection is successful, return new instance
        conn, err := amqp.Dial(uri)

        if err == nil {
            log.Println("Successfully connected to queue!")
            channel, _ := conn.Channel()
            queue.Connection = conn
            queue.Channel = channel
            return
        }

        log.Println("Failed to connect to queue, retrying...", err)

        // Wait 1 second
        time.Sleep(5 * time.Second)
    }
}

// Declare a new queue
func (queue *Queue) Declare(queueName string) (amqp.Queue, error) {
    return queue.Channel.QueueDeclare(
        queueName,
        true,
        false,
        false,
        false,
        nil,
    )
}

// Publish a message
func (queue *Queue) Publish(queueName string, payload []byte) error {
    return queue.Channel.Publish(
        "",
        queueName,
        false,
        false,
        amqp.Publishing{
            DeliveryMode: amqp.Persistent,
            ContentType:  "application/json",
            Body:         payload,
        },
    )
}

// Listen for a new message
func (queue *Queue) Listen(queueName string) (<-chan amqp.Delivery, error) {
    return queue.Channel.Consume(
        queueName,
        "",
        true,
        false,
        false,
        false,
        nil,
    )
}

Then in my main.go

        // Get queue connection
    queue := drivers.NewQueue()
    queue.Connect()

        // Declare new queue for submissions 'stuff'
    _, err = queue.Declare("submissions_queue")

    if err != nil {
        log.Panic(err)
    }

    // We need to declare the responses queue even though we're not actually
    // going to use it on the bot side as an emitter.
    _, err = queue.Declare("submissions_responses")

    // Log out any queue errors
    if err != nil {
        log.Panic(err)
    }

These queue then interact with a legacy PHP application (Laravel). Which runs perfectly for a day or two, then after about a day, sometimes less, sometimes longer, I start getting this Exception (504) Reason: "channel/connection is not open". Any clues? Am I doing something dumb?

Thanks in advance!

See server logs for channel exceptions: they close channels. Also abrupt client TCP connection closures. This client does not provide automatic connection recovery by design, that's your application responsibility.

I've updated my code where the publish to queue error sometimes occurs, to recreate the channel and try again, is that what you meant?

The only output I'm seeing is:

[bot-rmq-1]2016-11-23T10:27:42.499711789Z =INFO REPORT==== 23-Nov-2016::10:27:42 ===
[bot-rmq-1]2016-11-23T10:27:42.499718307Z closing AMQP connection <0.14157.0> (<redacted>:39617 -> <redacted>:5672)
[bot-rmq-1]2016-11-23T10:27:42.705177619Z 
[bot-rmq-1]2016-11-23T10:27:42.705206764Z =INFO REPORT==== 23-Nov-2016::10:27:42 ===
[bot-rmq-1]2016-11-23T10:27:42.705212691Z accepting AMQP connection <0.14215.0> (<redacted>:39622 -> <redacted>:5672)
[bot-rmq-1]2016-11-23T10:27:43.046378706Z 
[bot-rmq-1]2016-11-23T10:27:43.046413103Z =INFO REPORT==== 23-Nov-2016::10:27:43 ===
[bot-rmq-1]2016-11-23T10:27:43.046419515Z closing AMQP connection <0.14180.0> (<redacted>:51242 -> <redacted>:5672)
[bot-rmq-1]2016-11-23T10:27:43.253073052Z 
[bot-rmq-1]2016-11-23T10:27:43.253103233Z =INFO REPORT==== 23-Nov-2016::10:27:43 ===
[bot-rmq-1]2016-11-23T10:27:43.253109057Z accepting AMQP connection <0.14227.0> (<redacted>:51243 -> <redacted>:5672)
[bot-rmq-1]2016-11-23T10:27:45.950046536Z 
[bot-rmq-1]2016-11-23T10:27:45.950085091Z =INFO REPORT==== 23-Nov-2016::10:27:45 ===
[bot-rmq-1]2016-11-23T10:27:45.950091135Z closing AMQP connection <0.14215.0> (<redacted>:39622 -> <redacted>:5672)
[bot-rmq-1]2016-11-23T10:27:46.147715611Z 

I'm not actually seeing any exceptions anywhere so far

Yes, applications need to open another channel and use that, there is no other way. Automatic connection recovery in other clients allocates it while preserving channel id but as far as RabbitMQ is concerned, it's a completely separate channel.

Your log suggests connections are being closed < 5 seconds after they are accepted. RabbitMQ connections (with any messaging protocol) are meant to be long lived: are you sure your own code doesn't carry a high degree of connection churn? If it's concurrent on top of that, it's not difficult to see how one thing tries to do something while a connection is being closed. So try using long-lived connections, in particular if you have channel recovery in place already.

Awesome, cheers for you help! I'm not intentionally using a short-lived connection, perhaps something, somewhere is prematurely closing the connection. I'll have further dig into my code.

@EwanValentine connection-level errors ("hard errors" in protocol speak) are rare and should be very visible in the broker log. They usually indicate client bugs w.r.t. serialization or concurrency.

maybe your queue name is created.

I had this problem it turns out the rabbitmq server wasn't on. so maybe check that?

Facing Issue: Failed to publish a message: Exception (504) Reason: "channel/connection is not open"

I have checked logs in rabbitMQ
2020-07-30 11:55:57.792 [error] <X.XXXX.86> Channel error on connection <X.XXXX.86> (XX.XXX.XX.X:42801 -> XX.XX.X.XX:4672, vhost: '/', user: 'openapi-gateway'), channel 1: operation basic.publish caused a channel exception not_found: no exchange 'api-log-aggregator' in vhost '/' 2020-07-30 11:55:58.327 [warning] <X.XXXX.86> closing AMQP connection <X.XXXX.86> (XX.XXX.XX.X:42801 -> XX.XX.X.XX:4672, vhost: '/', user: 'openapi-gateway'):

as per above logs api-log-aggregator has not deployed that why getting error so I have deployed api-log-aggregator service and redeploy related services .
It worked fine....

Was this page helpful?
0 / 5 - 0 ratings