The Wayback Machine - https://web.archive.org/web/20250409215850/https://github.com/falconry/falcon/issues/1963
Skip to content

StaticRouteAsync leaves open files #1963

Closed
@vytas7

Description

@vytas7

When using static routes with a falcon.asgi.App, it seems that the _AsyncFileReader wrapper does not implement any .close() method, so files are left open.

On CPython, I wasn't able to demonstrate any practical impact of this bug as the file object in question is refcounted to 0 and garbage collected as soon as it goes out of scope. However, that isn't the case when running uvicorn on PyPy 3.7, as PyPy uses a different GC implementation.

Test case in point:

import io
import logging
import os.path
import unittest.mock

import falcon.asgi

logging.basicConfig(
    format='%(asctime)s [%(levelname)s] %(message)s', level=logging.INFO)


class DebugIO(io.BytesIO):

    @classmethod
    def open(cls, *args, **kwargs):
        return cls(b'Test data!\n')

    def close(self):
        logging.info(f'{self}.close()')
        super().close()


app = falcon.asgi.App()
app.add_static_route('/files', '/tmp')

debug = unittest.mock.patch('io.open', DebugIO.open)
debug.start()

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggood first issueComment on this issue if you'd like to volunteer to work on this. Thanks!

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions