From 4f97afe2243505b8a94c47f3ab495894dbc71ea3 Mon Sep 17 00:00:00 2001 From: Nicolas Ochem Date: Thu, 8 Feb 2024 12:24:49 -0800 Subject: [PATCH] signals workaround for windows (#695) * Contributor: nicolasochem, Effort=1h * Reviewer: jdsika, Effort=0.5h --------- Signed-off-by: jdsika Co-authored-by: jdsika --- src/pay/payment_producer.py | 21 ++++++++++++++------- src/util/process_life_cycle.py | 12 +++++++++--- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/pay/payment_producer.py b/src/pay/payment_producer.py index aaf29f13..0e6502dc 100644 --- a/src/pay/payment_producer.py +++ b/src/pay/payment_producer.py @@ -1,5 +1,5 @@ -import _thread import os +import platform import signal import threading from datetime import datetime, timedelta @@ -154,19 +154,26 @@ def exit(self, exit_code): self.life_cycle.is_running() and threading.current_thread() is not threading.main_thread() ): + if platform.system() == "Windows": + abnormal_signal = signal.SIGTERM + normal_signal = signal.SIGTERM + else: + # This will propagate the exit status to main process on linux. + abnormal_signal = signal.SIGUSR2 + normal_signal = signal.SIGUSR1 if self.consumer_failure: - os.kill(os.getpid(), signal.SIGUSR2) + os.kill(os.getpid(), abnormal_signal) logger.debug( - "Payment failure, sending sigusr2 signal to main thread." + "Payment failure, sending abnormal kill signal to main thread." ) elif exit_code != ExitCode.SUCCESS: - os.kill(os.getpid(), signal.SIGUSR2) + os.kill(os.getpid(), abnormal_signal) logger.debug( - "Producer failure, sending sigusr2 signal to main thread." + "Producer failure, sending abnormal kill signal to main thread." ) else: - os.kill(os.getpid(), signal.SIGUSR1) - logger.debug("Sending sigusr1 signal.") + os.kill(os.getpid(), normal_signal) + logger.debug("Sending normal kill signal.") exit_program( exit_code, "TRD Exit triggered by producer", diff --git a/src/util/process_life_cycle.py b/src/util/process_life_cycle.py index a80882b1..af9be5bf 100644 --- a/src/util/process_life_cycle.py +++ b/src/util/process_life_cycle.py @@ -2,7 +2,11 @@ import logging import queue import signal -from _signal import SIGABRT, SIGILL, SIGSEGV, SIGTERM, SIGUSR1, SIGUSR2 +import platform +from signal import SIGABRT, SIGILL, SIGSEGV, SIGTERM + +if platform.system() != "Windows": + from signal import SIGUSR1, SIGUSR2 from enum import Enum, auto from time import sleep @@ -301,9 +305,11 @@ def do_set_up_dirs(self, e): self.__baking_dirs = BakingDirs(self.args, self.__cfg.get_baking_address()) def do_register_signals(self, e): - for sig in (SIGABRT, SIGILL, SIGSEGV, SIGTERM, SIGUSR2): + for sig in (SIGABRT, SIGILL, SIGSEGV, SIGTERM): signal.signal(sig, self.stop_handler) - signal.signal(SIGUSR1, self.producer_exit_handler) + if "SIGUSR1" in globals(): + signal.signal(SIGUSR1, self.producer_exit_handler) + signal.signal(SIGUSR2, self.stop_handler) def do_init_service_fees(self, e): self.__srvc_fee_calc = ServiceFeeCalculator(