Repository navigation
fix(nuttx): stop TLS running the heap out during firmware downloads - #34
Merged
Merged
Conversation
With mbedTLS 4, every PSA call copied its input and output into heap buffers of its own unless MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS is set. With a firmware download and the MQTT session both busy, those copies ran the heap out: TLS failed with PSA_ERROR_INSUFFICIENT_MEMORY (-0x008d) on both connections and the download ended with -104. Set it in the crypto config, as ESP-IDF does, and add a TLS config that caps outgoing records at 4 KB and leaves out the server role, as the Zephyr port does. The update image shrinks by 74 KB. Log the mbedTLS error and errno before mapping a TLS failure to -ECONNRESET, so the next failure of this kind names its cause. Fixes #32 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes #32.
Cause
The download did not fail because of the token or a reused connection. The heap ran out. With mbedTLS 4, every PSA call copies its input and output into heap buffers of its own unless
MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERSis set. That is up to two 16 KB copies for each TLS record. With the firmware download and the MQTT session both busy, the C6 logged (with the new error logging in this PR):Other failures in the same runs were -0x6c00 (internal error) and -0x7f80 (a reconnect handshake that failed).
Changes
ports/nuttx/include/tedge_mbedtls_user_config.h(crypto config) setsMBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS. ESP-IDF's mbedTLS config sets it too. Every buffer here belongs to the one application.New
ports/nuttx/include/tedge_mbedtls_tls_config.h(TLS config,MBEDTLS_USER_CONFIG_FILE):MBEDTLS_SSL_SRV_C.The Zephyr port does the same.
ports/common/src/tedge_sockets.c: logs the mbedTLS error and errno at warning level before mapping a TLS failure to-ECONNRESET. A TLS read returns 0 only for a peer close.Testing
On an ESP32-C6-DevKitC (NuttX,
tedge-agent-nuttx-full), installing firmware from Cumulocity:Without the fix (0.16.0, 0.17.0, and debug builds of 0.17.0), downloads failed often. The 0.17.0 update failed twice before it succeeded, and a cycle of debug builds failed 1 install in 3.
With the fix, 20 installs alternating between two builds: no download failed.
The update image shrinks from 1,415,840 to 1,341,528 bytes.
The POSIX port builds with
-Werror, and its tests pass.One of the 20 installs stayed EXECUTING because of a separate bug. A reconnect between "image written" and the reboot read the pending marker as a rollback. A separate PR fixes it.
🤖 Generated with Claude Code