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.
This commit is contained in:
DeFiDude committed 2026-03-11 14:48:19 -06:00
1 parent bc33c0af64
commit bb291795da
3 files changed
+44 -11

No files matched your search

+16 -11
View File
@@ -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
+5
View File
@@ -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);
+23
View File
@@ -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 <typename T> uint32_t crc(const T& obj) {