Skip to content

Commit 9b9a832

Browse files
committed
Ensure ethstats plugin uses a timeout to wait on PeerCount
Previously, the Ethstats Plugin waited on the PeerCount forever. But since the PeerPool is not ready by the time that the Ethstats plugin sends the request, it just hangs there waiting and at some point the websocket connection times out. This change introduces a timeout of 500ms for the PeerPool to answer. If the PeerPool does not answer within 500ms, we assume 0 peers and move on. Fixes #1379
1 parent 9a9ad1d commit 9b9a832

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

trinity/plugins/builtin/ethstats/ethstats_service.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
)
88
from p2p.events import (
99
PeerCountRequest,
10-
PeerCountResponse,
1110
)
1211
from p2p.service import (
1312
BaseService,
@@ -134,13 +133,19 @@ def get_node_block(self) -> EthstatsData:
134133
}
135134

136135
async def get_node_stats(self) -> EthstatsData:
137-
response: PeerCountResponse = await self.context.event_bus.request(
138-
PeerCountRequest()
139-
)
136+
137+
try:
138+
peer_count = (await self.wait(
139+
self.context.event_bus.request(PeerCountRequest()),
140+
timeout=0.5
141+
)).peer_count
142+
except TimeoutError:
143+
self.logger.warning("Timeout: PeerPool did not answer PeerCountRequest")
144+
peer_count = 0
140145

141146
return {
142147
'active': True,
143-
'peers': response.peer_count,
148+
'peers': peer_count,
144149
}
145150

146151
def get_chain(self) -> BaseChain:

0 commit comments

Comments
 (0)