Skip to content

Commit 6b4872c

Browse files
committed
Ensure plugins don't hang the process on exit
1 parent 2260665 commit 6b4872c

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

fixtures

Submodule fixtures updated 14326 files

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"ipython>=6.2.1,<7.0.0",
4343
"plyvel==1.0.5",
4444
"web3==4.4.1",
45-
"lahja==0.6.1",
45+
"lahja==0.8.0",
4646
"uvloop==0.11.2;platform_system=='Linux' or platform_system=='Darwin'",
4747
],
4848
'test': [

trinity/main.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
from trinity.utils.profiling import (
7777
setup_cprofiler,
7878
)
79+
from trinity.utils.shutdown import (
80+
exit_on_signal
81+
)
7982
from trinity.utils.version import (
8083
construct_trinity_client_identifier,
8184
)
@@ -260,6 +263,7 @@ def trinity_boot(args: Namespace,
260263
database_server_process,
261264
networking_process,
262265
plugin_manager,
266+
main_endpoint,
263267
event_bus
264268
)
265269
)
@@ -279,6 +283,7 @@ def trinity_boot(args: Namespace,
279283
database_server_process,
280284
networking_process,
281285
plugin_manager,
286+
main_endpoint,
282287
event_bus
283288
)
284289

@@ -287,6 +292,7 @@ def kill_trinity_gracefully(logger: logging.Logger,
287292
database_server_process: Any,
288293
networking_process: Any,
289294
plugin_manager: PluginManager,
295+
main_endpoint: Endpoint,
290296
event_bus: EventBus,
291297
message: str="Trinity shudown complete\n") -> None:
292298
# When a user hits Ctrl+C in the terminal, the SIGINT is sent to all processes in the
@@ -301,7 +307,8 @@ def kill_trinity_gracefully(logger: logging.Logger,
301307
# perform a non-gracefull shutdown if the process takes too long to terminate.
302308
logger.info('Keyboard Interrupt: Stopping')
303309
plugin_manager.shutdown()
304-
event_bus.shutdown()
310+
main_endpoint.stop()
311+
event_bus.stop()
305312
kill_process_gracefully(database_server_process, logger)
306313
logger.info('DB server process (pid=%d) terminated', database_server_process.pid)
307314
# XXX: This short sleep here seems to avoid us hitting a deadlock when attempting to
@@ -336,21 +343,6 @@ def _sigint_handler(*args: Any) -> None:
336343
raise
337344

338345

339-
async def exit_on_signal(service_to_exit: BaseService) -> None:
340-
loop = service_to_exit.get_event_loop()
341-
sigint_received = asyncio.Event()
342-
for sig in [signal.SIGINT, signal.SIGTERM]:
343-
# TODO also support Windows
344-
loop.add_signal_handler(sig, sigint_received.set)
345-
346-
await sigint_received.wait()
347-
try:
348-
await service_to_exit.cancel()
349-
service_to_exit._executor.shutdown(wait=True)
350-
finally:
351-
loop.stop()
352-
353-
354346
@setup_cprofiler('launch_node')
355347
@with_queued_logging
356348
def launch_node(args: Namespace, chain_config: ChainConfig, endpoint: Endpoint) -> None:

trinity/utils/shutdown.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import asyncio
2+
import signal
3+
4+
from lahja import (
5+
Endpoint,
6+
)
7+
8+
from p2p.service import (
9+
BaseService,
10+
)
11+
12+
13+
async def exit_on_signal(service_to_exit: BaseService, endpoint: Endpoint = None) -> None:
14+
loop = service_to_exit.get_event_loop()
15+
sigint_received = asyncio.Event()
16+
for sig in [signal.SIGINT, signal.SIGTERM]:
17+
# TODO also support Windows
18+
loop.add_signal_handler(sig, sigint_received.set)
19+
20+
await sigint_received.wait()
21+
try:
22+
await service_to_exit.cancel()
23+
if endpoint is not None:
24+
endpoint.stop()
25+
service_to_exit._executor.shutdown(wait=True)
26+
finally:
27+
loop.stop()

0 commit comments

Comments
 (0)