# AMQPConnectionError: connection closed by server (405) UNEXPECTED_FRAME

- **ID:** `communication/amqp-connection-closed-by-server`
- **Domain:** communication
- **Category:** protocol_error
- **Error Code:** `405`
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

RabbitMQ or other AMQP broker closes the connection when a client sends a frame out of order, such as a basic.publish without a channel.open, or a heartbeat after connection.tune-ok.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| AMQP 0-9-1 | active | — | — |
| RabbitMQ 3.12 | active | — | — |
| Pika 1.3 | active | — | — |

## Workarounds

1. **Ensure the client opens a channel before publishing. In Pika, call channel = connection.channel() before any basic_publish. Verify the channel is not None and is open.** (90% success)
   ```
   Ensure the client opens a channel before publishing. In Pika, call channel = connection.channel() before any basic_publish. Verify the channel is not None and is open.
   ```
2. **Check for multiple threads sharing the same connection without synchronization. Use a connection per thread or add a lock around channel operations.** (80% success)
   ```
   Check for multiple threads sharing the same connection without synchronization. Use a connection per thread or add a lock around channel operations.
   ```

## Dead Ends

- **Restart the RabbitMQ server to clear the error** — The error is client-side; restarting the broker does not fix the client's frame ordering issue (95% fail)
- **Increase the heartbeat interval on the client** — Heartbeat timing is not the cause; the client is sending frames without proper channel initialization (85% fail)
- **Use a different AMQP library (e.g., switch from Pika to Kombu)** — The error is protocol-level, not library-specific; a different library will fail the same way if the frame sequence is wrong (80% fail)
