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

Watchdog installed breaks development server #2102

Open
rstcruzo opened this issue Oct 21, 2024 · 11 comments
Open

Watchdog installed breaks development server #2102

rstcruzo opened this issue Oct 21, 2024 · 11 comments
Labels

Comments

@rstcruzo
Copy link

rstcruzo commented Oct 21, 2024

Describe the bug
When I start the socketio server in development mode (config["DEBUG"] = True) and watchdog is installed, no client can't connect to it. I don't see any errors on server side but every client reports Connection Refused.

To Reproduce
Steps to reproduce the behavior:

  1. Use the example/app.py file in this repo
  2. Set config["DEBUG"] = True
  3. Install watchdog and other dependencies
  4. Start the server with python app.py
  5. Try to connect with any client

Expected behavior
The client can connect successfully

Logs
No error logs on server side.
On client side:

Traceback (most recent call last):
  File "/Users/rsantacruz/Code/client-example.py", line 4, in <module>
    sio.connect("ws://localhost:5000")
  File "/Users/rsantacruz/Code/.venv/lib/python3.9/site-packages/socketio/simple_client.py", line 82, in connect
    self.client.connect(url, headers=headers, auth=auth,
  File "/Users/rsantacruz/Code/.venv/lib/python3.9/site-packages/socketio/client.py", line 159, in connect
    raise exceptions.ConnectionError(exc.args[0]) from None
socketio.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=5000): Max retries exceeded with url: /socket.io/?transport=polling&EIO=4&t=1729524516.573758 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x105cf6580>: Failed to establish a new connection: [Errno 61] Connection refused'))

Additional context
I can't just uninstall watchdog because I'm also using dramatiq which uses it for autoreloading the code.

@miguelgrinberg
Copy link
Owner

miguelgrinberg commented Oct 21, 2024

This package does not provide a web server. This is an inconpatibility between watchdog and your chosen web server. If you are using the Flask development web server, note that it also has a code reloading feature, the issue might by these two reloading features interfering with each other. I suggest you try running your application with debug mode disabled if you are using watchdog for code reloads. Maybe that solves your problem.

@rstcruzo
Copy link
Author

rstcruzo commented Oct 21, 2024

Maybe I wasn't clear. The server I'm running is the one provided by this package, I'm running:

socketio.run(app)

The docs of that function states Run the SocketIO web server..

The Flask development server works fine. Flask automatically uses watchdog if it's installed. The issue lies in the incompatibility between the SocketIO web server and watchdog.

@miguelgrinberg
Copy link
Owner

I don't think you understand the extent of what this package does. As I said above, there is no web server here. All this package does is add routes on top of the web server you use for your Flask application. The socketio.run() function just starts your chosen web server. If you are using the Flask dev server, then using flask run achieves exactly the same result as socketio.run().

@rstcruzo
Copy link
Author

Oh, I see. So all this package does is add the /socket.io route to the flask application. Thank you for the explanation! I thought this was meant to be run as a separate server.

I wonder why my Flask development server, which I run with flask run, works, but when I run socketio.run(), no connections can be established. I guess it's something in my setup.

Thank you again, I'll try to dig more.

@miguelgrinberg
Copy link
Owner

miguelgrinberg commented Oct 21, 2024

Do you have gevent or eventlet installed? If you have, then you are not using the flask web server, so the issue is with one of these other web servers.

@rstcruzo
Copy link
Author

I do have gevent installed. Does that mean the incompatibility is between gevent and watchdog?

@miguelgrinberg
Copy link
Owner

miguelgrinberg commented Oct 23, 2024

@rstcruzo the fact that watchdog-gevent exists is a pretty good indication that these two packages do not like each other when used on their own.

Note that if you are using gevent, then you are not using the Flask development web server, you are using the gevent web server. Maybe you did not realize this, but this could be part of the confusion.

Any reason you need gevent in your project? You can try not using it. Flask-SocketIO works well with standard threads, and this would eliminate any incompatibilities with watchdog. If you uninstall gevent (and eventlet if you have it installed) then Flask-SocketIO will automatically decide to use threads for its concurrency needs.

@rstcruzo
Copy link
Author

Now that I'm using flask run to start the server I see the regular Werkzeug log WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead..

I want to use gevent because it is said to be more efficient; greenlets are lighter than threads.

To my understanding, If I use Werkerzug in development mode and gevent in production, I should be fine, as watchdog isn't used in production. Or am I missing something?

@miguelgrinberg
Copy link
Owner

I suppose you can, assuming nothing else conflicts with gevent.

@rstcruzo
Copy link
Author

@miguelgrinberg Quick question: is gevent-websocket needed anymore? I see you removed it from python-socketio here, but I still see a warning "WebSocket transport not available. Install gevent-websocket for improved performance." when it's not installed. The warning seems to be coming from here

@miguelgrinberg
Copy link
Owner

No, gevent-websocket is not needed because in recent versions the simple-websocket package provides the same functionality. The warning in this package is outdated, I will remove it.

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

2 participants