Fixed improper closing of resources on exit

This commit is contained in:
morpheus65535 2024-01-02 23:16:36 -05:00
parent 902d1e62b8
commit 117d37ad92
6 changed files with 37 additions and 23 deletions

View File

@ -52,22 +52,23 @@ dir_name = os.path.dirname(__file__)
def end_child_process(ep):
try:
ep.kill()
if os.name != 'nt':
ep.send_signal(signal.SIGINT)
else:
import win32api
import win32con
try:
win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT, ep.pid)
except KeyboardInterrupt:
pass
except:
pass
def terminate_child_process(ep):
try:
ep.terminate()
except:
pass
def start_bazarr():
script = [get_python_path(), "-u", os.path.normcase(os.path.join(dir_name, 'bazarr', 'main.py'))] + sys.argv[1:]
ep = subprocess.Popen(script, stdout=None, stderr=None, stdin=subprocess.DEVNULL)
atexit.register(end_child_process, ep=ep)
signal.signal(signal.SIGTERM, lambda signal_no, frame: terminate_child_process(ep))
def check_status():

View File

@ -8,6 +8,7 @@ import errno
from waitress.server import create_server
from time import sleep
from sqlalchemy.orm import close_all_sessions
from api import api_bp
from .ui import ui_bp
@ -63,15 +64,14 @@ class Server:
self.shutdown()
def start(self):
logging.info(f'BAZARR is started and waiting for request on http://{self.server.effective_host}:'
f'{self.server.effective_port}')
try:
logging.info(f'BAZARR is started and waiting for request on http://{self.server.effective_host}:'
f'{self.server.effective_port}')
try:
self.server.run()
except Exception:
pass
except KeyboardInterrupt:
self.server.run()
except (KeyboardInterrupt, SystemExit):
self.shutdown()
except Exception:
pass
def shutdown(self):
try:
@ -79,7 +79,7 @@ class Server:
except Exception as e:
logging.error(f'BAZARR Cannot stop Waitress: {repr(e)}')
else:
database.close()
close_all_sessions()
try:
stop_file = io.open(os.path.join(args.config_dir, "bazarr.stop"), "w", encoding='UTF-8')
except Exception as e:
@ -96,7 +96,7 @@ class Server:
except Exception as e:
logging.error(f'BAZARR Cannot stop Waitress: {repr(e)}')
else:
database.close()
close_all_sessions()
try:
restart_file = io.open(os.path.join(args.config_dir, "bazarr.restart"), "w", encoding='UTF-8')
except Exception as e:

View File

@ -340,14 +340,20 @@ def consume_queue(queue):
data = queue.popleft()
except IndexError:
pass
except (KeyboardInterrupt, SystemExit):
break
else:
dispatcher(data)
sleep(0.1)
# start both queue consuming threads
threading.Thread(target=consume_queue, args=(sonarr_queue,)).start()
threading.Thread(target=consume_queue, args=(radarr_queue,)).start()
sonarr_queue_thread = threading.Thread(target=consume_queue, args=(sonarr_queue,))
sonarr_queue_thread.daemon = True
sonarr_queue_thread.start()
radarr_queue_thread = threading.Thread(target=consume_queue, args=(radarr_queue,))
radarr_queue_thread.daemon = True
radarr_queue_thread.start()
# instantiate proper SignalR client
sonarr_signalr_client = SonarrSignalrClientLegacy() if get_sonarr_info.version().startswith(('0.', '2.', '3.')) else \

View File

@ -77,6 +77,8 @@ def is_virtualenv():
# deploy requirements.txt
if not args.no_update:
try:
if os.name == 'nt':
import win32api, win32con # noqa E401
import lxml, numpy, webrtcvad, setuptools, PIL # noqa E401
except ImportError:
try:

View File

@ -1,8 +1,6 @@
# coding=utf-8
import os
import io
import logging
from threading import Thread
@ -75,9 +73,15 @@ update_notifier()
if not args.no_signalr:
if settings.general.use_sonarr:
Thread(target=sonarr_signalr_client.start).start()
sonarr_signalr_thread = Thread(target=sonarr_signalr_client.start)
sonarr_signalr_thread.daemon = True
sonarr_signalr_thread.start()
sonarr_signalr_thread.join()
if settings.general.use_radarr:
Thread(target=radarr_signalr_client.start).start()
radarr_signalr_thread = Thread(target=radarr_signalr_client.start)
radarr_signalr_thread.daemon = True
radarr_signalr_thread.start()
radarr_signalr_thread.join()
if __name__ == "__main__":

View File

@ -3,3 +3,4 @@ lxml>=4.3.0, <5.0.0
numpy>=1.12.0
webrtcvad-wheels>=2.0.10
Pillow>=9.0.0 --only-binary=Pillow
pywin32; platform_system == "Windows"