Skip to content

connection timed out after few minutes #110

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

Closed
gencer opened this issue May 12, 2016 · 8 comments
Closed

connection timed out after few minutes #110

gencer opened this issue May 12, 2016 · 8 comments

Comments

@gencer
Copy link

gencer commented May 12, 2016

I have a definition like this:

 while True:
        if not websocket.open:
            await req.close()
            return
            message = await websocket.recv()

And few minutes later if i stay idle i get this on server and disconnect user:

  File "/usr/lib/python3.5/asyncio/selector_events.py", line 662, in _read_ready
    data = self._sock.recv(self.max_size)
TimeoutError: [Errno 110] Connection timed out

How can avoid this and stay connected as long as possible?

@aaugustin
Copy link
Member

You should wrap await websocket.recv() in a task with a timeout. asyncio provides APIs for that.

If the timeout is reached, await websocket.ping() and let while True loop resume.

I suggest using 25 seconds for the timeout as many systems close idle connections after 30 seconds.

(I'm interested in hearing whether that solution actually works. Perhaps I should document it better.)

@gencer
Copy link
Author

gencer commented May 14, 2016

@aaugustin are you talking about asyncio.timeout(timeout, *, loop=None) ? Perhaps an example for your suggestion :)

@aaugustin
Copy link
Member

Yes, that's the easier way to achieve the desired timeout here.

I thought it was added in Python 3.5 but apparently it also exists in 3.4 (and I don't think 3.3 matters much these days).

@gencer
Copy link
Author

gencer commented May 14, 2016

My python version is 3.5 so, It won't be a problem for me. I will test this ASAP.
--Which is i cannot find any example on anywhere yet :)
Thanks!

@gencer
Copy link
Author

gencer commented Jun 7, 2016

I solved this issue as you suggested.

Thanks.

Case closed now.

@gencer gencer closed this as completed Jun 7, 2016
@aaugustin
Copy link
Member

Actually I'd like to keep this issue around to check if I can improve documentation.

@aaugustin aaugustin reopened this Jun 7, 2016
@gencer
Copy link
Author

gencer commented Jun 8, 2016

@aaugustin: Actually i'll (and others) appreciate if you can document this subject as you stated. In fact, I fixed already but still need some document to check if i did the correct way.

@aaugustin
Copy link
Member

This will be built-in if #449 gets merged.

@aaugustin aaugustin mentioned this issue Jun 23, 2019
18 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants