Skip to content

fix(kad-dht): reset stream on RPC handler error#3509

Merged
tabcat merged 2 commits into
libp2p:mainfrom
tabcat:fix/kad-dht-reset-stream-on-rpc-error
May 16, 2026
Merged

fix(kad-dht): reset stream on RPC handler error#3509
tabcat merged 2 commits into
libp2p:mainfrom
tabcat:fix/kad-dht-reset-stream-on-rpc-error

Conversation

@tabcat
Copy link
Copy Markdown
Member

@tabcat tabcat commented May 15, 2026

Description

When an incoming DHT message handler throws, RPC.handleMessage swallowed the error and returned no response, leaving the stream open. The requesting peer then waited for a response until its request timed out (~10s) before the stream was torn down.

The stream is now reset as soon as a handler throws. handleMessage re-throws handler errors — and throws on an unknown message type — instead of swallowing them, and onIncomingStream aborts the stream, so the remote peer sees the failure immediately.

Notes & open questions

The kad-dht spec doesn't define how a node should respond to a request it can't fulfil — it only specifies echoing the request back on success. This follows go-libp2p, which resets the stream on any handler error. Before this change js-libp2p left the stream open and the requesting peer timed out.

test/rpc/index.node.ts is renamed to index.spec.ts — the test runner only matches *.spec.*js, so as index.node.ts neither its existing test nor the new one would run.

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

tabcat added 2 commits May 16, 2026 04:25
Abort the stream when an incoming message handler throws so the remote
sees the failure immediately instead of waiting for a timeout. Also
renames the orphaned rpc index test to .spec.ts so the runner picks it up.
Replace the redundant no-handler log with a single error log in the
onIncomingStream catch so every handler failure is observable.
@tabcat tabcat requested a review from a team as a code owner May 15, 2026 23:22
@tabcat tabcat merged commit 8878139 into libp2p:main May 16, 2026
47 of 48 checks passed
@tabcat tabcat mentioned this pull request May 15, 2026
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.

2 participants