Amogh1221 commited on
Commit
234acb2
Β·
verified Β·
1 Parent(s): 28accab

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +11 -2
main.py CHANGED
@@ -263,7 +263,8 @@ async def _engine_call(engine, coro, timeout_sec: float):
263
 
264
  # ─── Background Memory Cleanup Task ───────────────────────────────────────────
265
  _RAM_CLEANUP_THRESHOLD_MB = float(os.environ.get("RAM_CLEANUP_THRESHOLD_MB", "300"))
266
- _RAM_CLEANUP_INTERVAL_SEC = int(os.environ.get("RAM_CLEANUP_INTERVAL_SEC", "300"))
 
267
 
268
  async def memory_cleanup_task():
269
  """
@@ -303,6 +304,7 @@ async def memory_cleanup_task():
303
  async def lifespan(app: FastAPI):
304
  cleanup_task = asyncio.create_task(memory_cleanup_task())
305
  print(f"[STARTUP] Memory cleanup task started (every {_RAM_CLEANUP_INTERVAL_SEC}s, threshold {_RAM_CLEANUP_THRESHOLD_MB}MB)")
 
306
  yield
307
  cleanup_task.cancel()
308
  try:
@@ -490,7 +492,7 @@ async def get_move(request: MoveRequest):
490
  del result
491
  del info
492
 
493
- return MoveResponse(
494
  bestmove=best_move,
495
  score=score_pawns,
496
  depth=depth,
@@ -500,6 +502,13 @@ async def get_move(request: MoveRequest):
500
  mate_in=mate_in,
501
  opening=opening_name
502
  )
 
 
 
 
 
 
 
503
  except HTTPException:
504
  raise
505
  except Exception as e:
 
263
 
264
  # ─── Background Memory Cleanup Task ───────────────────────────────────────────
265
  _RAM_CLEANUP_THRESHOLD_MB = float(os.environ.get("RAM_CLEANUP_THRESHOLD_MB", "300"))
266
+ _RAM_CLEANUP_INTERVAL_SEC = int(os.environ.get("RAM_CLEANUP_INTERVAL_SEC", "60"))
267
+ _CLEAR_HASH_AFTER_MOVE = os.environ.get("CLEAR_HASH_AFTER_MOVE", "1").strip().lower() not in {"0", "false", "no", "off"}
268
 
269
  async def memory_cleanup_task():
270
  """
 
304
  async def lifespan(app: FastAPI):
305
  cleanup_task = asyncio.create_task(memory_cleanup_task())
306
  print(f"[STARTUP] Memory cleanup task started (every {_RAM_CLEANUP_INTERVAL_SEC}s, threshold {_RAM_CLEANUP_THRESHOLD_MB}MB)")
307
+ print(f"[STARTUP] Engine hash config: hash_mb={_engine_hash_mb()} clear_after_move={_CLEAR_HASH_AFTER_MOVE}")
308
  yield
309
  cleanup_task.cancel()
310
  try:
 
492
  del result
493
  del info
494
 
495
+ response = MoveResponse(
496
  bestmove=best_move,
497
  score=score_pawns,
498
  depth=depth,
 
502
  mate_in=mate_in,
503
  opening=opening_name
504
  )
505
+ # On constrained RAM environments (e.g. HF Spaces), keeping hash between
506
+ # move calls can still cause memory pressure over long games.
507
+ if _CLEAR_HASH_AFTER_MOVE:
508
+ async with _ENGINE_IO_LOCK:
509
+ await _clear_engine_hash(engine)
510
+ force_memory_release()
511
+ return response
512
  except HTTPException:
513
  raise
514
  except Exception as e: