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

'Received a broken close frame containing invalid UTF-8' upon receiving an emit on client-side #755

Closed
mvelebit opened this issue Aug 3, 2018 · 25 comments
Labels

Comments

@mvelebit
Copy link

mvelebit commented Aug 3, 2018

I'm sending a simple message by doing
socket.emit('blabla', {'wow': 20000})
I invoke the function which invokes that and nothing happens until ~30 seconds later my client connection snaps and it prints out Received a broken close frame containing invalid UTF-8 in Chrome console.

Now, the function from which I invoke the emit is run in a separate thread, but uses the same SocketIO object for communication, seems that that messes things up, any clues?

@miguelgrinberg
Copy link
Owner

Can you create a simple self-contained example that shows this error?

@mvelebit
Copy link
Author

mvelebit commented Aug 6, 2018

I resolved the issue by switching to eventlet from the standard flask dev server, it was an issue with my code, thanks for the quick response anyways!

@mvelebit mvelebit closed this as completed Aug 6, 2018
@mbwolff
Copy link

mbwolff commented Jan 1, 2019

I am trying to do the same thing (I invoke emit with a separate thread but nothing happens until many seconds later) but I get this error and am not sure how to fix it. I am using eventlet with gunicorn. Any suggestions?

@miguelgrinberg
Copy link
Owner

@mbwolff do you have an example I can use to reproduce the problem?

@mbwolff
Copy link

mbwolff commented Jan 1, 2019

Here's an example based on the code I'm developing.

SonGenIO copy.zip

To launch the server I use

gunicorn -b localhost:5000 -t 120 -w 1 --preload --worker-class eventlet makeSonnetTest:app

I simulated the delay from the threaded function with sleep(5). After about 30 seconds after the thread completes its task I get the following error:

WebSocket connection to 'ws://localhost:5000/socket.io/?EIO=3&transport=websocket&sid=423e65570d53417abf92517126f411ff' failed: Received a broken close frame containing invalid UTF-8.

Thanks for taking a look at this!

@miguelgrinberg
Copy link
Owner

@mbwolff Not sure what I'm looking for. I go to http://localhost:5000/start click submit and another page loads. The server starts working on this background task, and nothing else happens.

@mbwolff
Copy link

mbwolff commented Jan 1, 2019

@miguelgrinberg The sonnet page should load after the form is submitted and then update via socketio and Javascript with <LI> list items added as verses are generated by the threaded function. I'm not sure if it's a problem with the way I'm using socketio, threads or Javascript. I can assess to a certain degree what's going on with Web Sockets using Chrome's Developer Tools (no activity for a while and then the error message).

@mbwolff
Copy link

mbwolff commented Jan 1, 2019

I fixed a few things in makeSonnetTest.py (I was invoking socketio.run() incorrectly). Here's an updated zip file:

SonGenIO copy.zip

I'm still getting the same errors.

@miguelgrinberg
Copy link
Owner

You are not running the app in the right way, you need to use socketio.run(app) or flask run. Not sure if that's the problem or not, but that's definitely something you are not doing correctly.

@danielkrich
Copy link

Does anyone have a solution for this? I'm having the same problem.

@vonsky104
Copy link

I also have the same problem.
Basically it happens almost everytime when second client connects, the first one gets a disconnect and I get the same error which is on the subject of this issue.

@miguelgrinberg
Copy link
Owner

@danielkrich @vonsky104 if you can provide a complete example application that reproduces the issue I'm happy to investigate.

@Nv7-GitHub
Copy link

I was receiving the same issue, and what I did to fix it was actually in the JS.
Originally, the server emit looked something like this:

emit('start', data)  # The data could be anything

and the code on the client-side (JS) looked like this (socket is the object returned after io.connect() is run):

socket.on('start', function start(){/* Some code */});

What I did to fix this was to add the variable data into the parameters for the start function.
After this, the code looked like this:

socket.on('start', function start(data){/* Some code */});

I hope this helps!

@ApollonGT
Copy link

ApollonGT commented Dec 1, 2020

