Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncio.ensure_future not working in reactor with inlineCallbacks #11841

Open
stalkerg opened this issue Apr 17, 2023 · 1 comment · May be fixed by #11890
Open

asyncio.ensure_future not working in reactor with inlineCallbacks #11841

stalkerg opened this issue Apr 17, 2023 · 1 comment · May be fixed by #11890
Labels

Comments

@stalkerg
Copy link

stalkerg commented Apr 17, 2023

Describe the incorrect behavior you saw
If you use await asyncio.ensure_future inside deffered you will produce an error: builtins.RuntimeError: await wasn't used with future
Example to reproduce:

# import uvloop
import asyncio
# uvloop.install()

from twisted.web.resource import Resource
from twisted.web.server import NOT_DONE_YET
from twisted.internet import reactor
from twisted.internet import defer


class ClockPage(Resource):
    isLeaf = True

    async def _some_background_task(self, request):
        print("hello!")

    async def _more_test(self, request):
        request.write(
            b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
            b"<title></title></head><body>TEST</body></html>"
        )
        print("start")
        await asyncio.ensure_future(self._some_background_task(request))
        # request.finish()
        print("finish")

    @defer.inlineCallbacks
    def _real(self, request):
        yield defer.Deferred.fromCoroutine(self._more_test(request))
        request.finish()

    def render_GET(self, request):
        reactor.callLater(0, self._real, request)
        return NOT_DONE_YET

resource = ClockPage()

Describe how to cause this behavior

Run such s project like twistd -r asyncio -n web --path . and refresh page a few time, after a small delay you will see such error.

If you have a traceback, please paste it here; otherwise, delete this.
2023-04-17T19:08:32+0900 [twisted.internet.defer#critical] Unhandled error in Deferred:
2023-04-17T19:08:32+0900 [twisted.internet.defer#critical] 
        Traceback (most recent call last):
          File "/test-twisted/env/lib/python3.11/site-packages/twisted/internet/defer.py", line 1697, in _inlineCallbacks
            result = context.run(gen.send, result)
          File "/test-twisted/test1.rpy", line 31, in _real
            yield defer.Deferred.fromCoroutine(self._more_test(request))
          File "/test-twisted/env/lib/python3.11/site-packages/twisted/internet/defer.py", line 1138, in fromCoroutine
            return _cancellableInlineCallbacks(coro)
          File "/test-twisted/env/lib/python3.11/site-packages/twisted/internet/defer.py", line 1857, in _cancellableInlineCallbacks
            _inlineCallbacks(None, gen, status, _copy_context())
        --- <exception caught here> ---
          File "/test-twisted/test1.rpy", line 31, in _real
            yield defer.Deferred.fromCoroutine(self._more_test(request))
          File "/test-twisted/env/lib/python3.11/site-packages/twisted/internet/defer.py", line 1697, in _inlineCallbacks
            result = context.run(gen.send, result)
          File "/test-twisted/test1.rpy", line 25, in _more_test
            await asyncio.ensure_future(self._some_background_task(request))
        builtins.RuntimeError: await wasn't used with future

Describe the correct behavior you'd like to see
asyncio.ensure_future should work as other async/await functions.

Testing environment

  • Operating System and Version; paste the output of these commands:
    Linux rainbow 6.2.5-gentoo #1 SMP PREEMPT_DYNAMIC Mon Mar 13 00:22:30 JST 2023 x86_64 AMD Ryzen 9 5900X 12-Core Processor AuthenticAMD GNU/Linux
    but I can confirm on latest Debian as well.
  • Twisted version 22.10.0
  • Reactor: asyncio
  • Python 3.11.4

Other:

This issue blocks to use aiohttp inside twisted.

@stalkerg stalkerg added the bug label Apr 17, 2023
stalkerg added a commit to stalkerg/twisted that referenced this issue Jul 1, 2023
@stalkerg stalkerg linked a pull request Jul 1, 2023 that will close this issue
@exarkun
Copy link
Member

exarkun commented Jul 1, 2023

As discussed at #11890 (comment) - this is working as intended. There may be a documentation shortcoming though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants