Skip to content

Commit db49fd6

Browse files
authored
Updated keyboard interrupt handling code
1 parent 2964a0c commit db49fd6

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

bazarr.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ def check_status():
107107
child_process = start_bazarr()
108108

109109

110+
def is_process_running(pid):
111+
commands = {
112+
"win": ["tasklist", "/FI", f"PID eq {pid}"],
113+
"linux": ["ps", "-eo", "pid"],
114+
"darwin": ["ps", "-ax", "-o", "pid"]
115+
}
116+
117+
# Determine OS and execute corresponding command
118+
for key in commands:
119+
if sys.platform.startswith(key):
120+
result = subprocess.run(commands[key], capture_output=True, text=True)
121+
return str(pid) in result.stdout.split()
122+
123+
print("Unsupported OS")
124+
return False
125+
110126
def interrupt_handler(signum, frame):
111127
# catch and ignore keyboard interrupt Ctrl-C
112128
# the child process Server object will catch SIGINT and perform an orderly shutdown
@@ -116,7 +132,9 @@ def interrupt_handler(signum, frame):
116132
interrupted = True
117133
print('Handling keyboard interrupt...')
118134
else:
119-
print("Stop doing that! I heard you the first time!")
135+
if not is_process_running(child_process):
136+
# this will be caught by the main loop below
137+
raise SystemExit(EXIT_INTERRUPT)
120138

121139

122140
if __name__ == '__main__':

0 commit comments

Comments
 (0)