Skip to content

Commit

Permalink
signals workaround for windows (#695)
Browse files Browse the repository at this point in the history
* Contributor: nicolasochem, Effort=1h
* Reviewer: jdsika, Effort=0.5h
---------

Signed-off-by: jdsika <carlo.van-driesten@vdl.digital>
Co-authored-by: jdsika <carlo.van-driesten@vdl.digital>
  • Loading branch information
nicolasochem and jdsika authored Feb 8, 2024
1 parent 648cfde commit 4f97afe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
21 changes: 14 additions & 7 deletions src/pay/payment_producer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _thread
import os
import platform
import signal
import threading
from datetime import datetime, timedelta
Expand Down Expand Up @@ -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",
Expand Down
12 changes: 9 additions & 3 deletions src/util/process_life_cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 4f97afe

Please sign in to comment.