Skip to content

fix(yamux): add concurrency lock to YamuxStream to prevent data corruption #794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion libp2p/host/basic_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,20 @@ def get_peerstore(self) -> IPeerStore:

def get_mux(self) -> Multiselect:
"""
:return: mux instance of host
Retrieve the muxer instance for the host.

Returns
-------
Multiselect
The muxer instance of the host. Never returns None.

Raises
------
RuntimeError
If the multiselect instance is not initialized.
"""
if not hasattr(self, "multiselect") or self.multiselect is None:
raise RuntimeError("Multiselect instance not initialized")
return self.multiselect

def get_addrs(self) -> list[multiaddr.Multiaddr]:
Expand Down
50 changes: 37 additions & 13 deletions libp2p/identity/identify/identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,43 @@ def _remote_address_to_multiaddr(
def _mk_identify_protobuf(
host: IHost, observed_multiaddr: Multiaddr | None
) -> Identify:
public_key = host.get_public_key()
laddrs = host.get_addrs()
protocols = host.get_mux().get_protocols()

observed_addr = observed_multiaddr.to_bytes() if observed_multiaddr else b""
return Identify(
protocol_version=PROTOCOL_VERSION,
agent_version=AGENT_VERSION,
public_key=public_key.serialize(),
listen_addrs=map(_multiaddr_to_bytes, laddrs),
observed_addr=observed_addr,
protocols=protocols,
)
"""
Create an Identify protobuf message.

Parameters
----------
host : IHost
The host to create the identify message for.
observed_multiaddr : Multiaddr | None
The observed multiaddr of the peer, if any.

Returns
-------
Identify
The identify protobuf message.

Raises
------
RuntimeError
If required host information is not available.
"""
try:
public_key = host.get_public_key()
laddrs = host.get_addrs()
mux = host.get_mux()
protocols = tuple(str(p) for p in mux.get_protocols()) # get_protocols() now excludes None

observed_addr = observed_multiaddr.to_bytes() if observed_multiaddr else b""
return Identify(
protocol_version=PROTOCOL_VERSION,
agent_version=AGENT_VERSION,
public_key=public_key.serialize(),
listen_addrs=map(_multiaddr_to_bytes, laddrs),
observed_addr=observed_addr,
protocols=protocols,
)
except Exception as e:
raise RuntimeError(f"Failed to create identify protobuf: {str(e)}")


def identify_handler_for(host: IHost) -> StreamHandlerFn:
Expand Down
11 changes: 11 additions & 0 deletions libp2p/protocol_muxer/multiselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ async def negotiate(
except trio.TooSlowError:
raise MultiselectError("handshake read timeout")

def get_protocols(self) -> tuple[TProtocol, ...]:
"""
Retrieve the protocols for which handlers have been registered.

Returns
-------
tuple[TProtocol, ...]
A tuple of registered protocol names, excluding None values.
"""
return tuple(p for p in self.handlers.keys() if p is not None)

async def handshake(self, communicator: IMultiselectCommunicator) -> None:
"""
Perform handshake to agree on multiselect protocol.
Expand Down
Loading