@@ -1353,26 +1353,32 @@ void BaseChatMesh::checkSessionKeyTimeouts() {
13531353
13541354 if (entry->retries_left > 0 ) {
13551355 // Retry: find the contact and resend INIT
1356+ ContactInfo* contact = nullptr ;
13561357 for (int j = 0 ; j < num_contacts; j++) {
13571358 if (memcmp (contacts[j].id .pub_key , entry->peer_pub_prefix , 4 ) == 0 ) {
1358- entry->retries_left --;
1359- entry->timeout_at = futureMillis (SESSION_KEY_TIMEOUT_MS );
1360-
1361- // Regenerate ephemeral keypair for retry
1362- uint8_t seed[SEED_SIZE ];
1363- getRNG ()->random (seed, SEED_SIZE );
1364- ed25519_create_keypair (entry->ephemeral_pub , entry->ephemeral_prv , seed);
1365- memset (seed, 0 , SEED_SIZE );
1366-
1367- uint8_t req_data[1 + PUB_KEY_SIZE ];
1368- req_data[0 ] = REQ_TYPE_SESSION_KEY_INIT ;
1369- memcpy (&req_data[1 ], entry->ephemeral_pub , PUB_KEY_SIZE );
1370-
1371- uint32_t tag, est_timeout;
1372- sendRequest (contacts[j], req_data, sizeof (req_data), tag, est_timeout);
1359+ contact = &contacts[j];
13731360 break ;
13741361 }
13751362 }
1363+ if (!contact) {
1364+ entry->retries_left = 0 ; // contact gone — fall through to cleanup on next tick
1365+ continue ;
1366+ }
1367+ entry->retries_left --;
1368+ entry->timeout_at = futureMillis (SESSION_KEY_TIMEOUT_MS );
1369+
1370+ // Regenerate ephemeral keypair for retry
1371+ uint8_t seed[SEED_SIZE ];
1372+ getRNG ()->random (seed, SEED_SIZE );
1373+ ed25519_create_keypair (entry->ephemeral_pub , entry->ephemeral_prv , seed);
1374+ memset (seed, 0 , SEED_SIZE );
1375+
1376+ uint8_t req_data[1 + PUB_KEY_SIZE ];
1377+ req_data[0 ] = REQ_TYPE_SESSION_KEY_INIT ;
1378+ memcpy (&req_data[1 ], entry->ephemeral_pub , PUB_KEY_SIZE );
1379+
1380+ uint32_t tag, est_timeout;
1381+ sendRequest (*contact, req_data, sizeof (req_data), tag, est_timeout);
13761382 } else {
13771383 // All retries exhausted — clean up
13781384 memset (entry->ephemeral_prv , 0 , PRV_KEY_SIZE );
0 commit comments