fix: Enhance error handling in echo stream handler to manage stream closure and exceptions

This commit is contained in:
yashksaini-coder
2025-08-20 18:29:35 +05:30
parent c2c91b8c58
commit 5b9bec8e28

View File

@ -15,6 +15,9 @@ from libp2p.custom_types import (
from libp2p.network.stream.net_stream import ( from libp2p.network.stream.net_stream import (
INetStream, INetStream,
) )
from libp2p.network.stream.exceptions import (
StreamEOF,
)
from libp2p.peer.peerinfo import ( from libp2p.peer.peerinfo import (
info_from_p2p_addr, info_from_p2p_addr,
) )
@ -27,10 +30,19 @@ MAX_READ_LEN = 2**32 - 1
async def _echo_stream_handler(stream: INetStream) -> None: async def _echo_stream_handler(stream: INetStream) -> None:
# Wait until EOF try:
msg = await stream.read(MAX_READ_LEN) peer_id = stream.muxed_conn.peer_id
await stream.write(msg) print(f"Received connection from {peer_id}")
await stream.close() # Wait until EOF
msg = await stream.read(MAX_READ_LEN)
print(f"Echoing message: {msg.decode('utf-8')}")
await stream.write(msg)
except StreamEOF:
print("Stream closed by remote peer.")
except Exception as e:
print(f"Error in echo handler: {e}")
finally:
await stream.close()
async def run(port: int, destination: str, seed: int | None = None) -> None: async def run(port: int, destination: str, seed: int | None = None) -> None:
@ -63,8 +75,7 @@ async def run(port: int, destination: str, seed: int | None = None) -> None:
print( print(
"\nRun this from the same folder in another console:\n\n" "\nRun this from the same folder in another console:\n\n"
f"echo-demo " f"echo-demo -d {host.get_addrs()[0]}\n"
f"-d {host.get_addrs()[0]}\n"
) )
print("Waiting for incoming connections...") print("Waiting for incoming connections...")
await trio.sleep_forever() await trio.sleep_forever()