Spaces:
Sleeping
Sleeping
Update main.py
Browse files260 unmodified lines2612622632642652662672687 unmodified lines276277278279280281282478 unmodified lines761762763764765766767260 unmodified lines# βββ Background Memory Cleanup Task βββββββββββββββββββββββββββββββββββββββββββ_RAM_CLEANUP_THRESHOLD_MB = float(os.environ.get("RAM_CLEANUP_THRESHOLD_MB", "400"))_RAM_CLEANUP_INTERVAL_SEC = int(os.environ.get("RAM_CLEANUP_INTERVAL_SEC", "300"))async def memory_cleanup_task():"""7 unmodified linesprocess = psutil.Process(os.getpid())mem_mb = process.memory_info().rss / 1024 / 1024if mem_mb > _RAM_CLEANUP_THRESHOLD_MB:print(f"[CLEANUP] RAM at {mem_mb:.1f}MB (threshold {_RAM_CLEANUP_THRESHOLD_MB}MB) β clearing engine hash")engine = _GLOBAL_DEEPCASTLE_ENGINEif engine is not None:478 unmodified linesplayer_moves_count = 0current_score, _ = get_normalized_score(infos_before[0])for i, san_move in enumerate(request.moves):is_white_turn = board.turn == chess.WHITEis_player_turn = is_white_turn if player_is_white else not is_white_turn260 unmodified lines2612622632642652662672682692702712722737 unmodified lines281282283284285286287288289290291292293294478 unmodified lines773774775776777778779780781782783784785786787788789790791260 unmodified lines# βββ Background Memory Cleanup Task βββββββββββββββββββββββββββββββββββββββββββ_RAM_CLEANUP_THRESHOLD_MB = float(os.environ.get("RAM_CLEANUP_THRESHOLD_MB", "1024"))# Global System Limit: 18GB (All analysis sessions freed)_RAM_PANIC_THRESHOLD_MB = 18000.0# Individual Session Limit: 4GB_RAM_SESSION_LIMIT_MB = 4000.0# How often to run the cleanup in seconds (5s)_RAM_CLEANUP_INTERVAL_SEC = 5async def memory_cleanup_task():"""7 unmodified linesprocess = psutil.Process(os.getpid())mem_mb = process.memory_info().rss / 1024 / 1024# PANIC CASE: Over 18GB - total server resetif mem_mb > _RAM_PANIC_THRESHOLD_MB:print(f"[PANIC] RAM at {mem_mb:.1f}MB! Exceeded 18GB System limit. Resetting everything...")await shutdown_engine_async()force_memory_release()# NORMAL CLEANUPelif mem_mb > _RAM_CLEANUP_THRESHOLD_MB:print(f"[CLEANUP] RAM at {mem_mb:.1f}MB (threshold {_RAM_CLEANUP_THRESHOLD_MB}MB) β clearing engine hash")engine = _GLOBAL_DEEPCASTLE_ENGINEif engine is not None:478 unmodified linesplayer_moves_count = 0current_score, _ = get_normalized_score(infos_before[0])process = psutil.Process()mem_start = process.memory_info().rss / 1024 / 1024for i, san_move in enumerate(request.moves):# Check individual session growth (4GB limit)mem_now = process.memory_info().rss / 1024 / 1024if (mem_now - mem_start) > _RAM_SESSION_LIMIT_MB:print(f"[ANALYSIS-RESET] Individual session exceeds 4GB growth. Clearing RAM mid-analysis.")async with _ENGINE_IO_LOCK:await _clear_engine_hash(engine)force_memory_release()mem_start = process.memory_info().rss / 1024 / 1024is_white_turn = board.turn == chess.WHITEis_player_turn = is_white_turn if player_is_white else not is_white_turn
main.py+27-3