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

"Invalid frame header" error in browser #20

Closed
ekw opened this issue Feb 16, 2022 · 4 comments
Closed

"Invalid frame header" error in browser #20

ekw opened this issue Feb 16, 2022 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@ekw
Copy link

ekw commented Feb 16, 2022

First off, the example echo server example works for me. But when I try to modify it for my needs, I get an "Invalid frame header" error in browser at the end of the connection. Instead of a forever loop as in the echo example, I want to make a connection, send data, receive data and end the connection. I can send and receive data fine, but always get an "Invalid frame header" error in the browser at the end of the connection. My websocket endpoint is this:

@sock.route('/ws')
def ws(sock):
    data = sock.receive()  
    app.logger.info(data)   # "HI" from client received fine
    sock.send(1)  
    sock.send(2)
    sock.send(3)
    sock.close(message="BYE") # Error occurs in browser with or without this close()

On the client side:

const socket = new WebSocket('wss://myhost/ws');
socket.addEventListener('message', ev => { console.log(ev); })
socket.onopen = () => socket.send("HI");

I see the "HI" on the server, and I see all the integer data on the client. But the browser gets an error after the integer data is sent. It doesn't matter how many sends I do from the server, all the data is received by the client fine. It's only at the end that I always get an error on the browser.

On Chrome, the error is "Invalid frame header". On Firefox, the error is "The connection to wss://host/ws was interrupted while the page was loading."

I am using:
Python 3.8.10
gunicorn 20.1.0
gevent 21.12.0
nginx 1.18.0

gunicorn is being proxied by nginx. (As I'm detailing these versions, I now wonder if it's gunicorn/nginx not closing the connection properly.)

Does anyone know how to prevent the error from occurring?

edit: I took nginx and wss out of the equation and the error still occurs.

@miguelgrinberg miguelgrinberg added the bug Something isn't working label Feb 16, 2022
@miguelgrinberg miguelgrinberg self-assigned this Feb 16, 2022
@miguelgrinberg
Copy link
Owner

I'm not sure if this can be fixed. The issue happens because Flask, which does not recognize WebSocket requests, tries to issue a standard HTTP response when the connection ends.

@ekw
Copy link
Author

ekw commented Feb 17, 2022

OK, thanks for looking into it. Do the reason and message parameters in socket close function do anything at the moment (accessible by client)?

@miguelgrinberg
Copy link
Owner

I've made some improvements. Could you please test the main branch of this repo and report back?

Here is a summary:

  • If you use the eventlet or gunicorn servers, closing the connection works cleanly.
  • If you use the gevent server, closing the connection works cleanly from the browser's side, but the server shows a stack trace. I will need to spend more time investigating if that can be addressed.
  • If you use the werkzeug/Flask server, closing the connection remains broken. This requires fixes for Flask and/or Werkzeug, which do not know how to end a WebSocket request properly.

@ekw
Copy link
Author

ekw commented Feb 17, 2022

It works, thank you! I am using gunicorn proxied by nginx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants