Catch expections in PubsubNotifee

Also, add lock to avoid resource race condition
This commit is contained in:
mhchia 2019-12-24 18:31:39 +08:00
parent 2287dc95be
commit 573c049d0f
No known key found for this signature in database
GPG Key ID: 389EFBEA1362589A

View File

@ -16,6 +16,7 @@ class PubsubNotifee(INotifee):
initiator_peers_queue: "trio.MemorySendChannel[ID]"
dead_peers_queue: "trio.MemorySendChannel[ID]"
dead_peers_queue_lock: trio.Lock
def __init__(
self,
@ -29,7 +30,9 @@ class PubsubNotifee(INotifee):
can process dead peers after we disconnect from each other
"""
self.initiator_peers_queue = initiator_peers_queue
self.initiator_peers_queue_lock: trio.Lock()
self.dead_peers_queue = dead_peers_queue
self.dead_peers_queue_lock: trio.Lock()
async def opened_stream(self, network: INetwork, stream: INetStream) -> None:
pass
@ -46,12 +49,17 @@ class PubsubNotifee(INotifee):
:param network: network the connection was opened on
:param conn: connection that was opened
"""
try:
await self.initiator_peers_queue.send(conn.muxed_conn.peer_id)
except trio.BrokenResourceError:
# Raised when the receive channel is closed.
# TODO: Do something with loggers?
...
async with self.initiator_peers_queue_lock:
try:
await self.initiator_peers_queue.send(conn.muxed_conn.peer_id)
except (
trio.BrokenResourceError,
trio.ClosedResourceError,
trio.BusyResourceError,
):
# Raised when the receive channel is closed.
# TODO: Do something with loggers?
...
async def disconnected(self, network: INetwork, conn: INetConn) -> None:
"""
@ -61,7 +69,17 @@ class PubsubNotifee(INotifee):
:param network: network the connection was opened on
:param conn: connection that was opened
"""
await self.dead_peers_queue.send(conn.muxed_conn.peer_id)
async with self.dead_peers_queue_lock:
try:
await self.dead_peers_queue.send(conn.muxed_conn.peer_id)
except (
trio.BrokenResourceError,
trio.ClosedResourceError,
trio.BusyResourceError,
):
# Raised when the receive channel is closed.
# TODO: Do something with loggers?
...
async def listen(self, network: INetwork, multiaddr: Multiaddr) -> None:
pass