I had the same issue with python3.8.6 on a Fedora Linux 33, on AMD CPU (although I believe the system is irrelevant in this case).
I am using pipenv for the specific python version.

Checking the error messages I ended up on the eventlet source related to wsaccel.

More specifically I got an exception:

AttributeError: module 'wsaccel.utf8validator' has no attribute `Utf8Validator`

After uninstalling and installing again (even without installing it seems to work) the package wsaccel it worked.
Note that I did a force reinstall the same package and it didn't work.

I hope it helps someone!

@guansss
Copy link

guansss commented Feb 9, 2021

I met this issue when attempting to broadcast messages in a background thread, and I was able to fix it by setting up the monkey patching:

import eventlet

eventlet.monkey_patch() 

I also had to set "gevent": true in my VSCode debugging configuration to solve another problem that came with the monkey patching.

@jalapatip
Copy link

jalapatip commented Mar 7, 2023

@miguelgrinberg, I'm also facing the same issue. I tried monkey_patch, setting async_mode, decreasing ping_timeout. But nothing seems to work. Any suggestions?

Also, the behavior is not consistent. The values are published on the website which confirms that it is not a connection issue. I'm guessing it has something to do with my timeout.

@miguelgrinberg
Copy link
Owner

@jalapatip As I told every other person who commented in this thread. If you can provide a runnable example that demonstrates the issue I can investigate. You are not providing anything, so I really have nothing to suggest.

@jalapatip
Copy link

jalapatip commented Mar 7, 2023

@miguelgrinberg, I'm attaching a drive link containing an executable sample of my code. I also attached the logs from the console and web interface.

https://drive.google.com/drive/folders/1IF_ZVQ6ZCEKMQtX3WJnX26x7jbplocri?usp=sharing

Thanks for helping me debug!

@miguelgrinberg
Copy link
Owner

@jalapatip your application does not produce any errors for me.

@jalapatip
Copy link

That's strange! I'm getting the following error in the browser. I also tried switching between chrome and firefox.

EIO=4&transport=websocket&sid=dcD_HFkNukjkQjhdAAAQ' failed: Received a broken close frame containing invalid UTF-8.

How are you running the application?

@miguelgrinberg
Copy link
Owner

@jalapatip

python app.py

@jalapatip
Copy link

@miguelgrinberg,

So, I was able to host the service using gunicorn and it's publishing:

gunicorn app:app --worker-class eventlet -w 1 --bind 127.0.0.1:8080 --reload

But not when I do a simple:

python3 app.py

Any idea, what could be the issue?

@miguelgrinberg
Copy link
Owner

@jalapatip I don't understand what you mean by "it's publishing". In any case, any times there are errors or issues you should check out the server log for clues.

@jalapatip
Copy link

@miguelgrinberg, I have checked the server logs and see that it is emitting messages. The idea is to display these messages on a UI. On the console, I see the following error:

Screen Shot 2023-03-13 at 7 04 00 PM

I thought it had something to do with the structure of the message. But this is not a consistent issue. The thread emitting messages runs in a loop and the messages do get printed on the UI sometimes.

Other than that, I couldn't find any other errors or useful insights in the debug, error, and info logs (except for the above logs from console). I've also attached the same in the drive link earlier.

Also, this is only happening when I run python3 app.py. When I run it using gunicorn app:app --worker-class eventlet -w 1 --bind 127.0.0.1:8080 --reload, the application seems to be running fine.

@miguelgrinberg
Copy link
Owner

miguelgrinberg commented Mar 14, 2023

@jalapatip You have issues when you run a development build. You are using eventlet, and you also have the reloader enabled. Any of these things can be the problem, eventlet and gevent are known to be problematic when used with concurrent code that is not specifically designed with greenlets in mind.

Things you can try:

  • Stop using eventlet when running debug mode
  • Keep using eventlet, but stop using debug mode
  • Keep using eventlet and debug mode, but disable the reloader
  • Try older versions of eventlet
  • Switch from eventlet from gevent
  • Use gunicorn, since you are saying that it works fine

I'm not sure what other options you have beyond trying different combinations as the above examples.

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

9 participants