Troubleshooting
Why a Mosquitto broker logs malformed packet on a PLC MQTT client
Short answer
The malformed packet line is the broker's disconnect notice: Mosquitto received bytes from that client that do not decode as MQTT and closed that connection. It is a broker-side message, not a controller diagnostic. Make every client ID unique, correlate the line with the controller's Success, Error and Error Code status tags at the same attempt, then capture from the enable-bit trigger and decode the CONNECT before assigning a cause.
On this page
- What the malformed-packet line tells you
- Give every controller its own client ID first
- Read the controller's status values at the same attempt
- Capture the failing attempt from the enable-bit trigger
- Worked example: decoding the CONNECT packet Mosquitto 2.0.11 rejected at 09:58:41
- What each result decides
- Questions asked
Your Mosquitto broker is not reporting a controller fault when it logs Client plant-plc-01 disconnected due to malformed packet. (the client ID is an example). It is reporting its own finding: bytes arrived from that client that the broker's MQTT parser could not decode, so the broker closed that one connection and kept running. The line lives in the broker log, carries the broker's verdict on the traffic, and you will not find it in the controller's diagnostic list, because the controller never produced it. Work it in order: give every controller a unique client ID, read the controller's status values at the same attempt, capture the failing attempt from the enable-bit trigger, then decode the CONNECT packet against the MQTT header rules. The case behind this article: several controllers publishing to a Mosquitto 2.0.11 broker stopped connecting after more controllers were added with a changed topic structure, after one had worked alone for months.
What the malformed-packet line tells you #
The line says packets were exchanged; it does not say what they contained. The contents come from your capture, not from the log. On a parse failure Mosquitto closes only the offending client's socket, and every other session continues untouched. With an MQTT 5.0 client the rejection goes out as a CONNACK carrying reason code 0x81, Malformed Packet; with a 3.1.1 client the broker simply closes the socket, which is why the controller often reports nothing more than a dropped link.
Treat the line as a symptom with a short list of usual causes. What puts undecodable bytes on the wire in this kind of failure:
- A CONNECT or PUBLISH whose length fields do not match the bytes that follow, so the parser lands mid-field and cannot recover.
- A client stack that changed shortly before the faults began: new firmware, a new client library, a new message template.
- A device that speaks a proprietary variant only its own vendor's broker tolerates; a spec-conforming broker refuses those frames.
- An MQTT client configured for TLS aimed at a plaintext listener, so the broker reads TLS record bytes as MQTT and rejects them. The reverse mismatch fails inside the TLS handshake with its own log output; that case has its own article on why a Mosquitto MQTT TLS listener returns an unknown error.
- A change made once for the whole system, such as a broker reconfiguration or a new authentication layer, when a client that worked for months fails alongside freshly added ones.
Other industrial protocols log the same way; DNP3 malformed packet: a symptom, not a root cause makes the same point for DNP3. None of the causes above is proven until your capture shows it.
Give every controller its own client ID first #
The MQTT specification allows exactly one connection per client ID: when a second client connects with an ID already in use, the broker drops the existing session. Duplicates also destroy attribution. Once two controllers share an ID, a log line carrying that ID no longer names the controller that put the bytes on the wire, and the capture can no longer be tied to one device.
This failure began when more controllers joined a broker one PLC had served alone, and copied configuration is how client IDs get duplicated. List the client ID configured in every controller, plus every engineering laptop, test tool and second runtime instance that talks to the broker, and make each one unique — embed the line and position, for example plant-line1-plc01. Re-test after that change alone.
If the same client ID turns up in two places, stop and fix that first: every later step assumes one ID names one controller.
Read the controller's status values at the same attempt #
The controller side has its own view of the same attempt. Its MQTT client exposes live status values for the publisher and the subscriber — Success, Error and Error Code. Open the monitoring view in the controller's programming software, trigger the failing attempt, and record all values at the same wall-clock second as the broker log line; screenshots, not recollection. While online, record the programming software version and save the controller's system report for the evidence pack.
Record the Error Code exactly as displayed and leave it untranslated. An undocumented code converted into a presumed cause this early sends the diagnosis down the wrong path.
Success FALSE and Error TRUE at the same second the broker writes the line means the rejection is real and belongs to that attempt: the controller watched the connection die, and the broker log explains why. The same broker-log, status-value and capture correlation is covered for another controller pairing in troubleshooting an S7-1500 MQTT connection to a Mosquitto broker.
Capture the failing attempt from the enable-bit trigger #
The log line names the client and the reason; the capture names the bytes and the sender. The source IP address beside each packet is what ties the failing attempt to one physical controller.
- Mirror the broker's access switch port or move the test to a segment you control, inside a planned window; never splice a capture device inline into a live control network.
- Begin the capture before anything is triggered.
- Filter to the broker's address and port with the display filter
mqtt. - Trigger the controller's MQTT enable bit so the attempt reaches the wire from its first packet: the TCP handshake, the CONNECT and the broker's reply.
- Save the file and note the broker log timestamp of the matching line.
- Repeat for each affected controller, then compare the packets that immediately precede each disconnect.
If the attempt never reaches the broker at all, that is an earlier and different failure with its own checklist: fix a PLC MQTT client that never reaches your broker.
Worked example: decoding the CONNECT packet Mosquitto 2.0.11 rejected at 09:58:41 #
The inputs below are example values for this case, and the header rules come from the OASIS MQTT 3.1.1 specification.
- Broker: Mosquitto 2.0.11, the version in the log.
- Log line:
Client plant-plc-01 disconnected due to malformed packet.at 09:58:41 broker clock. - Client ID configured on the controller: plant-plc-01, 12 characters, example value.
- Client protocol: MQTT 3.1.1.
- Header rules from the specification: a CONNECT begins with byte 0x10, then a remaining-length field, then a 10-byte variable header — a 2-byte protocol-name length, the four bytes of the name MQTT, one protocol-level byte, one connect-flags byte, a 2-byte keep-alive — then the payload, which begins with a 2-byte client-ID length and the ID characters.
The rule in words: the remaining-length field must equal the number of bytes after it, and every string length prefix must equal the number of characters in the string that follows.
The arithmetic:
- Variable header: 10 bytes.
- Client ID field: 2 + 12 = 14 bytes, with no will, username or password flags set.
- Remaining length: 10 + 14 = 24 bytes, which encodes as the single byte 0x18 because 24 fits in one 7-bit chunk.
- Protocol-name length: 4 characters, encoded 0x00 0x04, followed by 0x4D 0x51 0x54 0x54, the ASCII bytes of the name MQTT.
Result: a well-formed CONNECT from this controller begins 10 18 00 04 4D 51 54 54 04 02 00 3C 00 0C and then carries plant-plc-01, where 0x04 is the 3.1.1 protocol level, 0x02 the clean-session flag and 0x003C a keep-alive of 60 seconds.
Decision: the threshold is equality. If any captured byte disagrees with that pattern — a first byte other than 0x10, a remaining-length byte that does not equal the byte count after it, a name prefix other than 0x00 0x04 — the controller built the packet wrong, the broker is right, and the fix belongs on the client side. If the bytes match and the broker still rejects, the line may not belong to this controller at all: hunt for the same client ID arriving from a second source address before touching the broker.
What each result decides #
- Length fields disagree with the bytes on the wire: the packet construction on the controller is the fault. Fix the message template, the client instruction's parameters, or the library and firmware versions on the controller; the broker needs no change. Prove the fix by connecting cleanly, then restoring the old configuration once under controlled conditions to confirm the fault returns.
- Bytes parse clean and the same client ID appears from two source addresses: duplicate client IDs. Make every ID unique and re-test.
- The log names a client the parser could not identify, or no matching traffic appears in your capture: the sender is not the controller you captured, or the failure sits in the earliest handshake bytes. Widen the capture point to the broker's access port and repeat.
File the evidence with the case: the capture, the matching broker log export, the status screenshots, the system report and the software versions. Verification is finished only when the broker stops logging the line and the controller reports Success on connect and on publish.
Questions asked
Should I restart or reconfigure the broker to clear the message?
No. The line disappears once the offending client stops sending undecodable bytes; restarting Mosquitto changes nothing about the traffic that produced it. Treat broker-side changes as the last step, taken only when a capture decodes clean against the header rules and a known-good client still triggers the line.
The log line names the client as unknown. What does that tell me?
The parser failed before it could read the client identifier out of the CONNECT packet, so there was no ID to log. That places the failure in the earliest bytes of the handshake, before anything about your topics, credentials or settings was read, and it points squarely at the packet construction on the client.
Can duplicate client IDs produce this exact message?
Usually not. A second connection with an ID already in use makes the broker drop the older session, and MQTT allows one connection per client ID, so that churn normally logs a different reason. You still fix duplicates first: with a shared ID, a malformed packet line no longer names the controller that sent the bytes.
Can I skip the packet capture and work from the broker log alone?
No. The log names the client ID and the reason, and it may show each connection's source address, but it never shows the bytes, so it cannot tell you which field failed or prove the fix. The capture pairs sender, bytes and timestamp in one place; that pairing is what turns a correlation into a cause.
The failures began right after a software update. Which side do I suspect?
Look at the updated side first. A new firmware, client library or message template can start building frames a conforming broker rejects, and a broker update can enforce parsing an older client never met. Decode the captured CONNECT against the MQTT header rules; the side whose bytes fail those rules is the side to fix.