Skip to content

Commit 9a9ad1d

Browse files
committed
Ensure Trinity handles SIGTERM send to main process
Currently, when sending a SIGTERM to the Trinity main process, Trinity doesn't shut down gracefully. The main process shuts down but the child processes doesn't. This change does two things: - print out the main process id at boot - handle SIGTERM correctly Relates to #1461
1 parent 7d0f188 commit 9a9ad1d

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

trinity/main.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from argparse import ArgumentParser, Namespace
22
import asyncio
33
import logging
4+
import os
45
import signal
56
from typing import (
67
Any,
@@ -254,35 +255,31 @@ def trinity_boot(args: Namespace,
254255
networking_process.start()
255256
logger.info("Started networking process (pid=%d)", networking_process.pid)
256257

257-
main_endpoint.subscribe(
258-
ShutdownRequest,
259-
lambda ev: kill_trinity_gracefully(
258+
def kill_trinity_with_reason(reason: str) -> None:
259+
kill_trinity_gracefully(
260260
logger,
261261
database_server_process,
262262
networking_process,
263263
plugin_manager,
264264
main_endpoint,
265265
event_bus,
266-
ev.reason
266+
reason=reason
267267
)
268+
269+
main_endpoint.subscribe(
270+
ShutdownRequest,
271+
lambda ev: kill_trinity_with_reason(ev.reason)
268272
)
269273

270274
plugin_manager.prepare(args, trinity_config, extra_kwargs)
271275

272276
try:
273277
loop = asyncio.get_event_loop()
278+
loop.add_signal_handler(signal.SIGTERM, lambda: kill_trinity_with_reason("SIGTERM"))
274279
loop.run_forever()
275280
loop.close()
276281
except KeyboardInterrupt:
277-
kill_trinity_gracefully(
278-
logger,
279-
database_server_process,
280-
networking_process,
281-
plugin_manager,
282-
main_endpoint,
283-
event_bus,
284-
reason="CTRL+C / Keyboard Interrupt"
285-
)
282+
kill_trinity_with_reason("CTRL+C / Keyboard Interrupt")
286283

287284

288285
def kill_trinity_gracefully(logger: logging.Logger,
@@ -363,6 +360,7 @@ def launch_node(args: Namespace, trinity_config: TrinityConfig, endpoint: Endpoi
363360
def display_launch_logs(trinity_config: TrinityConfig) -> None:
364361
logger = logging.getLogger('trinity')
365362
logger.info(TRINITY_HEADER)
363+
logger.info("Started main process (pid=%d)", os.getpid())
366364
logger.info(construct_trinity_client_identifier())
367365
logger.info("Trinity DEBUG log file is created at %s", str(trinity_config.logfile_path))
368366

0 commit comments

Comments
 (0)