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

Handling WebSocket disconnects #255

Closed
saghul opened this issue Jan 22, 2015 · 7 comments
Closed

Handling WebSocket disconnects #255

saghul opened this issue Jan 22, 2015 · 7 comments
Assignees

Comments

@saghul
Copy link
Contributor

saghul commented Jan 22, 2015

When reading from a ws connection, the receive_msg function may raise WSClientDisconnectedError. The example in the documentation just returns the ws object once done:

@asyncio.coroutine
def websocket_handler(request):
    ws = web.WebSocketResponse()
    ws.start(request)

    while True:
        try:
            data = yield from ws.receive_str()
            ws.send_str(data)
        except web.WSClientDisconnectedError:
            return ws

However, when an error is produced the exception is also stored in the closing_fut: https://github.com/KeepSafe/aiohttp/blob/master/aiohttp/web.py#L833-L859 As a result, a warning is logged because nobody retrieves that exception. I guess one way to "solve" it is to do something like this:

@asyncio.coroutine
def websocket_handler(request):
    ws = web.WebSocketResponse()
    ws.start(request)

    while True:
        try:
            data = yield from ws.receive_str()
            ws.send_str(data)
        except web.WSClientDisconnectedError:
            try:
                yield from ws.wait_closed()
            except web.WSClientDisconnectedError:
                pass
            return ws

But it kinda feels wrong.

Am I missing something or should this be handled differently? Thanks a lot for aiohttp, I'm really liking it so far!

@asvetlov
Copy link
Member

Got it, my wrong. Thanks for report, I'll publish 0.14.2 patch very shortly.

@asvetlov asvetlov self-assigned this Jan 22, 2015
@asvetlov asvetlov added the bug label Jan 22, 2015
@fafhrd91
Copy link
Member

@asvetlov wait with release

@fafhrd91
Copy link
Member

should be fixed

@saghul
Copy link
Contributor Author

saghul commented Jan 23, 2015

Thanks, I'll give it a shot. I see you added wait_eof in c587340, is that supposed to be used after getting the error while reading or will it be used internally?

@fafhrd91
Copy link
Member

RequestHandler always calls response.write_eof() and ServerHttpProtocol should handle WSClientDisconnectedError exception properly.

@saghul
Copy link
Contributor Author

saghul commented Jan 23, 2015

Thanks for clarifying!

@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants