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

socket.send() raised exception #7

Closed
pohmelie opened this issue Sep 18, 2015 · 6 comments
Closed

socket.send() raised exception #7

pohmelie opened this issue Sep 18, 2015 · 6 comments
Labels

Comments

@pohmelie
Copy link
Collaborator

Sometimes, when trying to write to closed socket there is no ConnectionResetError exception, but some message to console "socket.send() raised exception" and program continue to execute as nothing happens. I don't understand the nature of this issue, but I saw same problem in stackoverflow questions. Here is code to reproduce, and this work on 3.4.2, 3.4.3 and 3.5.0 on my ubuntu 14.04:

import asyncio
import time


host, port = "127.0.0.1", 8888
data_size = 8192


@asyncio.coroutine
def write_worker():

    reader, writer = yield from asyncio.open_connection(host, port)
    while True:

        writer.write(b"-" * data_size)
        yield from writer.drain()


def read_worker(reader, writer):

    start = time.perf_counter()
    while time.perf_counter() - start < 0.5:

        data = yield from reader.read(data_size)

    writer.close()


if __name__ == "__main__":

    try:

        loop = asyncio.get_event_loop()
        coro = asyncio.start_server(read_worker, host, port)
        server = loop.run_until_complete(coro)
        loop.run_until_complete(write_worker())

    except KeyboardInterrupt:

        server.close()

    print("done")

If we put yield between writer.write(b"-" * data_size) and yield from writer.drain() then problem go away, but we can't use yield in 3.5.0 with coroutines.
What to do:

  • Left as is (:-1:)
  • use yield from asyncio.sleep(0)
  • something else?
@pohmelie pohmelie added the bug label Sep 18, 2015
@rsichnyi
Copy link
Contributor

The weird thing is that problem goes away also in case if you put something like print(data) after yield from reader.read(data_size) or yield from asyncio.sleep(0) before closing writer in read_worker(). Tried to run server and client in different processes (just to avoid running in same loop) - mentioned changes in server code fixes the issue as well...

pohmelie added a commit that referenced this issue Sep 18, 2015
@pohmelie
Copy link
Collaborator Author

What a hell?! 😃
So you think it is enough to make some «magic» on server side? Cause I think anyone (client or server) can close connection, while another side will writing to socket.

@rsichnyi
Copy link
Contributor

Idk honestly, but it feels like asyncio bug, "socket.send() raised exception" is the warning which, afaik, should never be encountered under normal circumstances.

@pohmelie
Copy link
Collaborator Author

python/asyncio#248
python/asyncio#257
@rsichny, you're right. That's asyncio issue.

@rsichnyi
Copy link
Contributor

rsichnyi commented Nov 2, 2015

python/asyncio#280

@pohmelie
Copy link
Collaborator Author

pohmelie commented Nov 2, 2015

🎉

@pohmelie pohmelie closed this as completed Nov 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants