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

Websocket connections are not closed properly when we use them with WSGI middleware. #543

Closed
Hardtack opened this issue Jan 29, 2019 · 3 comments · Fixed by #544
Closed

Comments

@Hardtack
Copy link
Contributor

Hardtack commented Jan 29, 2019

I'm getting following error whenever websocket connections are closed.

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/eventlet/wsgi.py", line 580, in handle_one_response
    write(b'')
  File "/usr/local/lib/python3.6/site-packages/eventlet/wsgi.py", line 472, in write
    raise AssertionError("write() before start_response()")
AssertionError: write() before start_response()

More precisely, I'm using Flask-SocketIO with eventlet, with Flask integration of sentry-sdk.

As I see it, the problem occurred by following reason.

In eventlet.websocket, you made a special object called ALREADY_HANDLED which makes special functionalities for websocket handlers

# use this undocumented feature of eventlet.wsgi to ensure that it
# doesn't barf on the fact that we didn't call start_response
return wsgi.ALREADY_HANDLED

And, in eventlet.wsgi

The websocket-specific functionalities are handled by type of the return value of WSGI app.

eventlet/eventlet/wsgi.py

Lines 547 to 550 in 05d613d

result = self.application(self.environ, start_response)
if (isinstance(result, _AlreadyHandled)
or isinstance(getattr(result, '_obj', None), _AlreadyHandled)):
self.close_connection = 1

However, In sentry-sdk, the plugin wraps the WSGI responses (which are iterable objects) with there wrapper called _ScopedResponse.

This is fine, since PEP 3333 specifies the return value of WSGI app just have to be an iterable object.

But it breaks the type check on eventlet.wsgi. So eventlet doesn't finish at the if statement, and emits the above error message.

I think, you may have to use another way to handle websocekt specific features.

@jamesharding
Copy link

I am encountering the exact same issue. @Hardtack Did you manage to find a workaround?

@Hardtack
Copy link
Contributor Author

@jamesharding I already sent a pull request that fix this issue, but it's not be merged yet.

@ranihorev
Copy link

@Hardtack it looks like it was approved. It'd be great if you'll merge it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants