Skip to content

fix(kad): keep unreported protocol status when support is unchanged - #6649

Open
0xShadowX wants to merge 2 commits into
libp2p:masterfrom
0xShadowX:kad-protocol-confirmed
Open

0xShadowX wants to merge 2 commits into
libp2p:masterfrom
0xShadowX:kad-protocol-confirmed

Conversation

@0xShadowX

Copy link
Copy Markdown

A remote that speaks kademlia can end up never being added to the routing table when another behaviour polled before kad in a #[derive(NetworkBehaviour)] reports several protocol changes back to back (identify does this when it emits Added and Removed together).

The kad handler keeps a ProtocolStatus { supported, reported } and only emits ProtocolConfirmed from poll while reported is false. compute_new_protocol_status returns reported: true when support did not change, without looking at whether the current status was already reported. So if the status is { supported: true, reported: false } (first report or first negotiated stream) and another report that doesn't touch kademlia comes in before the handler is polled, the confirmation is dropped. on_connection_handler_event never sees ProtocolConfirmed, so the peer is not inserted and the connection is not marked as connected.

The fix returns current_status when nothing changed. The quickcheck property asserted the old behaviour, so it now checks that reported is carried over.

Tests:

cargo test -p libp2p-kad --lib unreported_status_stays_unreported_on_unrelated_protocol_change
cargo test -p libp2p-kad --test protocol_confirmation

The integration test uses a small behaviour polled before kad that reports kademlia and then an unrelated protocol. On master it times out waiting for RoutingUpdated.

kad 0.49.0 is released, so this bumps it to 0.49.1.

  • tests added, fail on master
  • changelog entry
  • cargo clippy -p libp2p-kad --all-targets --all-features clean

`compute_new_protocol_status` returned `reported: true` whenever kademlia
support did not change, even if the current status had never been
reported. A second protocol report arriving before the handler was polled
therefore swallowed `ProtocolConfirmed`, and the peer was never added to
the routing table on that connection.

Return the current status unchanged instead.
@mergify

mergify Bot commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts. Could you please resolve them @0xShadowX? 🙏

This branch has not been deployed

No deployments
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