Skip to content

MQTT: use the dedicated RX ring buffer mutex on all platforms - #2305

Open
paoloantinori wants to merge 2 commits into
openshwprojects:mainfrom
paoloantinori:fix/mqtt-rx-mutex-all-platforms
Open

paoloantinori wants to merge 2 commits into
openshwprojects:mainfrom
paoloantinori:fix/mqtt-rx-mutex-all-platforms

Conversation

@paoloantinori

Copy link
Copy Markdown

PR #2221 introduced a dedicated g_rx_mutex for the MQTT RX ring buffer, but guarded it with #if PLATFORM_ESPIDF. On every other platform, including Beken BK7231T, MQTT_RX_Mutex_Take fell back to the shared g_mutex that the publish and connect path also takes, keeping the same cross-thread sharing that fix removed for ESP-IDF. On Beken the tcp_thread fills the RX ring buffer while the command task drains it, the same split that fix addressed. The platforms are in the same position, but only one of them got the fix. This removes the guards: the dedicated mutex is created in MQTT_init and used by the RX path everywhere, and the shared g_mutex stays with publish and connect. No behavior change on ESPIDF.

For context, issue #2292 documents the Beken side of this freeze class, observed on 1.18.219: MQTT publishing stops permanently while the connection, wifi and the web UI stay alive, and an HTTP reboot recovers it. I cannot prove that freeze shares the exact mechanism PR #2221 fixed for ESP-IDF, and this change does not claim to close #2292. Our own last note in that issue says the sharing on Beken degrades to handled contention rather than corruption, and names it as the first suspect to check if the freeze returns. This change removes that suspect; it does not claim to fix the freeze.

Testing on our own bulbs: firmware built from this branch (APP_VERSION 20261108-rxmutex), flashed over the air to three BK7231T bulbs on Nov 8, configs preserved. Periodic checks since show zero publish errors and normal latency on all three. One bulb stalled once, first seen around six hours after flashing. HTTP and MQTT were both unresponsive, deeper than the #2292 signature. It recovered on its own by the next check window, and its uptime showed it never rebooted. So the sharing removal is not a complete cure for the class on Beken, and I report the remaining stall here rather than claim otherwise. The flashed firmware predates one later review fix by exactly one line, the lazy-create fallback described next; on our bulbs that line never runs, because the creation in MQTT_init succeeds and the receive counters keep growing.

Portability: the change introduces no new API. MQTT_Mutex_Take already creates g_mutex with xSemaphoreCreateMutex unconditionally on every platform that compiles this file, so the FreeRTOS semaphore calls used here are already part of every build. One caveat found in review: the win32 and linux simulator stubs implement xSemaphoreCreateMutex as return 0, and the ESP-IDF-only body of MQTT_RX_Mutex_Take treated a null handle as fatal, which would have dropped every incoming message on those simulator builds and broken their selftests. MQTT_RX_Mutex_Take therefore keeps the lazy-create fallback that MQTT_Mutex_Take has always used: on real targets the mutex is created in MQTT_init before any callback can run, and the fallback exists so the simulator builds keep receiving. It mirrors the tolerant pattern of MQTT_Mutex_Take, including its limitation that a failed allocation on real hardware is not handled.

Relates to #2292.

PR openshwprojects#2221 introduced a dedicated g_rx_mutex for the MQTT RX ring buffer
but guarded it with #if PLATFORM_ESPIDF. On every other platform,
including Beken BK7231T, MQTT_RX_Mutex_Take fell back to the shared
g_mutex that the publish and connect path also takes, keeping the same
cross-thread sharing that fix removed for ESP-IDF. Issue openshwprojects#2292 tracks
the Beken freeze class and names that sharing as the first suspect to
check if the freeze returns.

This removes the platform guards so the dedicated mutex is created in
MQTT_init and used by MQTT_RX_Mutex_Take/Free everywhere, leaving the
shared g_mutex to the publish and connect path only. MQTT_RX_Mutex_Take
keeps a lazy-create fallback, the same tolerant pattern MQTT_Mutex_Take
already uses, so the win32 and linux simulator builds keep receiving:
their semaphore stubs return a null handle, which the strict ESP-IDF
body would have treated as fatal. After MQTT_init the behavior on
ESP-IDF is unchanged; before init, the RX path now creates the mutex
instead of dropping the message. The fallback inherits the known
limits of MQTT_Mutex_Take's pattern: a failed allocation is not
handled, and two concurrent first takers could double-create; the
eager creation in MQTT_init, before any connection exists, makes both
unreachable in practice.
When the RF calibration partition is corrupted, Beken chips fall back
to a default MAC (c8:47:8c prefix) with the last 3 bytes zeroed. This
silently degrades WiFi performance and causes intermittent connectivity
issues that look like firmware bugs (openshwprojects#2292).

This detects the fallback MAC at boot, logs an actionable error message,
and exposes the health state as a new rf_health field in /api/info
("ok" or "corrupted").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MQTT publishing stops permanently while the connection stays alive (only a reboot recovers)

1 participant