fix(mqtt): defer the stale-token reconnect when the mint fails

The corrected-clock path reconnected a disconnected slot whether or not
createSlotAuthToken() had produced anything, which re-presented the credentials
the correction had just invalidated. Minting fails for recoverable reasons —
allocation pressure is treated as recoverable elsewhere in this file — so the
path is reachable, and the reconnect it spends is one that cannot succeed.

Move the decision into MQTTConnectionPolicy as classifyStaleToken(), where the
four outcomes are named and host-tested rather than spelled out in nested
conditions: Defer on a failed mint, Reconnect a client that is down, Bounce a
live session whose broker enforces exp, KeepAlive one whose broker does not.
Deferring leaves the slot to the backoff ladder, which mints again on its next
attempt.

Covers the reviewer's first four cases. The other two — that a completed SNTP
sync is required, and that time(nullptr) reflects the accepted epoch before
_ntp_synced flips — are inside MQTTBridge.cpp, which the native env does not
compile; locking those down needs a seam around the IDF calls that does not
exist yet.
This commit is contained in:
agessaman committed 2026-08-14 19:41:14 -07:00
1 parent 168d4a0a8a
commit 0d12ec7d79
3 files changed
+68 -11

No files matched your search

@@ -258,6 +258,38 @@ TEST(SlotActivation, DisabledAndOutOfRangeSlots) {
EXPECT_EQ(SlotActivation::Disabled, Policy::classifySlotActivation(0, nullptr, 3, 2));
}
using Policy::StaleTokenAction;
TEST(StaleToken, ConnectedSlotOnAnExpEnforcingBrokerBounces) {
// The broker will reject the stale token, and esp-mqtt refuses reconnect() from
// CONNECTED, so the transport has to close first.
EXPECT_EQ(StaleTokenAction::Bounce,
Policy::classifyStaleToken(/*minted=*/true, /*connected=*/true,
/*broker_enforces_exp=*/true));
}
TEST(StaleToken, ConnectedSlotOnATolerantBrokerKeepsItsSession) {
// waev leaves live sessions alone past exp. Bouncing would spend a 16 KiB
// contiguous handshake to replace a session the broker was not going to drop.
EXPECT_EQ(StaleTokenAction::KeepAlive,
Policy::classifyStaleToken(true, true, /*broker_enforces_exp=*/false));
}
TEST(StaleToken, DisconnectedSlotReconnectsRegardlessOfBrokerPolicy) {
EXPECT_EQ(StaleTokenAction::Reconnect, Policy::classifyStaleToken(true, false, true));
EXPECT_EQ(StaleTokenAction::Reconnect, Policy::classifyStaleToken(true, false, false));
}
TEST(StaleToken, FailedMintNeverReconnects) {
// Reconnecting here would re-present the credentials the correction invalidated.
// Every combination defers — including the disconnected one, which is the case
// that previously reconnected on the stale token.
EXPECT_EQ(StaleTokenAction::Defer, Policy::classifyStaleToken(false, false, true));
EXPECT_EQ(StaleTokenAction::Defer, Policy::classifyStaleToken(false, false, false));
EXPECT_EQ(StaleTokenAction::Defer, Policy::classifyStaleToken(false, true, true));
EXPECT_EQ(StaleTokenAction::Defer, Policy::classifyStaleToken(false, true, false));
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();