From f3a129f2e986a97ebd65f797815c0bc575a7da09 Mon Sep 17 00:00:00 2001 From: yao wei Date: Mon, 2 Mar 2026 23:57:24 -0800 Subject: [PATCH] fix(funasr): fix race condition in suppress_stdout causing startup crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When three model-loading threads run in parallel, each calls suppress_stdout() which saves and restores sys.stdout. Due to interleaving, one thread could capture another thread's devnull as its old_stdout, and later restore to a closed file — causing `ValueError: I/O operation on closed file` when the server tried to print the init result. Fix by restoring to sys.__stdout__ (Python's original stdout) instead of the locally saved reference, which is always stable regardless of thread order. Co-Authored-By: Claude Sonnet 4.6 --- funasr_server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/funasr_server.py b/funasr_server.py index 78b1863..11640b1 100644 --- a/funasr_server.py +++ b/funasr_server.py @@ -55,13 +55,13 @@ def get_log_path(): @contextlib.contextmanager def suppress_stdout(): """上下文管理器:临时重定向stdout到devnull,避免FunASR库的非JSON输出干扰IPC通信""" - old_stdout = sys.stdout devnull = open(os.devnull, "w") try: sys.stdout = devnull yield finally: - sys.stdout = old_stdout + # 使用 sys.__stdout__ 恢复原始stdout,避免多线程race condition + sys.stdout = sys.__stdout__ devnull.close()