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

Resources not being released - Too many open files #38

Closed
cpagravel opened this issue Nov 15, 2022 · 6 comments
Closed

Resources not being released - Too many open files #38

cpagravel opened this issue Nov 15, 2022 · 6 comments

Comments

@cpagravel
Copy link

cpagravel commented Nov 15, 2022

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
Copy link
Owner

miguelgrinberg commented Nov 16, 2022

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

@miguelgrinberg
Copy link
Owner

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

simple-websocket 0.9.0 is out with this fix.

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

No branches or pull requests

2 participants