Spaces:
Sleeping

p4r5kpftnp-cmd Claude Opus 4.7 commited on
Commit
ecb47f3
·
1 Parent(s): 8d1d708

Fix stdout race in solve_proof under concurrent requests

Browse files

contextlib.redirect_stdout rebinds the process-wide sys.stdout. With
multiple Gradio users invoking solve_proof simultaneously, the most
recent rebinding wins for every running thread, so log output ended
up in the wrong caller's buffer (or got lost when one call exited its
context manager mid-run of another).

Replace it with a single _ThreadLocalStdout proxy installed once on
sys.stdout, plus a per-call _capture_stdout context manager that
pushes/pops a StringIO onto a thread-local stack. Each thread now sees
only its own buffer; other threads (and any non-capturing threads)
continue to write to the original stdout.

Also harden the cleanup path: track tmp_path separately so that if
NamedTemporaryFile itself raises (or anything before tmp is bound),
the finally block doesn't raise NameError trying to unlink an
undefined name; swallow OSError if the file is already gone.

Add tests/test_concurrent_solve.py exercising 10 concurrent
solve_proof calls with a mocked LangGraphAgent: verifies tuple shape,
no input/output cross-contamination, unique tempfile paths, exception
isolation (1 raising call does not affect the other 9), tempfile
cleanup on all paths, and (the regression check) thread-local log
isolation.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

2 files changed +366 -6
Files changed (2)
app.py+95-6
tests/test_concurrent_solve.pyAdded+271