libp2p / libp2p/py-libp2p

Feat: Add system-wide stream handler unregister method.

Open
#1,227 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
624
Forks
256
Avg merge
1d 34m
Merged PRs (30d)
47

Description

Summary

py-libp2p supports registering protocol stream handlers via set_stream_handler, but does not provide a guaranteed, system-wide way to unregister them.

This creates inconsistent service lifecycle behavior across modules.

Motivation

We need lifecycle symmetry for stream protocols in existing service lifecycle methods:

  • service startup path should register handler(s)
  • service shutdown path should unregister handler(s).

Without a standard unregister API, services currently use module-specific workarounds, which makes behavior inconsistent and harder to reason about on long-running hosts.

Current state

Core interfaces currently expose registration but not guaranteed removal:

  • IHost.set_stream_handler(...):
    link
  • IMultiselectMuxer.add_handler(...):
    link

No required remove_stream_handler / remove_handler contract exists at those interface levels.

Existing workaround patterns

  1. Flags-only stop (service marked stopped, handler still registered)

    • Bitswap stop path:
      link
  2. Empty handler replacement as pseudo-unregister

    • DCUtR sets an empty_handler:
      link
  3. Optional unregister with fallback if method is missing

    • Relay protocol tries remove_stream_handler, falls back on AttributeError:
      link

Proposed change (stream scope only)

  1. Add interface methods:
  • IHost.remove_stream_handler(protocol_id: TProtocol) -> None
  • IMultiselectMuxer.remove_handler(protocol: TProtocol) -> None
  1. Implement in core:
  • BasicHost.remove_stream_handler(...) delegates to multiselect
  • Multiselect.remove_handler(...) removes the protocol from handler map
  1. Service migration (follow-up in same or separate PRs):
  • Update services that register handlers to unregister on stop/teardown
    (perf, bitswap, kad_dht, relay/DCUtR, etc.)

Expected impact

  • Consistent register/unregister semantics for stream protocols during service startup/shutdown
  • Fewer ad-hoc unregister workarounds
  • Cleaner behavior for long-lived hosts and protocol restarts
Are you planning to do it yourself in a pull request ?

yes

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the IHost and IMultiselectMuxer contracts in libp2p/abc.py, then locate BasicHost and Multiselect implementations. Confirm that BasicHost delegates removal and Multiselect removes the protocol from its handler map, using the listed bitswap, DCUtR, relay, perf, and kad_dht lifecycle paths as migration context. Done means the interfaces and core implementations provide the removal API with consistent stream-handler teardown semantics.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.