File tree 1 file changed +14
-3
lines changed
1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -68,6 +68,7 @@ def _is_in_trio_context() -> bool:
68
68
# Will raise RuntimeError if not in trio context
69
69
trio .lowlevel .current_task ()
70
70
except (RuntimeError , AttributeError ):
71
+ # Not in a trio context or trio API changed
71
72
return False
72
73
return True
73
74
@@ -195,9 +196,13 @@ def _create_lock(self) -> AsyncLock:
195
196
context = detect_async_context ()
196
197
197
198
if context == "trio" and has_anyio :
198
- import anyio
199
+ try :
200
+ import anyio
201
+ except Exception :
202
+ # Just continue to asyncio if anyio import fails
203
+ return asyncio .Lock ()
199
204
return anyio .Lock ()
200
-
205
+
201
206
# For asyncio or unknown contexts
202
207
return asyncio .Lock ()
203
208
@@ -207,7 +212,13 @@ async def _awaitable(self) -> _ValueType:
207
212
if self ._lock is None :
208
213
self ._lock = self ._create_lock ()
209
214
210
- async with self ._lock :
215
+ try :
216
+ async with self ._lock :
217
+ if self ._cache is _sentinel :
218
+ self ._cache = await self ._coro
219
+ return self ._cache # type: ignore
220
+ except RuntimeError :
221
+ # Fallback for when running in asyncio context with trio detection
211
222
if self ._cache is _sentinel :
212
223
self ._cache = await self ._coro
213
224
return self ._cache # type: ignore
You can’t perform that action at this time.
0 commit comments