fix: зависание UI — stderr deadlock + таймер анализа

3 исправления:
1. stderr=DEVNULL вместо PIPE — устраняет deadlock когда
   stderr буфер переполняется и proc.wait() зависает навечно
2. proc.wait() с timeout=5с + kill() при таймауте
3. Таймер на странице — показывает сколько идёт анализ (Xм Yс)

Также: fix cp437→utf-8 для кириллических имён в ZIP (batch.py)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-26 03:22:30 +10:00
co-authored by Claude Opus 4.6
parent 5549b5da97
commit 3c9f4b8f4f
2 changed files with 42 additions and 33 deletions
+25 -32
View File
@@ -266,6 +266,21 @@ The script must handle any file with the same general structure as the samples.
"""
def _read_result_files(job_dir: Path) -> dict:
"""Read batch_script.py and batch_summary.json from job dir."""
script_path = job_dir / "batch_script.py"
summary_path = job_dir / "batch_summary.json"
summary = {}
if summary_path.exists():
try:
summary = json.loads(summary_path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError):
pass
if script_path.exists():
return {"script": script_path.read_text(encoding="utf-8"), "summary": summary}
return {"script": "", "result": "Script not generated", "summary": summary}
async def run_claude_analysis(
job_dir: Path,
task_description: str,
@@ -317,7 +332,7 @@ async def run_claude_analysis(
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.DEVNULL, # don't capture stderr — prevents buffer deadlock
)
except FileNotFoundError:
yield {"type": "done", "script": "", "result": "Docker not found"}
@@ -349,43 +364,21 @@ async def run_claude_analysis(
elif etype == "result":
got_result = True
script_path = job_dir / "batch_script.py"
summary_path = job_dir / "batch_summary.json"
summary = {}
if summary_path.exists():
try:
summary = json.loads(summary_path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError):
pass
if script_path.exists():
script = script_path.read_text(encoding="utf-8")
yield {"type": "done", "script": script, "summary": summary}
else:
result_text = event.get("result", "")
yield {"type": "done", "script": "", "result": result_text, "summary": summary}
result_data = _read_result_files(job_dir)
yield {"type": "done", **result_data}
finally:
if proc.returncode is None:
proc.terminate()
await proc.wait()
try:
await asyncio.wait_for(proc.wait(), timeout=5)
except asyncio.TimeoutError:
proc.kill()
# If Claude exited without emitting a result event — check files directly
# Fallback: Claude exited without result event — check files on disk
if not got_result:
script_path = job_dir / "batch_script.py"
summary_path = job_dir / "batch_summary.json"
summary = {}
if summary_path.exists():
try:
summary = json.loads(summary_path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError):
pass
if script_path.exists():
script = script_path.read_text(encoding="utf-8")
yield {"type": "done", "script": script, "summary": summary}
else:
stderr = await proc.stderr.read()
err_msg = stderr.decode()[:500] if stderr else "Claude exited without generating script"
yield {"type": "done", "script": "", "result": f"Error: {err_msg}", "summary": summary}
result_data = _read_result_files(job_dir)
yield {"type": "done", **result_data}
def run_batch_v2(job_id: str, script: str, source_dir: str, target_file: str):