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

Possible memory leak when using Flask and send_from_directory #461

Open
cwilvx opened this issue Mar 3, 2025 · 0 comments
Open

Possible memory leak when using Flask and send_from_directory #461

cwilvx opened this issue Mar 3, 2025 · 0 comments

Comments

@cwilvx
Copy link

cwilvx commented Mar 3, 2025

I noticed that the memory usage for my application (https://github.com/swingmx/swingmusic) grows by ~4MB upto 20MB every time I stream audio files. The memory is never released and grows indefinitely as long as I'm streaming files. Investigating further showed that this only happens when I'm using waitress to serve the app.

I'm not sure whether other non-file sending routes are affected, but the memory increase when sending files is significant. Running the app for a week increases the memory upto ~4GB under moderate usage.

The minimal code that reproduces this problem is pasted below.

Dependencies:

  • Waitress v3.0.2
  • Flask v3.1.0
import waitress
from pathlib import Path
from flask import Flask, request, send_from_directory

app = Flask(__name__)


@app.route("/")
def stream_file():
    filepath = request.args.get("filepath")
    path = Path(filepath)

    if not path.exists():
        return "File not found", 404

    return send_from_directory(path.parent, path.name)


if __name__ == "__main__":
    # app.run(host="0.0.0.0", debug=False,port=1980)
    waitress.serve(app=app, port=1980)

Please check out the repo that I've setup to help reproduce this issue:

https://github.com/cwilvx/waitress-flask-memory-bug

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

1 participant