From 677b2e8e4711161f9733e8ce90426680e7168cf4 Mon Sep 17 00:00:00 2001 From: Zhanghao Wu Date: Mon, 17 Jul 2023 09:21:40 -0700 Subject: [PATCH] [Core] Avoid signal when sky.launch is not executed in the main thread (#2252) * avoid signal when not used in main thread * avoid signal handling for spot tail logs * import order --- sky/backends/cloud_vm_ray_backend.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/sky/backends/cloud_vm_ray_backend.py b/sky/backends/cloud_vm_ray_backend.py index 3f776ec4dfe..7e3ae02aeee 100644 --- a/sky/backends/cloud_vm_ray_backend.py +++ b/sky/backends/cloud_vm_ray_backend.py @@ -14,6 +14,7 @@ import sys import tempfile import textwrap +import threading import time import typing from typing import Dict, Iterable, List, Optional, Tuple, Union, Set @@ -3262,8 +3263,9 @@ def tail_logs(self, # With the stdin=subprocess.DEVNULL, the ctrl-c will not directly # kill the process, so we need to handle it manually here. - signal.signal(signal.SIGINT, backend_utils.interrupt_handler) - signal.signal(signal.SIGTSTP, backend_utils.stop_handler) + if threading.current_thread() is threading.main_thread(): + signal.signal(signal.SIGINT, backend_utils.interrupt_handler) + signal.signal(signal.SIGTSTP, backend_utils.stop_handler) try: returncode = self.run_on_head( handle, @@ -3296,8 +3298,9 @@ def tail_spot_logs(self, # With the stdin=subprocess.DEVNULL, the ctrl-c will not directly # kill the process, so we need to handle it manually here. - signal.signal(signal.SIGINT, backend_utils.interrupt_handler) - signal.signal(signal.SIGTSTP, backend_utils.stop_handler) + if threading.current_thread() is threading.main_thread(): + signal.signal(signal.SIGINT, backend_utils.interrupt_handler) + signal.signal(signal.SIGTSTP, backend_utils.stop_handler) # Refer to the notes in tail_logs. self.run_on_head(