fix(MQTTTransport): reply with error instead of silently dropping malformed input (F-063)
Invalid JSON or a missing 'cmd' field on control/command previously logged
a WARNING on serial and returned with no reply at all — a client waiting
on control/ack had no way to tell "malformed request" apart from "message
never arrived" or "device offline," all three looking identical until its
own timeout fired.
Both onMessage() drop paths now publish an ERROR to control/ack before
returning: { "status": "ERROR", "type": "parse_error", "message": "..." }.
Unparseable JSON necessarily has no req_id (nothing to read it from); a
valid-but-cmd-less message echoes req_id if one was present. The heap-guard
drop above these two (hasHeapForPublish() check) is untouched — that one's
silence is deliberate, protecting against allocation under critical heap
pressure (see F-055), not something this fix should touch.
Bumped to v2.2.1. Verified: pio run -e vesper-std-v1 -e agnus-v1 both build clean.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -64,7 +64,18 @@ void MQTTTransport::onMessage(const String& topic, const String& payload) {
|
||||
JsonDocument doc;
|
||||
DeserializationError err = deserializeJson(doc, payload);
|
||||
if (err) {
|
||||
LOG_WARNING(TAG, "JSON parse error: %s — dropping message", err.c_str());
|
||||
LOG_WARNING(TAG, "JSON parse error: %s — replying with error", err.c_str());
|
||||
// Can't recover a req_id from unparseable JSON — the reply necessarily
|
||||
// has none. Still worth an ack: a caller waiting on a timeout gets an
|
||||
// immediate, actionable "malformed" instead of silence indistinguishable
|
||||
// from a lost network message.
|
||||
JsonDocument resp;
|
||||
resp["status"] = "ERROR";
|
||||
resp["type"] = "parse_error";
|
||||
resp["message"] = String("Invalid JSON: ") + err.c_str();
|
||||
String out;
|
||||
serializeJson(resp, out);
|
||||
_mqtt.publishAck(out);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -87,7 +98,17 @@ void MQTTTransport::onMessage(const String& topic, const String& payload) {
|
||||
}
|
||||
|
||||
if (envelope.command.isEmpty()) {
|
||||
LOG_WARNING(TAG, "MQTT message missing 'cmd' field — dropping");
|
||||
LOG_WARNING(TAG, "MQTT message missing 'cmd' field — replying with error");
|
||||
JsonDocument resp;
|
||||
resp["status"] = "ERROR";
|
||||
resp["type"] = "parse_error";
|
||||
resp["message"] = "Missing required field: cmd";
|
||||
if (!envelope.reqId.isEmpty()) {
|
||||
resp["req_id"] = envelope.reqId;
|
||||
}
|
||||
String out;
|
||||
serializeJson(resp, out);
|
||||
_mqtt.publishAck(out);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -62,5 +62,5 @@ Responses are published to `control/ack`:
|
||||
- **Broker connection is deferred.** `begin()` does not connect. The connection is triggered by `onNetworkConnected()`, which starts a 2-second non-blocking stabilisation timer in `MQTTDriver`.
|
||||
- **MQTT must be enabled in config, and the `command` topic switch must be on.** `MQTTDriver::connect()` is a no-op if `mqttConfig.enabled` is false. Toggle the master switch with `mqtt.enable` / `mqtt.disable`. Independently, the `command` topic switch (via `mqtt.set_topics`) gates the `control/command` subscription and `control/ack` replies — turning it off while MQTT stays connected silently stops all command traffic.
|
||||
- **No per-message QoS.** Command replies and broadcasts are published at QoS 1 (matching the topic table). Heartbeats use QoS 1 with `retain = true`.
|
||||
- **JSON parse errors drop the message silently** (with a `LOG_WARNING`). The client receives no error response.
|
||||
- **JSON parse errors and missing `cmd` get an `ERROR` reply on `control/ack`** (`type: "parse_error"`), not a silent drop — a `LOG_WARNING` is still emitted too. A parse-error reply necessarily has no `req_id` (it can't be read from input that failed to parse); a missing-`cmd` reply echoes `req_id` if the JSON was otherwise valid and included one.
|
||||
- **Incoming commands are dropped under critical heap pressure.** `onMessage()` checks `MQTTDriver::hasHeapForPublish()` (free heap ≥ 20KB) before even constructing the `JsonDocument` used to deserialize the incoming payload. Below that threshold, the message is dropped with no reply and no error response — indistinguishable from a lost network message to the caller. This exists because a rapid burst of commands (each involving deserialize + dispatch + reply, all allocation-heavy, all running inline on AsyncTCP's own task) previously fragmented the heap enough to crash the device with an unrecoverable `abort()` — see `docs/features/feature-catalog.md` F-055. A full fix (queueing incoming commands to a dedicated task, so this work never runs on AsyncTCP's task at all) is a scoped future improvement, not yet implemented.
|
||||
|
||||
Reference in New Issue
Block a user