Repository navigation
esp32: Close the NVS handle when nvs_get_binary returns early - #2451
Merged
Merged
Conversation
bettio
approved these changes
Oct 27, 2026
nif_esp_nvs_fetch_binary() opens an NVS handle and closed it only after reading the blob. A missing key, an error reading its size or no memory for the result returned without nvs_close(), so each such read lost the handle ESP-IDF allocated: about 72 bytes on the heap. Reading a missing key in a loop slowly ran a device out of memory. test_nvs reads a missing key 500 times and checks the free heap does not shrink; without this change it loses 36 KB. Signed-off-by: Gabriel Mancini <gabriel.mancini@gmail.com>
gabrielmancini
force-pushed
the
nvs-close
branch
from
October 28, 2026 18:08
87f1a40 to
c5231b7
Compare
Author
|
Rebased on |
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.
nif_esp_nvs_fetch_binary()(esp:nvs_get_binary/1,2) opens an NVS handle and closes it only after reading the blob. Three returns after a successfulnvs_openskipnvs_close:{error, not_found}, whichesp:nvs_get_binaryturns intoundefined)nvs_get_blobfails while reading the sizememory_ensure_freefails (thenot_foundtuple or the binary)Each of them loses the handle ESP-IDF allocated in
nvs_open, about 72 bytes of heap. Reading a missing key now and then is common, for example an optional setting, and it went unnoticed. Reading one on every loop does not: we read an optional setting on every 2 s poll, and an ESP32 without PSRAM lost ~1.2 KB/min and aborted withCannot handle out of memoryevery ~20 minutes.The fix calls
nvs_closebefore each of those returns.put_binaryand botheraseNIFs already close before returning.Test.
test_nvs(ESP32) writes a key so the namespace exists, then reads a missing key 500 times as a warm-up and 500 times more, and checks the free heap (esp32_free_heap_size) did not shrink by 4 KB or more across the second batch.{leaked,36032}, 36 KB lost in 500 reads; with it,test_nvspasses (test_socketfails here as it does on plain release-0.7, no network inside QEMU).These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license).
SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later