Skip to content

Commit

Permalink
Fixed improper closing of resources on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
morpheus65535 authored Jan 11, 2024
1 parent c17d127 commit 0807bd9
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 47 deletions.
21 changes: 13 additions & 8 deletions bazarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,27 @@ def get_python_path():

def end_child_process(ep):
try:
ep.kill()
if os.name != 'nt':
try:
ep.send_signal(signal.SIGINT)
except ProcessLookupError:
pass
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))
signal.signal(signal.SIGTERM, lambda signal_no, frame: end_child_process(ep))


def check_status():
Expand Down
10 changes: 9 additions & 1 deletion bazarr/app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import logging
import os
import flask_migrate
import signal

from dogpile.cache import make_region
from datetime import datetime

from sqlalchemy import create_engine, inspect, DateTime, ForeignKey, Integer, LargeBinary, Text, func, text, BigInteger
# importing here to be indirectly imported in other modules later
from sqlalchemy import update, delete, select, func # noqa W0611
from sqlalchemy.orm import scoped_session, sessionmaker, mapped_column
from sqlalchemy.orm import scoped_session, sessionmaker, mapped_column, close_all_sessions
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.pool import NullPool

Expand Down Expand Up @@ -74,11 +75,18 @@ def set_sqlite_pragma(dbapi_connection, connection_record):
database = scoped_session(session_factory)


def close_database():
close_all_sessions()
engine.dispose()


@atexit.register
def _stop_worker_threads():
database.remove()


signal.signal(signal.SIGTERM, lambda signal_no, frame: close_database())

Base = declarative_base()
metadata = Base.metadata

Expand Down
55 changes: 23 additions & 32 deletions bazarr/app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .ui import ui_bp
from .get_args import args
from .config import settings, base_url
from .database import database
from .database import close_database
from .app import create_app

app = create_app()
Expand Down Expand Up @@ -63,49 +63,40 @@ def configure_server(self):
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:
self.server.close()
stop_file = io.open(os.path.join(args.config_dir, "bazarr.stop"), "w", encoding='UTF-8')
except Exception as e:
logging.error(f'BAZARR Cannot stop Waitress: {repr(e)}')
logging.error(f'BAZARR Cannot create stop file: {repr(e)}')
else:
database.close()
try:
stop_file = io.open(os.path.join(args.config_dir, "bazarr.stop"), "w", encoding='UTF-8')
except Exception as e:
logging.error(f'BAZARR Cannot create stop file: {repr(e)}')
else:
logging.info('Bazarr is being shutdown...')
stop_file.write(str(''))
stop_file.close()
os._exit(0)
logging.info('Bazarr is being shutdown...')
stop_file.write(str(''))
stop_file.close()
close_database()
self.server.close()
os._exit(0)

def restart(self):
try:
self.server.close()
restart_file = io.open(os.path.join(args.config_dir, "bazarr.restart"), "w", encoding='UTF-8')
except Exception as e:
logging.error(f'BAZARR Cannot stop Waitress: {repr(e)}')
logging.error(f'BAZARR Cannot create restart file: {repr(e)}')
else:
database.close()
try:
restart_file = io.open(os.path.join(args.config_dir, "bazarr.restart"), "w", encoding='UTF-8')
except Exception as e:
logging.error(f'BAZARR Cannot create restart file: {repr(e)}')
else:
logging.info('Bazarr is being restarted...')
restart_file.write(str(''))
restart_file.close()
os._exit(0)
logging.info('Bazarr is being restarted...')
restart_file.write(str(''))
restart_file.close()
close_database()
self.server.close()
os._exit(0)


webserver = Server()
10 changes: 8 additions & 2 deletions bazarr/app/signalr_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 2 additions & 0 deletions bazarr/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 8 additions & 4 deletions bazarr/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# coding=utf-8

import os
import io
import logging

from threading import Thread

Expand Down Expand Up @@ -75,9 +73,15 @@

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__":
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 0807bd9

Please sign in to comment.