From 3c9f4b8f4f559cf398e5f867a4954b4928386f1d Mon Sep 17 00:00:00 2001 From: anor Date: Thu, 26 Mar 2026 03:22:30 +1000 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20UI=20=E2=80=94=20stderr=20deadlock=20+=20?= =?UTF-8?q?=D1=82=D0=B0=D0=B9=D0=BC=D0=B5=D1=80=20=D0=B0=D0=BD=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- batch_v2.py | 57 ++++++++++++++++++++-------------------------- static/batch2.html | 18 ++++++++++++++- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/batch_v2.py b/batch_v2.py index 3c67f9f..1b5257d 100644 --- a/batch_v2.py +++ b/batch_v2.py @@ -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): diff --git a/static/batch2.html b/static/batch2.html index 865219f..44d6a6d 100644 --- a/static/batch2.html +++ b/static/batch2.html @@ -60,7 +60,7 @@ pre { background: #2c2c2c; color: #f5f0eb; padding: .8rem; border-radius: 6px; o @@ -95,10 +95,23 @@ if (!token) window.location.href = '/'; const H = {'Authorization': 'Bearer ' + token}; let jobId = null; +let timerInterval = null; +let timerStart = 0; function show(id) { document.getElementById(id).classList.remove('hidden'); } function hide(id) { document.getElementById(id).classList.add('hidden'); } +function startTimer() { + timerStart = Date.now(); + timerInterval = setInterval(() => { + const s = Math.floor((Date.now() - timerStart) / 1000); + const m = Math.floor(s / 60); + const sec = s % 60; + document.getElementById('analyze-timer').textContent = m > 0 ? m + 'м ' + sec + 'с' : sec + 'с'; + }, 1000); +} +function stopTimer() { if (timerInterval) { clearInterval(timerInterval); timerInterval = null; } } + async function uploadFiles() { const zipFile = document.getElementById('zip-file').files[0]; const targetFile = document.getElementById('target-file').files[0]; @@ -134,6 +147,7 @@ async function uploadFiles() { async function startAnalysis() { const log = document.getElementById('analyze-log'); log.textContent = ''; + startTimer(); try { const r = await fetch('/api/batch-v2/analyze/' + jobId, {method: 'POST', headers: H}); @@ -163,6 +177,7 @@ async function startAnalysis() { log.scrollTop = log.scrollHeight; } else if (eventType === 'done') { document.getElementById('analyze-spinner').style.display = 'none'; + stopTimer(); if (d.script) { document.getElementById('analyze-status').textContent = 'Анализ завершён'; document.getElementById('script-code').textContent = d.script; @@ -178,6 +193,7 @@ async function startAnalysis() { } } catch(e) { document.getElementById('analyze-spinner').style.display = 'none'; + stopTimer(); document.getElementById('analyze-status').textContent = 'Ошибка: ' + e.message; document.getElementById('analyze-status').style.color = 'var(--accent)'; }