Skip to content

Task exception was never retrieved #3991

@Bingdom

Description

@Bingdom

Describe the Bug

Using the following code below:

from collections.abc import AsyncIterator
import asyncio
import strawberry
from aiohttp import web
from strawberry.aiohttp.views import GraphQLView
import traceback

HTTP_SERVER_PORT = 1400

@strawberry.type
class Query:
    @strawberry.field
    def hello(self) -> str:
        return "Hello World!"


@strawberry.type
class Subscription:
    @strawberry.subscription
    async def longRunning(self, count: int) -> AsyncIterator[int]:
        try:
            for i in range(count):
                yield i
                await asyncio.sleep(0.1)
        except (asyncio.CancelledError, GeneratorExit) as e:
            print(repr(e))
        except Exception as e:
            print(repr(e))
            print(
                f"{''.join(traceback.format_tb(e.__traceback__))}")


schema = strawberry.Schema(query=Query, subscription=Subscription)


def httpRunner(view) -> web.AppRunner:
    app = web.Application()
    app.add_routes([
        web.post('/graphql', view),
        web.get('/graphql', view)
    ])

    return web.AppRunner(app)


async def main():
    strawberryView = GraphQLView(schema)
    webHttpRunner = httpRunner(strawberryView)
    await webHttpRunner.setup()
    # stop blocking the backend from shutting down. Only have n seconds
    httpSite = web.TCPSite(webHttpRunner, '0.0.0.0',
                           HTTP_SERVER_PORT, shutdown_timeout=1)

    print("Starting http server")
    await httpSite.start()
    # Keep the server running until interrupted
    try:
        while True:
            await asyncio.sleep(3600)
    except (KeyboardInterrupt, asyncio.CancelledError):
        pass

asyncio.run(main(), debug=True)

Then use Insomnia or another http client capable of websockets. Attempt to subscribe to longRunning, then disconnect. Every once in a while you'll get the following exception:

Task exception was never retrieved
future: <Task finished name='Task-115' coro=<<async_generator_asend without __name__>()> exception=StopAsyncIteration() created at c:\python312\Lib\asyncio\tasks.py:695>
source_traceback: Object created at (most recent call last):
  File "C:\Users\user\Downloads\nuitka_gen_bug\backend.py", line 67, in <module>
    asyncio.run(main(), debug=True)
  File "c:\python312\Lib\asyncio\runners.py", line 195, in run
    return runner.run(main)
  File "c:\python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
  File "c:\python312\Lib\asyncio\base_events.py", line 678, in run_until_complete
    self.run_forever()
  File "c:\python312\Lib\asyncio\windows_events.py", line 322, in run_forever
    super().run_forever()
  File "c:\python312\Lib\asyncio\base_events.py", line 645, in run_forever
    self._run_once()
  File "c:\python312\Lib\asyncio\base_events.py", line 1991, in _run_once
    handle._run()
  File "c:\python312\Lib\asyncio\events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "C:\Users\user\AppData\Roaming\Python\Python312\site-packages\strawberry\subscriptions\protocols\graphql_transport_ws\handlers.py", line 283, in run_operation
    async for result in result_source:
  File "C:\Users\user\AppData\Roaming\Python\Python312\site-packages\strawberry\schema\schema.py", line 859, in _subscribe
    async for result in aiter_or_result:
  File "C:\Users\user\AppData\Roaming\Python\Python312\site-packages\graphql\execution\map_async_iterator.py", line 38, in __anext__
    anext = ensure_future(self.iterator.__anext__())
  File "c:\python312\Lib\asyncio\tasks.py", line 695, in ensure_future
    return loop.create_task(coro_or_future)
StopAsyncIteration

System Information

  • Operating system: Windows 11
  • Python version: Python 3.12.10
  • Strawberry version (if applicable): 0.282.0

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions