Skip to content

Possible memory leak when using Flask and send_from_directory #461

@cwilvx

Description

@cwilvx

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions