Skip to content

Commit 65ca3ca

Browse files
committed
Skip signal handler registration when not on main thread
When launch_server is called from a background thread (e.g., in a Ray actor), Python raises ValueError because signal.signal() can only be called from the main thread. Guard the SIGQUIT handler registration with a threading.main_thread() check to avoid this error.
1 parent f441747 commit 65ca3ca

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

python/sglang/srt/entrypoints/engine.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -853,25 +853,29 @@ def _set_envs_and_config(server_args: ServerArgs):
853853
"Please reinstall the latest version with `pip install sgl-kernel --force-reinstall`",
854854
)
855855

856-
if server_args.custom_sigquit_handler is None:
857-
# Register the signal handler.
858-
# The child processes will send SIGQUIT to this process when any error happens
859-
# This process then clean up the whole process tree
860-
# Note: This sigquit handler is used in the launch phase, and may be replaced by
861-
# the running_phase_sigquit_handler in the tokenizer manager after the grpc server is launched.
862-
def launch_phase_sigquit_handler(signum, frame):
856+
# Signal handlers can only be registered from the main thread.
857+
# When launch_server is called from a background thread (e.g., in a Ray actor),
858+
# skip signal registration to avoid ValueError.
859+
if threading.current_thread() is threading.main_thread():
860+
if server_args.custom_sigquit_handler is None:
861+
# Register the signal handler.
862+
# The child processes will send SIGQUIT to this process when any error happens
863+
# This process then clean up the whole process tree
864+
# Note: This sigquit handler is used in the launch phase, and may be replaced by
865+
# the running_phase_sigquit_handler in the tokenizer manager after the grpc server is launched.
866+
def launch_phase_sigquit_handler(signum, frame):
867+
logger.error(
868+
"Received sigquit from a child process. It usually means the child failed."
869+
)
870+
kill_process_tree(os.getpid())
871+
872+
signal.signal(signal.SIGQUIT, launch_phase_sigquit_handler)
873+
else:
874+
# Allow users to register a custom SIGQUIT handler for things like crash dump
863875
logger.error(
864-
"Received sigquit from a child process. It usually means the child failed."
876+
f"Using custom SIGQUIT handler: {server_args.custom_sigquit_handler}"
865877
)
866-
kill_process_tree(os.getpid())
867-
868-
signal.signal(signal.SIGQUIT, launch_phase_sigquit_handler)
869-
else:
870-
# Allow users to register a custom SIGQUIT handler for things like crash dump
871-
logger.error(
872-
f"Using custom SIGQUIT handler: {server_args.custom_sigquit_handler}"
873-
)
874-
signal.signal(signal.SIGQUIT, server_args.custom_sigquit_handler)
878+
signal.signal(signal.SIGQUIT, server_args.custom_sigquit_handler)
875879

876880
# Set mp start method
877881
mp.set_start_method("spawn", force=True)

0 commit comments

Comments
 (0)