Skip to content

Commit e3af5de

Browse files
committed
Clearer service exceptions
- if child service can't start - ValidationError mildly more helpful than RuntimeError Also, prefer run_child_service over run_task, because it has more info to give better error messages.
1 parent 040674c commit e3af5de

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

p2p/peer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ def unsubscribe(self, subscriber: PeerSubscriber) -> None:
732732
peer.remove_subscriber(subscriber)
733733

734734
async def start_peer(self, peer: BasePeer) -> None:
735-
self.run_task(peer.run())
735+
self.run_child_service(peer)
736736
await self.wait(peer.events.started.wait(), timeout=1)
737737
try:
738738
# Although connect() may seem like a more appropriate place to perform the DAO fork

p2p/service.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
)
1414
from weakref import WeakSet
1515

16-
from eth.tools.logging import TraceLogger
17-
1816
from cancel_token import CancelToken, OperationCancelled
17+
from eth_utils import (
18+
ValidationError,
19+
)
20+
21+
from eth.tools.logging import TraceLogger
1922

2023
from p2p.cancellable import CancellableMixin
2124
from p2p.utils import get_asyncio_executor
@@ -88,9 +91,9 @@ async def run(
8891
finished_callback (if one was passed).
8992
"""
9093
if self.is_running:
91-
raise RuntimeError("Cannot start the service while it's already running")
94+
raise ValidationError("Cannot start the service while it's already running")
9295
elif self.is_cancelled:
93-
raise RuntimeError("Cannot restart a service that has already been cancelled")
96+
raise ValidationError("Cannot restart a service that has already been cancelled")
9497

9598
if finished_callback:
9699
self._finished_callbacks.append(finished_callback)
@@ -144,6 +147,15 @@ def run_child_service(self, child_service: 'BaseService') -> None:
144147
"""
145148
Run a child service and keep a reference to it to be considered during the cleanup.
146149
"""
150+
if child_service.is_running:
151+
raise ValidationError(
152+
f"Can't start service {child_service!r}, child of {self!r}: it's already running"
153+
)
154+
elif child_service.is_cancelled:
155+
raise ValidationError(
156+
f"Can't restart {child_service!r}, child of {self!r}: it's already completed"
157+
)
158+
147159
self._child_services.add(child_service)
148160
self.run_task(child_service.run())
149161

@@ -153,6 +165,16 @@ def run_daemon(self, service: 'BaseService') -> None:
153165
154166
If the service finishes while we're still running, we'll terminate as well.
155167
"""
168+
if service.is_running:
169+
raise ValidationError(
170+
f"Can't start daemon {child_service!r}, child of {self!r}: it's already running"
171+
)
172+
elif service.is_cancelled:
173+
raise ValidationError(
174+
f"Can't restart daemon {service!r}, child of {self!r}: it's already completed"
175+
)
176+
177+
156178
self._child_services.add(service)
157179

158180
async def _run_daemon_wrapper() -> None:
@@ -193,7 +215,7 @@ async def cancel(self) -> None:
193215
self.logger.warning("Tried to cancel %s, but it was already cancelled", self)
194216
return
195217
elif not self.is_running:
196-
raise RuntimeError("Cannot cancel a service that has not been started")
218+
raise ValidationError("Cannot cancel a service that has not been started")
197219

198220
self.logger.debug("Cancelling %s", self)
199221
self.events.cancelled.set()

0 commit comments

Comments
 (0)