Try to clean up the file change observer when restarting

This is all theoretical.
This commit is contained in:
Isaiah Odhner 2023-04-19 14:32:31 -04:00
parent 3c2ff7b3d8
commit 509618c1be

View File

@ -28,6 +28,8 @@ from windows import Window, DialogWindow
from localization.i18n import get as _, load_language from localization.i18n import get as _, load_language
observer = None
def restart_program(): def restart_program():
"""Restarts the current program, after file objects and descriptors cleanup""" """Restarts the current program, after file objects and descriptors cleanup"""
@ -40,6 +42,14 @@ def restart_program():
except Exception as e: except Exception as e:
print("Error stopping application mode. The command line may not work as expected.", e) print("Error stopping application mode. The command line may not work as expected.", e)
try:
observer.stop()
observer.join(timeout=1)
if observer.is_alive:
print("Timed out waiting for file change observer thread to stop.")
except Exception as e:
print("Error stopping file change observer:", e)
try: try:
p = psutil.Process(os.getpid()) p = psutil.Process(os.getpid())
for handler in p.open_files() + p.connections(): for handler in p.open_files() + p.connections():
@ -64,6 +74,7 @@ class RestartHandler(PatternMatchingEventHandler):
def restart_on_changes(): def restart_on_changes():
"""Restarts the current program when a file is changed""" """Restarts the current program when a file is changed"""
global observer
observer = Observer() observer = Observer()
observer.schedule(RestartHandler( observer.schedule(RestartHandler(
# Don't need to restart on changes to .css, since Textual will reload them in --dev mode # Don't need to restart on changes to .css, since Textual will reload them in --dev mode