Skip to content

Run lookups after dial failure NET-384 - #216

Open
kalabukdima wants to merge 4 commits into
mainfrom
address-cache
Open

Run lookups after dial failure NET-384#216
kalabukdima wants to merge 4 commits into
mainfrom
address-cache

Conversation

@kalabukdima

Copy link
Copy Markdown
Contributor

The current approach

After #199 the clients, e.g. pings collector, are trying to always keep connections to all workers open. This is a separate loop from actually sending the requests to them.
If a request is attempted and there is neither an existing connection nor known addresses of the peer, it just fails immediately. It doesn't trigger new Kademlia lookups because it assumes that the background loop already made the best effort to have that connection open.

The background loop works like this:

  • We schedule DHT lookups for all the known workers, putting them in the queue.
  • Every lookup for a peer finds many other peer+address pairs along the way. Those addresses get saved to the AddressCache.
  • On the next lookup request from the queue, we check the address cache first and only start a Kademlia query if the peer is not yet cached.

As a result

  • Some workers are kept connected. On connection close the find_and_dial is rescheduled and the connection gets re-established.
  • Some workers don't have addresses in DHT and fall out of the loop. (They are usually dead forever and don't return to the network. But if they do, they won't be picked up until the pings collector restarts, which is a separate issue.)

The issue

Every time the worker changes the address, it drops out of the healthy loop. At least until a happy coincidence returns it to the healthy loop, which usually takes >1h.

Why:

  1. The connection to the worker closes, and a lookup is scheduled.
  2. The address cache still holds the peer's addresses. A connection closing doesn't evict anything — only a dial failure does. So the cached address is dialed, but no Kad query is issued.
  3. That dial uses the stale cached addresses and fails. Now AddressCache::on_dial_failure evicts the entry. But nothing re-schedules the address lookup.
  4. Effectively that worker stays disconnected and all requests to it fail with "no addresses for peer".

The fix

The lookups are now scheduled on dial failures caused by unreachable addresses.

On the healthy loop, a stale address triggers a Kad query to get the worker reconnected if there is a reachable address in DHT.
Even if that peer is not reachable yet, it will keep retrying lookups every 30s because Kademlia keeps the last known address and won't evict it. Only if the peer completely drops out of Kademlia (NoAddresses path), it gets dropped from the reconnect loop.

kalabukdima and others added 4 commits August 3, 2026 16:54
`addr_is_reachable` reads the variable on every call and Rust runs a binary's
tests as threads in one process, so the test that set it decided what every
concurrently running test saw.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The reconnect path fires on `ConnectionClosed`, while the address cache still
holds the dead address, so it dials instead of looking the worker up. The dial
then empties the cache and nothing re-arms: `reconnect_queue` is fed only by
connection closures, and the whitelist sweep is suppressed while the registered
set is unchanged.

Part of NET-384

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The reconnect path fires only on ConnectionClosed, and at that point the
address cache still holds the dead address, so it is dialed instead of
looked up. That dial fails and evicts the entry, and nothing re-arms: a
failed dial establishes no connection, so no close event follows, and the
whitelist sweep is suppressed while the registered set is unchanged. The
worker stayed undialable until unrelated DHT traffic happened to supply
an address.

NoAddresses is excluded deliberately -- it is the error a fruitless
lookup produces, so scheduling on it would let recovery feed itself.

Part of NET-384

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.

1 participant