Amogh1221 commited on
Commit
2b214dc
Β·
verified Β·
1 Parent(s): c2681fa

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +27 -3
main.py CHANGED
@@ -261,8 +261,13 @@ async def _engine_call(engine, coro, timeout_sec: float):
261
 
262
 
263
  # ─── Background Memory Cleanup Task ───────────────────────────────────────────
264
- _RAM_CLEANUP_THRESHOLD_MB = float(os.environ.get("RAM_CLEANUP_THRESHOLD_MB", "400"))
265
- _RAM_CLEANUP_INTERVAL_SEC = int(os.environ.get("RAM_CLEANUP_INTERVAL_SEC", "300"))
 
 
 
 
 
266
 
267
  async def memory_cleanup_task():
268
  """
@@ -276,7 +281,14 @@ async def memory_cleanup_task():
276
  process = psutil.Process(os.getpid())
277
  mem_mb = process.memory_info().rss / 1024 / 1024
278
 
279
- if mem_mb > _RAM_CLEANUP_THRESHOLD_MB:
 
 
 
 
 
 
 
280
  print(f"[CLEANUP] RAM at {mem_mb:.1f}MB (threshold {_RAM_CLEANUP_THRESHOLD_MB}MB) β€” clearing engine hash")
281
  engine = _GLOBAL_DEEPCASTLE_ENGINE
282
  if engine is not None:
@@ -761,7 +773,19 @@ async def analyze_game(request: AnalyzeRequest):
761
  player_moves_count = 0
762
  current_score, _ = get_normalized_score(infos_before[0])
763
 
 
 
 
764
  for i, san_move in enumerate(request.moves):
 
 
 
 
 
 
 
 
 
765
  is_white_turn = board.turn == chess.WHITE
766
  is_player_turn = is_white_turn if player_is_white else not is_white_turn
767
 
 
261
 
262
 
263
  # ─── Background Memory Cleanup Task ───────────────────────────────────────────
264
+ _RAM_CLEANUP_THRESHOLD_MB = float(os.environ.get("RAM_CLEANUP_THRESHOLD_MB", "1024"))
265
+ # Global System Limit: 18GB (All analysis sessions freed)
266
+ _RAM_PANIC_THRESHOLD_MB = 18000.0
267
+ # Individual Session Limit: 4GB
268
+ _RAM_SESSION_LIMIT_MB = 4000.0
269
+ # How often to run the cleanup in seconds (5s)
270
+ _RAM_CLEANUP_INTERVAL_SEC = 5
271
 
272
  async def memory_cleanup_task():
273
  """
 
281
  process = psutil.Process(os.getpid())
282
  mem_mb = process.memory_info().rss / 1024 / 1024
283
 
284
+ # PANIC CASE: Over 18GB - total server reset
285
+ if mem_mb > _RAM_PANIC_THRESHOLD_MB:
286
+ print(f"[PANIC] RAM at {mem_mb:.1f}MB! Exceeded 18GB System limit. Resetting everything...")
287
+ await shutdown_engine_async()
288
+ force_memory_release()
289
+
290
+ # NORMAL CLEANUP
291
+ elif mem_mb > _RAM_CLEANUP_THRESHOLD_MB:
292
  print(f"[CLEANUP] RAM at {mem_mb:.1f}MB (threshold {_RAM_CLEANUP_THRESHOLD_MB}MB) β€” clearing engine hash")
293
  engine = _GLOBAL_DEEPCASTLE_ENGINE
294
  if engine is not None:
 
773
  player_moves_count = 0
774
  current_score, _ = get_normalized_score(infos_before[0])
775
 
776
+ process = psutil.Process()
777
+ mem_start = process.memory_info().rss / 1024 / 1024
778
+
779
  for i, san_move in enumerate(request.moves):
780
+ # Check individual session growth (4GB limit)
781
+ mem_now = process.memory_info().rss / 1024 / 1024
782
+ if (mem_now - mem_start) > _RAM_SESSION_LIMIT_MB:
783
+ print(f"[ANALYSIS-RESET] Individual session exceeds 4GB growth. Clearing RAM mid-analysis.")
784
+ async with _ENGINE_IO_LOCK:
785
+ await _clear_engine_hash(engine)
786
+ force_memory_release()
787
+ mem_start = process.memory_info().rss / 1024 / 1024
788
+
789
  is_white_turn = board.turn == chess.WHITE
790
  is_player_turn = is_white_turn if player_is_white else not is_white_turn
791