فهرست منبع

Guard sync credential refresh functions against async callers

ZiB 4 ماه پیش
والد
کامیت
dfb60ca846
1فایلهای تغییر یافته به همراه14 افزوده شده و 0 حذف شده
  1. 14 0
      room_cal/service_api/_scripts.py

+ 14 - 0
room_cal/service_api/_scripts.py

@@ -1,3 +1,4 @@
+import asyncio
 import logging
 import os
 import subprocess
@@ -37,12 +38,24 @@ def _ensure_installed(ts_dir: Path) -> None:
     print()
 
 
+def _assert_not_async(fn_name: str) -> None:
+  try:
+    asyncio.get_running_loop()
+  except RuntimeError:
+    return
+  raise RuntimeError(
+    f'{fn_name}() called from an async context — '
+    'use run_in_executor to avoid blocking the event loop'
+  )
+
+
 # One SMS code is valid at a time and Booking.com rate-limits sends regardless of how often we ask,
 # so there is no benefit to concurrent booking browser operations — serialize them all.
 _booking_lock = threading.Lock()
 
 
 def refresh_booking_cookies() -> None:
+  _assert_not_async('refresh_booking_cookies')
   ts_dir = _ts_dir()
   _ensure_installed(ts_dir)
   _log.info('booking: starting credential refresh')
@@ -57,6 +70,7 @@ def refresh_booking_cookies() -> None:
 
 
 def refresh_airbnb_cookies() -> None:
+  _assert_not_async('refresh_airbnb_cookies')
   ts_dir = _ts_dir()
   _ensure_installed(ts_dir)
   _log.info('airbnb: starting credential refresh')