Troubleshooting
Why an MQTT client stays open after a cellular WAN drop
Short answer
After a cellular WAN or PPP drop, the old MQTT client object can still report connected and accept publishes while the downlink is dead. The recovery that works is event-driven: close and discard the client on WAN or PPP loss, recreate it only after the new path has an address and a route, then confirm one message in each direction before trusting the session. If a fresh client still passes no traffic, restart the application, then reboot the gateway.
On this page
An MQTT publish that the client library accepts says nothing about delivery. When the cellular WAN or PPP path drops, the old TCP session can keep looking connected: writes sit in a local buffer, the client still reports connected, and once the modem redials onto the same public address the uplink can even reach the broker again, while the downlink stays dead and cloud commands never arrive. The recovery that clears this is event-driven, not flag-driven. Close and discard the client as soon as WAN or PPP goes down, recreate it only after the restored path holds a real address and a working default route, then prove the session with one confirmed message in each direction. If a fresh client still passes no traffic, restart the application; if that fails, reboot the gateway.
Why the client still reports connected after the path dies #
Four layers hold state separately: the modem link, the PPP or WAN manager, the TCP socket, and the MQTT client object. A WAN restart tears down the first two and the route beneath the third; the fourth keeps its open state until a socket error, a keepalive failure, or an explicit close() reaches it. Most client libraries take a publish into a local queue or the socket send buffer before the transport state is checked, so the call returns success while the path is already gone. Neither the return value nor the connected flag is evidence of delivery.
Two timers decide how long the illusion lasts. The MQTT keepalive is one: at the common 60 s setting, the broker drops a client that has sent nothing for 1.5 × 60 s = 90 s, so any outage shorter than that passes without a client-side error. The carrier side is the other: cellular sessions are re-dialed by routine timers, a maximum connection time on the PPP profile or an idle timeout that closes the link when no traffic resets it, with 900 s (15 minutes) as a documented example of the kind. The redial is normal behavior; the design fault is a client that never hears about it.
After the redial comes the asymmetry. Carriers often hand back the same public address, so packets from the old socket find the broker again and uplink looks healthy. The inbound half of the old session is another matter: commands pushed into that dead path never arrive, and because the client believes it is still connected, it never resubscribes. The broker side behaves the same way after its own restart, which is why a subscriber can stop receiving while still reporting connected. Transport and session are separate questions, the same separation as when TCP works but the MQTT handshake fails. Treat uplink flow as a sign the path exists, never as proof the session is sound.
Watch the physical link too. In one reported Ethernet test of this pattern, pulling the cable produced no WAN-down callback at all: the client accepted publishes for several minutes, declared itself disconnected, and then refused to reconnect even after the cable went back in. A WAN callback is one input, not the whole truth, so poll the interface state as well.
Worked example: one cellular drop, with the arithmetic #
The timeline below is a worked example of this failure, written the way a cellular router log shows it; the values are illustrative, and your own log carries the real numbers.
| Time | Event | What it tells you |
|---|---|---|
| 15:13:18 | WAN and PPP up, both holding the example address 46.23.250.103 | Cellular path ready |
| 15:14:15 | MQTT connection reported OK | Session established on that path |
| 15:24:18 | PPP maximum connection time exceeded | A router timer forced the redial |
| 15:24:19 to 15:24:22 | Default-gateway read failures | The routed path under the socket is gone |
| 15:24:23 | PPP closes, the WAN manager restarts the permanent connection, WAN=0 with every address 0.0.0.0 | The path-down transition reaches the application |
| 15:24:29 | PPP=0 | The PPP-down event lags the WAN event |
| 15:24:32 | First redial attempt fails | No connect attempt belongs here |
| 15:25:37 | WAN=1 and PPP=1, same address as before | Path restored, same public address returned |
Read the numbers. The outage runs from 15:24:18 to 15:25:37, which is 79 s. The PPP=0 event lands at 15:24:29, which is 6 s after WAN=0, so a recovery routine must hold the session closed across both events, not just one. With the 60 s keepalive, the broker's silence limit is 1.5 × 60 s = 90 s, and the 79 s outage is shorter than that window, which is exactly why no client-side error appears during the drop. The restored path also presents the same public address as before, which is what lets the old socket's uplink packets find the broker again and feeds the false health signal.
One log value deserves suspicion. The close record reports a session duration counter of 3606 s, which does not agree with the timestamps around it, so use your own clock for outage arithmetic rather than the PPP layer's counter. The trigger here, a maximum connection time, is one of the routine reasons cellular sessions re-dial, and idle timeouts behave the same way. After 15:25:37, build the new client only once the interface holds the address again and the default route resolves toward the broker; how a router addresses its uplink, the static LAN versus DHCP addressing question on a WAN uplink, decides how long that wait is.
The recovery sequence #
Drive recovery from the WAN and PPP events and gate every step on evidence.
- On WAN=0 or PPP=0, mark the session unavailable, stop publishing, and hold outgoing data in local storage under your retention policy. Do not wait for a client-side error; the worked example shows that error may never come.
- Close the client and discard the object: an explicit
close()on the old instance, then a new instance for the next connection. Never reuse the object that rode the dead path. - Never call
connect()while the path is down. Gate every attempt on WAN=1 and PPP=1, and bound each attempt with a timeout; connecting into a dead path has wedged client libraries outright in reported cases. - After both up events, verify the path before building anything: the interface must hold a current address, not 0.0.0.0, and the default route must resolve toward the broker. On a router with more than one uplink, failover shifts the default path between routed interfaces and your socket rides whichever wins, so how routed uplink interfaces behave during WAN failover belongs in this check.
- Create one new client, connect, and re-create every subscription the application needs. Keep the same client identifier so the broker replaces the old session instead of refusing a duplicate.
- Prove the uplink: publish a uniquely valued test message with broker acknowledgment required (QoS 1), or confirm that exact value on the broker. Client acceptance proves nothing.
- Prove the downlink: send a unique test command from the cloud and require the device to act on it and report back. A confirmed round trip is the real test, the same principle as making downlink acknowledgments visible on LoRaWAN links, where a command counts as delivered only when the far end acknowledges it.
- If either direction still fails, escalate in bounded steps: one close-and-recreate cycle, then an application restart, then a gateway reboot. In the reported runs behind this failure, a fresh client reported connected with no traffic either way, the application restart did not clear it, and only the gateway reboot did.
A gateway reboot takes down every service riding that box, remote access, local buffering, anything else on the device, so run it in a maintained window and keep a local way in so a remote site is never locked out.
Symptoms and the decisive check #
| Symptom | Failure domain | Decisive check |
|---|---|---|
| WAN=0, then PPP=0 seconds later | Routine router-forced redial | Run the recovery sequence on the events, not on MQTT errors |
| Cable pulled, no WAN event at all | Physical link and logical WAN state not tracking together | Poll the link and count how long the client keeps accepting publishes anyway |
| Publish accepted, nothing reaches the broker | Buffered writes on a stale socket | Broker receipt of a unique value |
| Uplink flows, cloud commands never arrive | Dead inbound half of the old session, subscriptions never restored | Unique cloud-to-device command with a device-side echo |
| Fresh client connected, no traffic either way | Wedged network stack after repeated drops | One rebuild, one application restart, then the gateway reboot |
For the third row, the publish-path diagnostics for a client that never reaches the broker apply unchanged to a PC-side client: follow the packets from the socket outward and stop at the first hop that swallows them.
Build the client around the path, not the flag #
A small state machine keeps the session honest: offline, path-ready, connecting, online. Only the online state accepts publishes; the other two hold data locally; connecting is bounded by a timeout. Triggers are always the WAN and PPP events plus a periodic link poll, never the client's connected flag. A shorter keepalive shortens detection, but it cannot fix buffered writes, so keep the events as the first line of detection and the keepalive as the second. Design for the redial: cellular sessions are meant to come and go on timers, and a client that rides out the drop and proves itself on return is what the application actually needs.
Questions asked
Does QoS 1 prove my message reached the broker?
A QoS 1 publish is confirmed when the broker returns its acknowledgment for that single message, so a received acknowledgment is broker-side confirmation. The client library accepting the publish is not that; it only means the message entered a local queue. If the WAN drops between publish and acknowledgment, hold the message locally and republish it after the new session is up, and let the receiving side handle the possible duplicate.
Why did the router redial when nothing was wrong?
Cellular sessions are forced to re-dial by configuration, not only by faults: a maximum connection time on the PPP profile, or an idle timer that closes the link when no traffic resets it, with 900 s (15 minutes) as a documented example of such a timer. The drop is routine. The fault to fix is a client that never reacts to the WAN and PPP events around it.
How long should I wait after WAN=1 before reconnecting?
Wait until the interface holds a current address and the default route resolves toward the broker; in the worked example those follow within seconds of the WAN=1 event, after a first redial attempt had already failed. Bound the connect attempt too: a connection-attempt timeout around 45 s, the default in one firewall product's PPP profile, keeps a hung attempt from stalling the recovery sequence.
Can I just set an aggressive keepalive and skip the WAN events?
A shorter keepalive shortens detection: at 30 s, the broker ends a silent session at 1.5 times the interval, so 45 s of silence closes it. It cannot fix the local problem, because a library that buffers writes still accepts publishes during the outage without raising an error. Treat keepalive as the second line of detection and the WAN and PPP events as the first.
What happens on the broker side when I reconnect with the same client ID?
Brokers key sessions by client identifier, so a reconnect with the same identifier makes the broker drop the previous connection and hand the session to the new one, which is exactly what you want after discarding a dead client. Start a clean session instead of resuming the stale one, then re-create every subscription explicitly, because the command path only works over subscriptions the new session actually holds.