Skip to content

Commit

Permalink
signal handling improvements; types
Browse files Browse the repository at this point in the history
  • Loading branch information
c0m4r committed Jan 17, 2024
1 parent 1fc9ccc commit 2c9e361
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 13 additions & 5 deletions paranoya.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
import sys
import threading
import traceback
import signal
from signal import signal, SIGPIPE, SIG_DFL, SIGTERM, SIGINT

from bisect import bisect_left
from collections import Counter
from subprocess import Popen, PIPE, run
from types import FrameType
from typing import Optional

# paranoya modules
from lib.paranoya_args import parser
Expand Down Expand Up @@ -77,6 +79,8 @@
print(e)
sys.exit(0)

signal(SIGPIPE, SIG_DFL)


def ioc_contains(sorted_list, value):
"""
Expand Down Expand Up @@ -1766,7 +1770,7 @@ def remove_pidfile() -> None:
os.remove(args.pidfile)


def sigint_handler(signal_name, frame) -> None:
def sigint_handler(signal_name: int, frame: Optional[FrameType]) -> None:
"""
SIGINT handler
"""
Expand All @@ -1779,15 +1783,19 @@ def sigint_handler(signal_name, frame) -> None:
except Exception:
print("paranoya's work has been interrupted by a human. Returning to Asgard.")
remove_pidfile()
if args.debug:
print(signal_name, frame)
sys.exit(0)


def sigterm_handler(signal_name, frame) -> None:
def sigterm_handler(signal_name: int, frame: Optional[FrameType]) -> None:
"""
SIGTERM handler
"""
remove_pidfile()
print("paranoya's work has been interrupted by a SIGTERM. Returning to Asgard.")
if args.debug:
print(signal_name, frame)
sys.exit(0)


Expand All @@ -1796,10 +1804,10 @@ def signal_handlers() -> None:
Signal handlers
"""
# Signal handler for CTRL+C
signal.signal(signal.SIGINT, sigint_handler)
signal(SIGINT, sigint_handler)

# Signal handler for SIGTERM
signal.signal(signal.SIGTERM, sigterm_handler)
signal(SIGTERM, sigterm_handler)


def get_platform() -> str:
Expand Down
4 changes: 2 additions & 2 deletions upgrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from shutil import copyfileobj
from signal import signal, SIGPIPE, SIG_DFL, SIGTERM, SIGINT
from types import FrameType
from typing import IO
from typing import IO, Optional
from zipfile import ZipFile

# paranoya modules
Expand Down Expand Up @@ -488,7 +488,7 @@ def get_application_path() -> str:
return ""


def sig_handler(signal_name: int, frame: FrameType) -> None:
def sig_handler(signal_name: int, frame: Optional[FrameType]) -> None:
"""
signal handler
"""
Expand Down

0 comments on commit 2c9e361

Please sign in to comment.