From bb291795da0becb83243c64228f50e82dfba343d Mon Sep 17 00:00:00 2001 From: DeFiDude <59237470+DeFiDude@users.noreply.github.com> Date: Wed, 11 Mar 2026 14:48:19 -0600 Subject: [PATCH] Fix TCP outbound: stop get_path() from destroying valid routes + PSRAM allocator - get_path(): Remove announce_packet null check that destroyed valid path entries when cache deserialization failed under heap pressure. Routing fields (_received_from, _hops, receiving_interface) are valid without it. - hops_to(): Direct path table lookup instead of get_path() to avoid triggering expensive announce_packet lazy load from flash cache. - path_request_handler(): Guard against null announce_packet gracefully. - Persistence: Add PSRAM allocator for ArduinoJson JsonDocument on ESP32 boards with PSRAM, preventing deserialization failures when heap is low. --- src/Transport.cpp | 27 ++++++++++++++++----------- src/Utilities/Persistence.cpp | 5 +++++ src/Utilities/Persistence.h | 23 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/Transport.cpp b/src/Transport.cpp index a2773a8..82497e8 100644 --- a/src/Transport.cpp +++ b/src/Transport.cpp @@ -2683,11 +2683,10 @@ Deregisters an announce handler. auto iter = _path_table.find(destination_hash); if (iter == _path_table.end()) return empty_destination_entry; DestinationEntry& destination_entry = (*iter).second; - if (!destination_entry.announce_packet()) { - DEBUGF("Entry for destination %s found but is missing announce packet, discarding", destination_hash.toHex().c_str()); - remove_path(destination_hash); - return empty_destination_entry; - } + // Don't require announce_packet for basic routing — it's only needed for + // path response forwarding. The routing fields (_received_from, _hops, + // receiving_interface) are valid without it, and removing the path here + // when deserialization fails under memory pressure breaks TCP outbound. if (!destination_entry.receiving_interface()) { DEBUGF("Entry for destination %s found but is missing receiving interface, discarding", destination_hash.toHex().c_str()); remove_path(destination_hash); @@ -2725,13 +2724,14 @@ Deregisters an announce handler. :returns: The number of hops to the specified destination, or ``RNS.Transport.PATHFINDER_M`` if the number of hops is unknown. */ /*static*/ uint8_t Transport::hops_to(const Bytes& destination_hash) { - auto& destination_entry = get_path(destination_hash); - if (destination_entry) { - return destination_entry._hops; - } - else { - return PATHFINDER_M; + // Direct path table lookup — avoids get_path() which triggers + // expensive announce_packet lazy load from flash cache. + // hops_to() only needs _hops which is already in memory. + auto iter = _path_table.find(destination_hash); + if (iter != _path_table.end()) { + return (*iter).second._hops; } + return PATHFINDER_M; } /* @@ -3049,6 +3049,10 @@ will announce it. else if ((Reticulum::transport_enabled() || is_from_local_client) && destination_entry) { TRACEF("Transport::path_request_handler: entry found for destination %s", destination_hash.toHex().c_str()); const Packet announce_packet = destination_entry.announce_packet(); + if (!announce_packet) { + DEBUGF("Path entry for %s exists but announce packet unavailable (cache miss), cannot answer path request", destination_hash.toHex().c_str()); + } + else { TRACEF("announce_packet destination_hash: %s", announce_packet.destination_hash().toHex().c_str()); TRACEF("announce_packet hops: %u", announce_packet.hops()); TRACEF("announce_packet str: %s", announce_packet.toString().c_str()); @@ -3135,6 +3139,7 @@ TRACEF("announce_packet str: %s", announce_packet.toString().c_str()); cull_announce_table(); } } + } // else (announce_packet valid) } else if (is_from_local_client) { // Forward path request on all interfaces diff --git a/src/Utilities/Persistence.cpp b/src/Utilities/Persistence.cpp index 4c835a3..67e4e98 100644 --- a/src/Utilities/Persistence.cpp +++ b/src/Utilities/Persistence.cpp @@ -4,6 +4,11 @@ using namespace RNS; +#if defined(BOARD_HAS_PSRAM) && defined(ESP32) +/*static*/ RNS::Persistence::PsramAllocator _psramAllocatorImpl; +/*static*/ JsonDocument _document(&_psramAllocatorImpl); +#else /*static*/ //DynamicJsonDocument _document(Type::Persistence::DOCUMENT_MAXSIZE); /*static*/ JsonDocument _document; +#endif /*static*/ Bytes _buffer(Type::Persistence::BUFFER_MAXSIZE); diff --git a/src/Utilities/Persistence.h b/src/Utilities/Persistence.h index c39adb5..7524238 100644 --- a/src/Utilities/Persistence.h +++ b/src/Utilities/Persistence.h @@ -474,8 +474,31 @@ namespace RNS { namespace RNS { namespace Persistence { +#if defined(BOARD_HAS_PSRAM) && defined(ESP32) + // PSRAM allocator for ArduinoJson — prevents deserialization failures + // under heap pressure (heap can be <1KB free while PSRAM has 8+ MB). + class PsramAllocator : public ArduinoJson::Allocator { + public: + void* allocate(size_t size) override { + void* p = ps_malloc(size); + if (!p) p = malloc(size); // fallback to heap if PSRAM unavailable + return p; + } + void deallocate(void* ptr) override { + free(ptr); // free() handles both heap and PSRAM on ESP32 + } + void* reallocate(void* ptr, size_t new_size) override { + void* p = ps_realloc(ptr, new_size); + if (!p) p = realloc(ptr, new_size); // fallback + return p; + } + }; + static PsramAllocator _psramAllocator; + static JsonDocument _document(&_psramAllocator); +#else //static DynamicJsonDocument _document(Type::Persistence::DOCUMENT_MAXSIZE); static JsonDocument _document; +#endif static Bytes _buffer(Type::Persistence::BUFFER_MAXSIZE); template uint32_t crc(const T& obj) {