Gtsingh111111 commited on
Commit
0896bc8
·
1 Parent(s): 32efb70

fix: handle BrokenPipeError from validator pipe closure

Browse files

- Add _safe_print() wrapper that silently catches BrokenPipeError/OSError
- All log functions (log_start, log_step, log_end) use _safe_print
- main() catches all exceptions with top-level try/except
- SIGPIPE signal reset to SIG_DFL on Linux for clean exit
- Silently close stdout/stderr in finally block to prevent secondary crashes

Files changed (1) hide show
  1. inference.py +42 -10
inference.py CHANGED
@@ -5,6 +5,7 @@ from __future__ import annotations
5
  import asyncio
6
  import json
7
  import os
 
8
  import subprocess
9
  import sys
10
  import time
@@ -49,23 +50,29 @@ def _step_budget(steps_remaining: int) -> int:
49
  return min(MAX_STEPS_CAP, max(1, int(steps_remaining)))
50
 
51
 
 
 
 
 
 
 
 
 
52
  def log_start(task: str, env: str, model: str) -> None:
53
- print(f"[START] task={task} env={env} model={model}", flush=True)
54
 
55
 
56
  def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
57
  error_text = "null" if error is None else error.replace("\n", " ").strip()
58
- print(
59
- f"[STEP] step={step} action={action} reward={reward:.2f} done={_bool_token(done)} error={error_text}",
60
- flush=True,
61
  )
62
 
63
 
64
  def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
65
  rewards_text = ",".join(f"{item:.2f}" for item in rewards)
66
- print(
67
- f"[END] success={_bool_token(success)} steps={steps} score={score:.2f} rewards={rewards_text}",
68
- flush=True,
69
  )
70
 
71
 
@@ -267,11 +274,17 @@ async def run_task(client: Optional[OpenAI], base_url: str, task_id: str) -> flo
267
 
268
 
269
  async def main() -> None:
270
- client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN) if API_BASE_URL and HF_TOKEN else None
271
- base_url, process = await prepare_base_url()
272
  try:
 
 
273
  for task_id in TASK_IDS:
274
  await run_task(client, base_url, task_id)
 
 
 
 
 
275
  finally:
276
  if process is not None:
277
  process.terminate()
@@ -282,4 +295,23 @@ async def main() -> None:
282
 
283
 
284
  if __name__ == "__main__":
285
- asyncio.run(main())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import asyncio
6
  import json
7
  import os
8
+ import signal
9
  import subprocess
10
  import sys
11
  import time
 
50
  return min(MAX_STEPS_CAP, max(1, int(steps_remaining)))
51
 
52
 
53
+ def _safe_print(msg: str) -> None:
54
+ """Print to stdout, silently ignoring broken pipe errors."""
55
+ try:
56
+ print(msg, flush=True)
57
+ except (BrokenPipeError, OSError):
58
+ pass
59
+
60
+
61
  def log_start(task: str, env: str, model: str) -> None:
62
+ _safe_print(f"[START] task={task} env={env} model={model}")
63
 
64
 
65
  def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
66
  error_text = "null" if error is None else error.replace("\n", " ").strip()
67
+ _safe_print(
68
+ f"[STEP] step={step} action={action} reward={reward:.2f} done={_bool_token(done)} error={error_text}"
 
69
  )
70
 
71
 
72
  def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
73
  rewards_text = ",".join(f"{item:.2f}" for item in rewards)
74
+ _safe_print(
75
+ f"[END] success={_bool_token(success)} steps={steps} score={score:.2f} rewards={rewards_text}"
 
76
  )
77
 
78
 
 
274
 
275
 
276
  async def main() -> None:
277
+ process: Optional[subprocess.Popen] = None
 
278
  try:
279
+ client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN) if API_BASE_URL and HF_TOKEN else None
280
+ base_url, process = await prepare_base_url()
281
  for task_id in TASK_IDS:
282
  await run_task(client, base_url, task_id)
283
+ except Exception as exc:
284
+ try:
285
+ print(f"[FATAL] {type(exc).__name__}: {exc}", file=sys.stderr, flush=True)
286
+ except (BrokenPipeError, OSError):
287
+ pass
288
  finally:
289
  if process is not None:
290
  process.terminate()
 
295
 
296
 
297
  if __name__ == "__main__":
298
+ # On Linux, reset SIGPIPE to default so broken-pipe produces a clean exit
299
+ # instead of a Python exception. On Windows this signal doesn't exist.
300
+ if hasattr(signal, "SIGPIPE"):
301
+ signal.signal(signal.SIGPIPE, signal.SIG_DFL)
302
+
303
+ try:
304
+ asyncio.run(main())
305
+ except (BrokenPipeError, OSError, KeyboardInterrupt):
306
+ pass
307
+ finally:
308
+ # Silently close stdout/stderr to avoid secondary BrokenPipeError
309
+ # during interpreter shutdown.
310
+ try:
311
+ sys.stdout.close()
312
+ except (BrokenPipeError, OSError):
313
+ pass
314
+ try:
315
+ sys.stderr.close()
316
+ except (BrokenPipeError, OSError):
317
+ pass