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

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

Closed
dstruck opened this issue Jan 28, 2019 · 5 comments
Labels
missing feature a feature of vanilla asyncio is not implemented in uvloop

Comments

@dstruck
Copy link

dstruck commented Jan 28, 2019

  • 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__
@1st1
Copy link
Member

1st1 commented Jan 30, 2019

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

@1st1 1st1 added the missing feature a feature of vanilla asyncio is not implemented in uvloop label Jan 30, 2019
@jlaine
Copy link
Contributor

jlaine commented Feb 16, 2019

This was fixed by PR #224

@jlaine jlaine closed this as completed Feb 16, 2019
@ahesford
Copy link

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.

@jlaine jlaine reopened this Mar 11, 2019
jlaine added a commit to jlaine/uvloop that referenced this issue Mar 16, 2019
The Server class was missing __aenter__ and __aexit__ magic methods to
allow usage of the form:

  async with server:
    await server.serve_forever()
jlaine added a commit to jlaine/uvloop that referenced this issue Mar 16, 2019
The Server class was missing __aenter__ and __aexit__ magic methods to
allow usage of the form:

  async with server:
    await server.serve_forever()
jlaine added a commit to jlaine/uvloop that referenced this issue Mar 16, 2019
The Server class was missing __aenter__ and __aexit__ magic methods to
allow usage of the form:

  async with server:
    await server.serve_forever()
@1st1 1st1 closed this as completed in d6c67e7 Mar 17, 2019
@cemsbr
Copy link

cemsbr commented Jun 21, 2019

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

Edit: more info:

  • uvloop 0.12.2
  • Python 3.7.3
  • Platform: Linux

@jlaine
Copy link
Contributor

jlaine commented Jun 22, 2019

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
Labels
missing feature a feature of vanilla asyncio is not implemented in uvloop
Projects
None yet
Development

No branches or pull requests

5 participants