Repository navigation
MQTT: use the dedicated RX ring buffer mutex on all platforms - #2305
Open
paoloantinori wants to merge 2 commits into
Open
paoloantinori wants to merge 2 commits into
paoloantinori wants to merge 2 commits into
Conversation
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").
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR #2221 introduced a dedicated
g_rx_mutexfor the MQTT RX ring buffer, but guarded it with#if PLATFORM_ESPIDF. On every other platform, including Beken BK7231T,MQTT_RX_Mutex_Takefell back to the sharedg_mutexthat 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 inMQTT_initand used by the RX path everywhere, and the sharedg_mutexstays 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_initsucceeds and the receive counters keep growing.Portability: the change introduces no new API.
MQTT_Mutex_Takealready createsg_mutexwithxSemaphoreCreateMutexunconditionally 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 implementxSemaphoreCreateMutexasreturn 0, and the ESP-IDF-only body ofMQTT_RX_Mutex_Taketreated a null handle as fatal, which would have dropped every incoming message on those simulator builds and broken their selftests.MQTT_RX_Mutex_Taketherefore keeps the lazy-create fallback thatMQTT_Mutex_Takehas always used: on real targets the mutex is created inMQTT_initbefore any callback can run, and the fallback exists so the simulator builds keep receiving. It mirrors the tolerant pattern ofMQTT_Mutex_Take, including its limitation that a failed allocation on real hardware is not handled.Relates to #2292.