# AMQPConnectionError: 服务器关闭连接 (405) UNEXPECTED_FRAME

- **ID:** `communication/amqp-connection-closed-by-server`
- **领域:** communication
- **类别:** protocol_error
- **错误码:** `405`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

RabbitMQ或其他AMQP代理在客户端发送无序帧时关闭连接，例如在没有channel.open的情况下发送basic.publish，或在connection.tune-ok之后发送心跳。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| AMQP 0-9-1 | active | — | — |
| RabbitMQ 3.12 | active | — | — |
| Pika 1.3 | active | — | — |

## 解决方案

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.
   ```
2. ```
   Check for multiple threads sharing the same connection without synchronization. Use a connection per thread or add a lock around channel operations.
   ```

## 无效尝试

- **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% 失败率)
- **Increase the heartbeat interval on the client** — Heartbeat timing is not the cause; the client is sending frames without proper channel initialization (85% 失败率)
- **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% 失败率)
