Skip to content

Commit 68c91ed

Browse files
committed
sync codepath detector
1 parent 06baa06 commit 68c91ed

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

sky/server/requests/requests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151

5252
DEFAULT_REQUESTS_RETENTION_HOURS = 24 # 1 day
5353

54+
55+
def _is_running_pytest() -> bool:
56+
return 'PYTEST_CURRENT_TEST' in os.environ
57+
58+
5459
# TODO(zhwu): For scalability, there are several TODOs:
5560
# [x] Have a way to queue requests.
5661
# [ ] Move logs to persistent place.
@@ -666,6 +671,8 @@ def update_request(request_id: str) -> Generator[Optional[Request], None, None]:
666671
"""Get and update a SkyPilot API request."""
667672
# Acquire the lock to avoid race conditions between multiple request
668673
# operations, e.g. execute and cancel.
674+
if not _is_running_pytest() and asyncio_utils.is_running_async():
675+
logger.warning('synchronous filelock is being used in an async context')
669676
with filelock.FileLock(request_lock_path(request_id)):
670677
request = _get_request_no_lock(request_id)
671678
yield request
@@ -752,6 +759,8 @@ async def get_latest_request_id_async() -> Optional[str]:
752759
def get_request(request_id: str,
753760
fields: Optional[List[str]] = None) -> Optional[Request]:
754761
"""Get a SkyPilot API request."""
762+
if not _is_running_pytest() and asyncio_utils.is_running_async():
763+
logger.warning('synchronous filelock is being used in an async context')
755764
with filelock.FileLock(request_lock_path(request_id)):
756765
return _get_request_no_lock(request_id, fields)
757766

sky/utils/asyncio_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,12 @@ async def async_wrapper(*args, **kwargs):
7676
raise
7777

7878
return async_wrapper
79+
80+
81+
def is_running_async() -> bool:
82+
"""Check if the code is currently running inside an asyncio event loop."""
83+
try:
84+
asyncio.get_running_loop()
85+
return True
86+
except RuntimeError:
87+
return False

0 commit comments

Comments
 (0)