# MQTT CONNACK: 数据包标识符已被占用

- **ID:** `communication/mqtt-packet-identifier-in-use`
- **领域:** communication
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 76%

## 根因

MQTT 客户端发送的 PUBLISH 或 SUBSCRIBE 数据包使用了尚未确认的重复数据包标识符，违反了 MQTT 协议规范。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Mosquitto 2.0.15 | active | — | — |
| Paho MQTT 1.3.12 | active | — | — |
| EMQX 5.0 | active | — | — |

## 解决方案

1. ```
   Implement a proper packet identifier manager that reuses IDs only after acknowledgment or timeout. Example in C using Paho: `MQTTAsync_setCallbacks(client, NULL, connlost, msgarrvd, onDeliveryComplete)` and track IDs via a hash set.
   ```
2. ```
   Increase `max_inflight_messages` in broker config (e.g., Mosquitto: `max_inflight_messages 20`) to reduce identifier reuse pressure.
   ```

## 无效尝试

- **** — Disabling QoS entirely (QoS 0) avoids identifiers but loses delivery guarantees. (50% 失败率)
- **** — Manually incrementing packet IDs without tracking acknowledgments still causes collisions. (80% 失败率)
- **** — Restarting the broker clears state but doesn't fix the client's identifier management logic. (70% 失败率)
