RuntimeError: Attempted to exit a cancel scope that isn't the current tasks's current cancel scope
Author: hermeschen1116Created Dec 15, 2025Updated Feb 25, 2026
- Initially raised as discussion
Hello, I recently testing my project in Python 3.13.9. And I got the error below. It seems like some problem with anyio in Python 3.13. Everything works when downgrade to Python 3.12.
2025-11-04T04:21:06.180982Z [DEBUG ] httpcore.connection: connect_tcp.started host='localhost' port=9997 local_address=None timeout=5.0 socket_options=None func_name=atrace lineno=87 pathname=/Users/hermeschen/Repo/work/stream-server/.venv/lib/python3.13/site-packages/httpcore/_trace.py thread=8453398656 process=49194 app_env=dev
2025-11-04T04:21:06.181586Z [DEBUG ] httpcore.connection: connect_tcp.started host='localhost' port=9997 local_address=None timeout=5.0 socket_options=None func_name=atrace lineno=87 pathname=/Users/hermeschen/Repo/work/stream-server/.venv/lib/python3.13/site-packages/httpcore/_trace.py thread=8453398656 process=49194 app_env=dev
2025-11-04T04:21:06.182975Z [DEBUG ] httpcore.connection: connect_tcp.failed exception=RuntimeError("Attempted to exit a cancel scope that isn't the current tasks's current cancel scope") func_name=atrace lineno=87 pathname=/Users/hermeschen/Repo/work/stream-server/.venv/lib/python3.13/site-packages/httpcore/_trace.py thread=8453398656 process=49194 app_env=dev
2025-11-04T04:21:06.183217Z [DEBUG ] httpcore.connection: connect_tcp.failed exception=RuntimeError("Attempted to exit a cancel scope that isn't the current tasks's current cancel scope") func_name=atrace lineno=87 pathname=/Users/hermeschen/Repo/work/stream-server/.venv/lib/python3.13/site-packages/httpcore/_trace.py thread=8453398656 process=49194 app_env=dev
2025-11-04T04:21:06.183472Z [DEBUG ] utils.Utils: Failed to execute check_server_status.<locals>._check func_name=async_wrapper lineno=139 pathname=/Users/hermeschen/Repo/work/stream-server/.venv/lib/python3.13/site-packages/utils/Utils.py thread=6141276160 process=49194 app_env=dev num_attempt=0
╭──────────────────────────────────── Traceback (most recent call last) ────────────────────────────────────╮
│ in async_wrapper:132 │
│ │
│ /Users/hermeschen/Repo/work/stream-server/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py: │
│ 781 in __aexit__ │
│ │
│ 778 │ │ │ │ │ # added to self._exceptions so it's ok to break exception │
│ 779 │ │ │ │ │ # chaining and avoid adding a "During handling of above..." │
│ 780 │ │ │ │ │ # for each nesting level. │
│ ❱ 781 │ │ │ │ │ raise BaseExceptionGroup( │
│ 782 │ │ │ │ │ │ "unhandled errors in a TaskGroup", self._exceptions │
│ 783 │ │ │ │ │ ) from None │
│ 784 │ │ │ │ elif exc_val: │
│ │
│ ╭───────────────────────────────── locals ──────────────────────────────────╮ │
│ │ exc_type = None │ │
│ │ loop = <uvloop.Loop running=True closed=False debug=False> │ │
│ │ self = <anyio._backends._asyncio.TaskGroup object at 0x10e807610> │ │
│ │ wait_scope = <anyio._backends._asyncio.CancelScope object at 0x10e7ff9b0> │ │
│ ╰───────────────────────────────────────────────────────────────────────────╯ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────╯
ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
╭──────────────────────────────────────────── Sub-exception #1 ─────────────────────────────────────────────╮
│ ╭────────────────────────────────── Traceback (most recent call last) ──────────────────────────────────╮ │
│ │ in _run_in_isolation:130 │ │
│ │ │ │
│ │ /Users/hermeschen/Repo/work/stream-server/src/stream_server/internal/MediaServer.py:99 in _check │ │
│ │ │ │
│ │ 96 │ @retry(default_max_attempt=5, duration_or_func=_delay) │ │
│ │ 97 │ async def _check() -> None: │ │
│ │ 98 │ │ await LOGGER.adebug("try to call media server") │ │
│ │ ❱ 99 │ │ response: httpx.Response = await client.get("/v3/paths/list") │ │
│ │ 100 │ │ if response.status_code == 200: │ │
│ │ 101 │ │ │ await LOGGER.adebug("media server is running") │ │
│ │ 102 │ │ │ return │ │
│ │ │ │
│ │ ╭────────────────────── locals ──────────────────────╮ │ │
│ │ │ client = <httpx.AsyncClient object at 0x10e805950> │ │ │
│ │ ╰────────────────────────────────────────────────────╯ │ │
│ │ │ │
│ │ /Users/hermeschen/Repo/work/stream-server/.venv/lib/python3.13/site-packages/httpx/_client.py:1768 in │ │
│ │ get │ │
│ │ │ │
│ │ 1765 │ │ │ │
│ │ 1766 │ │ **Parameters**: See `httpx.request`. │ │
│ │ 1767 │ │ """ │ │
│ │ ❱ 1768 │ │ return await self.request( │ │
│ │ 1769 │ │ │ "GET", │ │
│ │ 1770 │ │ │ url, │ │
│ │ 1771 │ │ │ params=params, │ │
│ │ │ │
│ │ ╭───────────────────────────────── locals ──────────────────────────────────╮ │ │
│ │ │ auth = <httpx._client.UseClientDefault object at 0x10adb5fd0> │ │ │
│ │ │ cookies = None │ │ │
│ │ │ extensions = None │ │ │
│ │ │ follow_redirects = <httpx._client.UseClientDefault object at 0x10adb5fd0> │ │ │
│ │ │ headers = None │ │ │
│ │ │ params = None │ │ │
│ │ │ self = <httpx.AsyncClient object at 0x10e805950> │ │ │
│ │ │ timeout = <httpx._client.UseClientDefault object at 0x10adb5fd0> │ │ │
│ │ │ url = '/v3/paths/list' │ │ │
│ │ ╰───────────────────────────────────────────────────────────────────────────╯ │ │
│ │ │ │
│ │ /Users/hermeschen/Repo/work/stream-server/.venv/lib/python3.13/site-packages/httpx/_client.py:1540 in │ │
│ │ request │ │
│ │ │ │
│ │ 1537 │ │ │ timeout=timeout, │ │
│ │ 1538 │ │ │ extensions=extensions, │ │
│ │ 1539 │ │ ) │ │
│ │ ❱ 1540 │ │ return await self.send(request, auth=auth, follow_redirects=follow_redirects) │ │
│ │ 1541 │ │ │
│ │ 1542 │ @asynccontextmanager │ │
│ │ 1543 │ async def stream( │ │
│ │ │ │
│ │ ╭────────────────────────────────── locals ──────────────────────────────────╮ │ │
│ │ │ auth = <httpx._client.UseClientDefault object at 0x10adb5fd0> │ │ │
│ │ │ content = None │ │ │
│ │ │ cookies = None │ │ │
│ │ │ data = None │ │ │
│ │ │ extensions = None │ │ │
│ │ │ files = None │ │ │
│ │ │ follow_redirects = <httpx._client.UseClientDefault object at 0x10adb5fd0> │ │ │
│ │ │ headers = None │ │ │
│ │ │ json = None │ │ │
│ │ │ method = 'GET' │ │ │
│ │ │ params = None │ │ │
│ │ │ request = <Request('GET', 'http://localhost:9997/v3/paths/list')> │ │ │
│ │ │ self = <httpx.AsyncClient object at 0x10e805950> │ │ │
│ │ │ timeout = <httpx._client.UseClientDefault object at 0x10adb5fd0> │ │ │
│ │ │ url = '/v3/paths/list' │ │ │
│ │ ╰────────────────────────────────────────────────────────────────────────────╯ │ │
│ │ │ │
│ │ /Users/hermeschen/Repo/work/stream-server/.venv/lib/python3.13/site-packages/httpx/_client.py:1629 in │ │
│ │ send │ │
│ │ │ │
│ │ 1626 │ │ │ │
│ │ 1627 │ │ auth = self._build_request_auth(request, auth) │ │
│ │ 1628 │ │ │ │
│ │ ❱ 1629 │ │ response = await self._send_handling_auth( │ │
│ │ 1630 │ │ │ request, │ │
│ │ 1631 │ │ │ auth=auth, │ │
│ │ 1632 │ │ │ follow_redirects=follow_redirects, │ │
│ │ │ │
│ │ ╭────────────────────────────────── locals ──────────────────────────────────╮ │ │
│ │ │ auth = <httpx.Auth object at 0x10e816ba0> │ │ │
│ │ │ follow_redirects = True │ │ │
│ │ │ request = <Request('GET', 'http://localhost:9997/v3/paths/list')> │ │ │
│ │ │ self = <httpx.AsyncClient object at 0x10e805950> │ │ │
│ │ │ stream = False │ │ │
│ │ ╰────────────────────────────────────────────────────────────────────────────╯ │ │
│ │ │ │
│ │ /Users/hermeschen/Repo/work/stream-server/.venv/lib/python3.13/site-packages/httpx/_client.py:1657 in │ │
│ │ _send_handling_auth │ │
│ │ │ │
│ │ 1654 │ │ │ request = await auth_flow.__anext__() │ │
│ │ 1655 │ │ │ │ │
│ │ 1656 │ │ │ while True: │ │
│ │ ❱ 1657 │ │ │ │ response = await self._send_handling_redirects( │ │
│ │ 1658 │ │ │ │ │ request, │ │
│ │ 1659 │ │ │ │ │ follow_redirects=follow_redirects, │ │
│ │ 1660 │ │ │ │ │ history=history, │ │
│ │ │ │
│ │ ╭──────────────────────────────────── locals ─────────────────────────────────────╮ │ │
│ │ │ auth = <httpx.Auth object at 0x10e816ba0> │ │ │
│ │ │ auth_flow = <async_generator object Auth.async_auth_flow at 0x10e84b300> │ │ │
│ │ │ follow_redirects = True │ │ │
│ │ │ history = [] │ │ │
│ │ │ request = <Request('GET', 'http://localhost:9997/v3/paths/list')> │ │ │
│ │ │ self = <httpx.AsyncClient object at 0x10e805950> │ │ │
│ │ ╰─────────────────────────────────────────────────────────────────────────────────╯ │ │
│ │ │ │
│ │ /Users/hermeschen/Repo/work/stream-server/.venv/lib/python3.13/site-packages/httpx/_client.py:1694 in │ │
│ │ _send_handling_redirects │ │
│ │ │ │
│ │ 1691 │ │ │ for hook in self._event_hooks["request"]: │ │
│ │ 1692 │ │ │ │ await hook(request) │ │
│ │ 1693 │ │ │ │ │
│ │ ❱ 1694 │ │ │ response = await self._send_single_request(request) │ │
│ │ 1695 │ │ │ try: │ │
│ │ 1696 │ │ │ │ for hook in self._event_hooks["response"]: │ │
│ │ 1697 │ │ │ │ │ await hook(response) │ │
│ │ │ │
│ │ ╭────────────────────────────────── locals ──────────────────────────────────╮ │ │
│ │ │ follow_redirects = True │ │ │
│ │ │ history = [] │ │ │
│ │ │ request = <Request('GET', 'http://localhost:9997/v3/paths/list')> │ │ │
│ │ │ self = <httpx.AsyncClient object at 0x10e805950> │ │ │
│ │ ╰────────────────────────────────────────────────────────────────────────────╯ │ │
│ │ │ │
│ │ /Users/hermeschen/Repo/work/stream-server/.venv/lib/python3.13/site-packages/httpx/_client.py:1730 in │ │
│ │ _send_single_request │ │
│ │ │ │
│ │ 1727 │ │ │ ) │ │
│ │ 1728 │ │ │ │
│ │ 1729 │ │ with request_context(request=request): │ │
│ │ ❱ 1730 │ │ │ response = await transport.handle_async_request(request) │ │
│ │ 1731 │ │ │ │
│ │ 1732 │ │ assert isinstance(response.stream, AsyncByteStream) │ │
│ │ 1733 │ │ response.request = request │ │
│ │ │ │
│ │ ╭────────────────────────────── locals ───────────────────────────────╮ │ │
│ │ │ request = <Request('GET', 'http://localhost:9997/v3/paths/list')> │ │ │
│ │ │ self = <httpx.AsyncClient object at 0x10e805950> │ │ │
│ │ │ start = 264998.268840791 │ │ │
│ │ │ transport = <httpx.AsyncHTTPTransport object at 0x10e806c10>Source: encode/httpx