miguelgrinberg / flask-sock

Modern WebSocket support for Flask.
MIT License
271 stars 24 forks source link

Resources not being released - Too many open files #38

Closed cpagravel closed 1 year ago

cpagravel commented 1 year ago

I'm using this lovely library to facilitate socket communication. I really like this because it's so light. However, I ran into an issue where I've noticed that resources are not being released.

Issue

I was running a server for an extended period of time and repeatedly opening and close the page. It got to a point where the server started throwing "too many files open errors". I looked more into this and found that there are a lot of eventpoll handles open. This can be seen using the lsof command on the PID of the flask processes.

COMMAND   PID  USER   FD      TYPE     DEVICE SIZE/OFF    NODE NAME
...
python  26774 chris   15u  a_inode       0,13        0    7189 [eventpoll]

Repro

Create basic setup for web socket.

Files:

# app.py

from flask import Flask, render_template
from flask_sock import Sock

app = Flask(__name__)
sock = Sock(app)

@app.route("/")
def main() -> str:
  return render_template("main.html")

@sock.route("/echo")
def echo(ws) -> None:
  try:
    while True:
      data = ws.receive()
      ws.send(data)
  finally:
    print("echo end")
# main.html
<!doctype html>
<html lang="en"><head></head><body>
<script>

      let ws = new WebSocket(`ws://${location.host}/echo`);
      ws.onopen = () => {
        ws.send("test");
      }
</script>
</body></html>

Steps:

  1. Open the webpage and close it.
  2. On the server, after every time the page gets opened, use the following command to count eventpoll handles:
    function countpoll {
    for p in $(pgrep -f "flask run"); do
    lsof -p $p | grep eventpoll | wc -l
    done;
    }

You'll see the number go up after every time the page gets reloaded.

System Info

I've reproduced this issue on Ubuntu and Raspian OS environments.

I'm using Python 3.9.2

Here's the Python packages I'm using.

flask-sock==0.5.2
python-socketio==5.7.2
simple-websocket==0.8.1
websockets==10.4
miguelgrinberg commented 1 year ago

Thanks. Might be addressed by #20, which I'm still reviewing.

miguelgrinberg commented 1 year ago

Thank you so much for the details. I believe the fix referenced above (which I just merged) addressed your issue. Could I ask you to install simple-websocket's main branch from GitHub and retest? Let me know if the issue is addressed.

cpagravel commented 1 year ago

Superb work! It fixes the issue. I don't see the number of handles rise after I've closed the page. Could you provide a release?

cpagravel commented 1 year ago

Please hold off. I'm facing another issue and I'm not sure if it's related. I would like to double check my results.

cpagravel commented 1 year ago

It looks like the issues I'm seeing are unrelated to this specific change. I'll investigate the the issue I'm seeing before filing a ticket to see if it's my code.

miguelgrinberg commented 1 year ago

simple-websocket 0.9.0 is out with this fix.