python-trio / trio

Trio – a friendly Python library for async concurrency and I/O
https://trio.readthedocs.io
Other
6.19k stars 341 forks source link

`open_tcp_stream` fails #3134

Closed hlovatt closed 17 hours ago

hlovatt commented 1 day ago

The client server example in the tutorial fails because open_tcp_stream fails:

import sys

import trio
import trio.testing

async def sender(client_stream):
    print("sender: started!")
    while True:
        data = b"async can sometimes be confusing, but I believe in you!"
        print(f"sender: sending {data!r}")
        await client_stream.send_all(data)
        await trio.sleep(1)

async def receiver(client_stream):
    print("receiver: started!")
    async for data in client_stream:
        print(f"receiver: got data {data!r}")
    print("receiver: connection closed")
    sys.exit()

async def echo_parent():
    ADDR = 'localhost'
    PORT = 12345
    print(f"parent: connecting to {ADDR}:{PORT}")
    async with await trio.open_tcp_stream(ADDR, PORT) as client_stream:
        async with trio.open_nursery() as nursery:
            print("parent: spawning sender...")
            nursery.start_soon(sender, client_stream)

            print("parent: spawning receiver...")
            nursery.start_soon(receiver, client_stream)

# async def echo_parent():
#     async with trio.open_nursery() as nursery:
#         print("parent: spawning receiver...")
#         listeners = await nursery.start(trio.serve_tcp, receiver, 0)
#         client_stream = await trio.testing.open_stream_to_socket_listener(listeners[0])
#         print("parent: spawning sender...")
#         nursery.start_soon(sender, client_stream)

trio.run(echo_parent)

Fails with:

  File "/Users/lov080/Library/Mobile Documents/com~apple~CloudDocs/Python/examples/Client Server example/pythonProject/trioclientserver1.py", line 27, in echo_parent
    async with await trio.open_tcp_stream(ADDR, PORT) as client_stream:
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lov080/Library/Mobile Documents/com~apple~CloudDocs/Python/examples/Client Server example/pythonProject/.venv/lib/python3.12/site-packages/trio/_highlevel_open_tcp_stream.py", line 407, in open_tcp_stream
    raise OSError(msg) from ExceptionGroup(msg, oserrors)
OSError: all attempts to connect to localhost:12345 failed

If echo_parent is commented out and the alternate version of echo_parent un-commented, then works as expected.

Using MacBook Air M1 2020 with Sequoia 15.1.

CoolCat467 commented 1 day ago

Kind of expected if you think about it. First version is trying to connect to local server, and has an error because local server is not running. You need to start the local server and then try to connect with client.

hlovatt commented 1 day ago

From the error message it fails before getting to 'spawning' either the server or client.

However, I'm away from my laptop and replying on my phone. Will check by swapping over spawning order when I'm back with laptop.

A5rocks commented 1 day ago

The script you're running is the client of an echo server. There's also a server to run. That is, you need to run (assuming you cloned trio) python ./docs/source/tutorial/echo-server.py and then python ./docs/source/tutorial/echo-client.py.

This is noted in the tutorial as:

Note that this code will not work without a TCP server such as the one we’ll implement below.

Hopefully that will work. If the tutorial is structured weirdly, feel free to suggest changes!

hlovatt commented 17 hours ago

Thanks. Completely missed that sentence in the tutorial. Works now.