Skip to content

Commit a069d0a

Browse files
committed
re-set context
1 parent 499060e commit a069d0a

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

trio_asyncio/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,12 @@ async def run_coroutine(self, coro):
214214
This is a Trio coroutine.
215215
"""
216216
self._check_closed()
217+
t = sniffio.current_async_library_cvar.set("asyncio")
217218
coro = asyncio.ensure_future(coro, loop=self)
218-
return await run_future(coro)
219+
try:
220+
return await run_future(coro)
221+
finally:
222+
sniffio.current_async_library_cvar.reset(t)
219223

220224
def wrap_generator(self, gen, *args):
221225
"""Call an asyncio generator.

trio_asyncio/handles.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import trio
33
import asyncio
4+
import sniffio
45
try:
56
from asyncio.format_helpers import _format_callback, _get_function_source
67
except ImportError: # <3.7
@@ -17,6 +18,11 @@ def _format_callback_source(func, args):
1718
return func_repr
1819

1920

21+
async def _set_sniff(proc, *args):
22+
sniffio.current_async_library_cvar.set("trio")
23+
return await proc(*args)
24+
25+
2026
class _TrioHandle:
2127
"""
2228
This extends asyncio.Handle by providing:
@@ -98,9 +104,9 @@ async def _call_async(self, task_status=trio.TASK_STATUS_IGNORED):
98104
with trio.open_cancel_scope() as scope:
99105
self._scope = scope
100106
if self._is_sync is None:
101-
await self._context.run(self._callback, self)
107+
await self._context.run(_set_sniff, self._callback, self)
102108
else:
103-
await self._context.run(self._callback, *self._args)
109+
await self._context.run(_set_sniff, self._callback, *self._args)
104110
except Exception as exc:
105111
self._raise(exc)
106112
finally:

0 commit comments

Comments
 (0)