Solareva Taisia commited on
Commit
3471246
·
1 Parent(s): 68ab9bd

fix(render): load model in background to avoid port scan timeout

Browse files
Files changed (1) hide show
  1. api/main.py +14 -4
api/main.py CHANGED
@@ -490,10 +490,20 @@ async def startup_event():
490
  if not model_path_p.exists():
491
  model_path_p = _resolve_path("models/best_model.pt")
492
 
493
- if model_path_p.exists():
494
- await load_model(str(model_path_p))
495
- else:
496
- logger.warning(f"Model file not found: {model_path_p}. API will not work until model is loaded.")
 
 
 
 
 
 
 
 
 
 
497
 
498
  # Resolve thresholds path: env var wins, else config/thresholds.json (if present)
499
  thresholds_env = os.environ.get("THRESHOLDS_PATH")
 
490
  if not model_path_p.exists():
491
  model_path_p = _resolve_path("models/best_model.pt")
492
 
493
+ async def _load_resources_background() -> None:
494
+ """Load model (and later potentially other heavy resources) without blocking server startup."""
495
+ try:
496
+ if model_path_p.exists():
497
+ await load_model(str(model_path_p))
498
+ else:
499
+ logger.warning(f"Model file not found: {model_path_p}. API will not work until model is loaded.")
500
+ except Exception as e:
501
+ # Keep the process alive; health will show model_not_loaded.
502
+ logger.exception(f"Background model load failed: {e}")
503
+
504
+ # IMPORTANT (Render): don't block server startup on large model/tokenizer load.
505
+ # Render performs a port scan shortly after starting the process; heavy startup work can time out.
506
+ asyncio.create_task(_load_resources_background())
507
 
508
  # Resolve thresholds path: env var wins, else config/thresholds.json (if present)
509
  thresholds_env = os.environ.get("THRESHOLDS_PATH")