Skip to content

Python 3.7 "TCP echo server using streams" sample code does not work with uvloop. #221

Closed

Description

@dstruck
  • uvloop version: 0.12.0
  • Python version: 3.7.1
  • Platform: Linux
  • Can you reproduce the bug with PYTHONASYNCIODEBUG in env?: not tested

Running the following code (source: https://docs.python.org/3/library/asyncio-stream.html#tcp-echo-server-using-streams) with uvloop:

import asyncio
import uvloop
import sys

async def handle_echo(reader, writer):
  data = await reader.read(100)
  message = data.decode()
  addr = writer.get_extra_info('peername')

  print(f"Received {message!r} from {addr!r}")

  print(f"Send: {message!r}")
  writer.write(data)
  await writer.drain()

  print("Close the connection")
  writer.close()

async def main():
  server = await asyncio.start_server(handle_echo, '127.0.0.1', 8888)

  addr = server.sockets[0].getsockname()
  print(f'Serving on {addr}')

  async with server:
    await server.serve_forever()

uvloop.install()

print("python version:", sys.version)
print("uvloop version:", uvloop.__version__)

asyncio.run(main())

Produces the following error message:

python uvlopp_issue.py 
python version: 3.7.1 (default, Dec 14 2018, 19:28:38) 
[GCC 7.3.0]
uvloop version: 0.12.0
Serving on ('127.0.0.1', 8888)
Traceback (most recent call last):
  File "uvlopp_issue.py", line 36, in <module>
    asyncio.run(main())
  File "/home/test/miniconda3/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "uvloop/loop.pyx", line 1451, in uvloop.loop.Loop.run_until_complete
  File "uvlopp_issue.py", line 27, in main
    async with server:
AttributeError: __aexit__

Activity

  1. 1st1 commented on Jan 30, 2019

    @1st1
    Member

    Thank you for reporting. This is basically a duplicate of #211.

  2. added
    missing featurea feature of vanilla asyncio is not implemented in uvloop
    on Jan 30, 2019
  3. jlaine commented on Feb 16, 2019

    @jlaine
    Contributor

    This was fixed by PR #224

  4. ahesford commented on Mar 11, 2019

    @ahesford

    I don't believe PR #224 fixes this issue. This issue is caused because the stock asyncio.base_events.Server in Python 3.7 was made into an asynchronous context manager. The entry method is simple:

    async def __aenter__(self):
        return self
    

    while the exit method closes the server:

    async def __aexit__(self, *exc):
        self.close()
        await self.wait_closed()
    

    The TCP echo server uses this manager in an async with server clause to ensure the server closes.

    The uvloop replacement, uvloop.loop.Server, does not implement the methods for an asynchronous context manager in the master branch or in commit #8fac545a9351935eefc0c8d281bebd0dd2917d66 to address PR #224.

  5. reopened this on Mar 11, 2019
  6. added 3 commits that reference this issue on Mar 16, 2019
    28e1c47
    10ddd23
    abc4424
  7. cemsbr commented on Jun 21, 2019

    @cemsbr

    The error persists. Won't the fix be merged?

    Edit: more info:

    • uvloop 0.12.2
    • Python 3.7.3
    • Platform: Linux
  8. jlaine commented on Jun 22, 2019

    @jlaine
    Contributor

    The fix landed for v0.13.0, you can for instance give the release candidate a go: https://github.com/MagicStack/uvloop/releases/tag/v0.13.0rc1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    missing featurea feature of vanilla asyncio is not implemented in uvloop

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions