|
| 1 | +import logging |
1 | 2 | from abc import abstractmethod |
2 | 3 | from enum import IntEnum |
3 | 4 | from time import time |
4 | 5 | from typing import AsyncGenerator, Generator, Optional |
5 | 6 |
|
6 | | -from anyio import Lock, get_current_task |
| 7 | +from anyio import Lock |
7 | 8 | from httpx import Auth as HttpxAuth |
8 | 9 | from httpx import Request, Response, codes |
9 | 10 |
|
10 | 11 | from firebolt.utils.token_storage import TokenSecureStorage |
11 | 12 | from firebolt.utils.util import Timer, cached_property, get_internal_error_code |
12 | 13 |
|
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
13 | 16 |
|
14 | 17 | class FireboltAuthVersion(IntEnum): |
15 | 18 | """Enum for Firebolt authentication versions.""" |
@@ -199,12 +202,19 @@ async def async_auth_flow( |
199 | 202 | finally: |
200 | 203 | # token gets updated only after flow.send is called |
201 | 204 | # so unlock only after that |
202 | | - # TODO: FIR-38687 Fix support for anyio 4.5.0+ |
203 | | - if ( |
204 | | - self._lock.locked() |
205 | | - and self._lock._owner_task == get_current_task() # type: ignore |
206 | | - ): |
207 | | - self._lock.release() |
| 205 | + self._release_lock() |
| 206 | + |
| 207 | + def _release_lock(self) -> None: |
| 208 | + """Release the lock if held.""" |
| 209 | + if self._lock.locked(): |
| 210 | + try: |
| 211 | + self._lock.release() |
| 212 | + except RuntimeError as e: |
| 213 | + # Check the error string since RuntimeError is very generic |
| 214 | + if "a Lock you don't own" not in str(e): |
| 215 | + raise |
| 216 | + # This task does not own the lock, can't release |
| 217 | + logging.warning("Tried to release a lock not owned by the current task") |
208 | 218 |
|
209 | 219 | def sync_auth_flow(self, request: Request) -> Generator[Request, Response, None]: |
210 | 220 | """ |
|
0 commit comments