Skip to content

Commit fb73938

Browse files
committed
Use BaseService.run_daemon() where apropriate
1 parent dee977e commit fb73938

File tree

6 files changed

+15
-8
lines changed

6 files changed

+15
-8
lines changed

p2p/DEVELOPMENT.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ library.
2020
- If your service needs to run coroutines in the background, you should use the `BaseService.run_task()` method and
2121
ensure they exit when `is_running` is False or when the cancel token is triggered.
2222
- If your service runs other services in the background, you should pass your CancelToken down to
23-
those services and run those using `BaseService.run_child_service()`.
23+
those services and run those using `BaseService.run_child_service()`, or
24+
`BaseService.run_daemon()` if you want the parent to be terminated when the child dies
25+
unexpectedly.
2426

2527
```Python
2628
class Node(BaseService):
2729
async def _run(self):
2830
self.discovery = DiscoveryService(token=self.cancel_token)
29-
self.run_child_service(self.discovery)
31+
self.run_daemon(self.discovery)
3032
self.run_task(self.discovery.bootstrap())
33+
# UPNP service is still experimental and not essential, so we don't use run_daemon() for
34+
# it as that means if it crashes we'd be terminated as well.
35+
self.run_child_service(UPnPService(token=self.cancel_token))
3136
# Node's run logic goes here...
3237

3338
```

trinity/nodes/light.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, plugin_manager: PluginManager, chain_config: ChainConfig) ->
4545
self.notify_resource_available()
4646

4747
async def _run(self) -> None:
48-
self.run_child_service(self._peer_chain)
48+
self.run_daemon(self._peer_chain)
4949
await super()._run()
5050

5151
def get_chain(self) -> LightDispatchChain:

trinity/protocol/common/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, peer: BasePeer) -> None:
3434
async def _run(self) -> None:
3535
for attr in self._managers.keys():
3636
manager = getattr(self, attr)
37-
self.run_child_service(manager)
37+
self.run_daemon(manager)
3838

3939
await self.cancel_token.wait()
4040

trinity/protocol/eth/peer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def get_extra_stats(self) -> List[str]:
3737
def requests(self) -> ETHRequestResponseHandler:
3838
if self._requests is None:
3939
self._requests = ETHRequestResponseHandler(self)
40-
self.run_child_service(self._requests)
40+
self.run_daemon(self._requests)
4141
return self._requests
4242

4343
@property

trinity/protocol/les/peer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def get_extra_stats(self) -> List[str]:
4848
def requests(self) -> LESRequestResponseHandler:
4949
if self._requests is None:
5050
self._requests = LESRequestResponseHandler(self)
51-
self.run_child_service(self._requests)
51+
self.run_daemon(self._requests)
5252
return self._requests
5353

5454
@property

trinity/server.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,10 @@ async def _run(self) -> None:
151151
self.privkey, addr, self.bootstrap_nodes, self.preferred_nodes)
152152
self.discovery = DiscoveryService(
153153
discovery_proto, self.peer_pool, self.port, self.cancel_token)
154-
self.run_child_service(self.peer_pool)
155-
self.run_child_service(self.discovery)
154+
self.run_daemon(self.peer_pool)
155+
self.run_daemon(self.discovery)
156+
# UPNP service is still experimental and not essential, so we don't use run_daemon() for
157+
# it as that means if it crashes we'd be terminated as well.
156158
self.run_child_service(self.upnp_service)
157159
self.syncer = self._make_syncer(self.peer_pool)
158160
await self.syncer.run()

0 commit comments

Comments
 (0)