falconry / falcon

The no-magic web data plane API and microservices framework for Python developers, with a focus on reliability, correctness, and performance at scale.
https://falcon.readthedocs.io/en/stable/
Apache License 2.0
9.51k stars 937 forks source link

[ASGI] close() not called if iterating over resp.stream raises an exception #1943

Closed vytas7 closed 2 years ago

vytas7 commented 3 years ago

At the time of writing, the close() method of resp.stream is only called if the provided generator is successfully exhausted (or yields None).

However, the primary use case of the close() method is freeing up resources, so one might expect the method to be called regardless. It is also what PEP 3333 compliant WSGI servers do.

I would suggest introducing a finally clause, and moving the invocation of close() there.

Make sure to also write proper regression tests:

Kurt Griffiths @kgriffs We also need a test that forces stream to raise an error. (and ensures close() is called)

dimucciojonathan commented 3 years ago

Hi Vytautas,

I am interested in helping contribute to Falcon. Can I have this issued assigned to me?

vytas7 commented 3 years ago

Hi @dimucciojonathan! Sure, go ahead! (A comment here is enough so that ppl know you're working on it.)

dimucciojonathan commented 3 years ago

Awesome, thanks!