fix(funasr): fix race condition in suppress_stdout causing startup crash#53
Open
wei-yao wants to merge 1 commit intoyan5xu:mainfrom
Open
fix(funasr): fix race condition in suppress_stdout causing startup crash#53wei-yao wants to merge 1 commit intoyan5xu:mainfrom
wei-yao wants to merge 1 commit intoyan5xu:mainfrom
Conversation
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 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On startup, FunASR models finish loading but the server immediately crashes with:
The app gets stuck showing "模型加载中" indefinitely.
Root Cause
suppress_stdout()saves and restores the globalsys.stdoutto suppress noise from FunASR/modelscope during model initialization. Since three models (ASR, VAD, punc) load in parallel threads, each thread callssuppress_stdout()concurrently — causing a race condition:sys.stdoutold_A = sys.stdout(real pipe)sys.stdout = devnull_Aold_B = sys.stdout→ captures devnull_Asys.stdout = devnull_Bsys.stdout = old_A,devnull_A.close()sys.stdout = old_B→ closed devnull_A ❌After all threads complete,
sys.stdoutpoints to a closed file, so any subsequentprint()raisesValueError.Fix
Restore to
sys.__stdout__(Python's original stdout) instead of the locally saved reference. This is always stable regardless of thread interleaving.