Skip to content

fix(nuttx): stop TLS running the heap out during firmware downloads - #34

Merged
reubenmiller merged 2 commits into
mainfrom
fix/nuttx-download-reset
Nov 19, 2026
Merged

reubenmiller merged 2 commits into
mainfrom
fix/nuttx-download-reset

Conversation

@reubenmiller

@reubenmiller reubenmiller commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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_BUFFERS is 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):

TLS: connection 0: write failed (-0x008d, errno 0)   # MQTT: PSA_ERROR_INSUFFICIENT_MEMORY
session ended (-104)
TLS: connection 1: read failed (-0x008d, errno 0)    # the download
download: the transfer failed (-104)

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) sets MBEDTLS_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):

    • caps outgoing records at 4 KB, which saves 12 KB per session;
    • leaves out 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

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>
@reubenmiller
reubenmiller merged commit 52baddc into main Nov 19, 2026
20 checks passed
@reubenmiller
reubenmiller deleted the fix/nuttx-download-reset branch November 19, 2026 03:55
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.

NuttX: firmware download fails with -104 after the agent has run for many hours

1 participant