Handle errors from

- `read_delim`
    - `read_varint_prefixed_bytes`
    - `decode_uvarint_from_stream`
This commit is contained in:
NIC619
2019-09-15 16:58:08 +08:00
parent 905a473ac3
commit 879f193aa1
4 changed files with 34 additions and 11 deletions

View File

@ -16,12 +16,14 @@ from typing import (
import base58
from lru import LRU
from libp2p.exceptions import ValidationError
from libp2p.exceptions import ParseError, ValidationError
from libp2p.host.host_interface import IHost
from libp2p.io.exceptions import IncompleteReadError
from libp2p.network.stream.net_stream_interface import INetStream
from libp2p.peer.id import ID
from libp2p.typing import TProtocol
from libp2p.utils import encode_varint_prefixed, read_varint_prefixed_bytes
from libp2p.network.stream.exceptions import StreamEOF, StreamReset
from .pb import rpc_pb2
from .pubsub_notifee import PubsubNotifee
@ -154,7 +156,13 @@ class Pubsub:
peer_id = stream.mplex_conn.peer_id
while True:
incoming: bytes = await read_varint_prefixed_bytes(stream)
try:
incoming: bytes = await read_varint_prefixed_bytes(stream)
except (ParseError, IncompleteReadError) as error:
logger.debug(
"read corrupted data from peer %s, error=%s", peer_id, error
)
continue
rpc_incoming: rpc_pb2.RPC = rpc_pb2.RPC()
rpc_incoming.ParseFromString(incoming)
if rpc_incoming.publish:
@ -228,7 +236,13 @@ class Pubsub:
on one of the supported pubsub protocols.
:param stream: newly created stream
"""
await self.continuously_read_stream(stream)
try:
await self.continuously_read_stream(stream)
except (StreamEOF, StreamReset) as error:
logger.debug("fail to read from stream, error=%s", error)
stream.reset()
# TODO: what to do when the stream is terminated?
# disconnect the peer?
async def _handle_new_peer(self, peer_id: ID) -> None:
stream: INetStream = await self.host.new_stream(peer_id, self.protocols)