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

test_client() does not close a file opened by send_file() #3735

Closed
danny0838 opened this issue Aug 20, 2020 · 1 comment
Closed

test_client() does not close a file opened by send_file() #3735

danny0838 opened this issue Aug 20, 2020 · 1 comment

Comments

@danny0838
Copy link

As #2468 has mentioned, send_file opens a file that mean to be closed by some further processing of the WSGI server. However, test_client does not really run the WSGI server, causing the opened file not closed until all test ends, and thus triggers a ResourceWarning and causes cleaning code after a test request to fail.

Expected Behavior

A file opened by send_file() should be closed after response is sent.

from flask import Flask
from flask import send_file
import unittest
import os

app = Flask(__name__, root_path='.')

@app.route('/<path:filepath>')
def handle_request(filepath):
    return send_file(filepath)

class Test(unittest.TestCase):
    def test(self):
        temp_file = os.path.join(os.path.dirname(__file__), 'test.txt')
        for i in ('123abc', '&@!#^*', '你好嗎'):
            with self.subTest(i=i):
                test_text = 'test' + str(i)
                with open(temp_file, 'w', encoding='UTF-8') as f:
                    f.write(test_text)
                try:
                    with app.test_client() as c:
                        r = c.get(temp_file)
                        self.assertEqual(r.data.decode('UTF-8'), test_text)
                finally:
                    os.remove(temp_file)

if __name__ == '__main__':
    unittest.main()

Actual Behavior

The opened file is not closed properly after the response is sent, causing following code in the test fail to delete the file (which is temporarily generated for testing purpose).

......\test.py:22: ResourceWarning: unclosed file <_io.BufferedReader name='......\\test.txt'>
  r = c.get(temp_file)
ResourceWarning: Enable tracemalloc to get the object allocation traceback
......\case.py:704: ResourceWarning: unclosed file <_io.BufferedReader name='......\\test.txt'>
  outcome.errors.clear()
ResourceWarning: Enable tracemalloc to get the object allocation traceback

======================================================================
ERROR: test (__main__.Test) (i=1)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "......\test.py", line 25, in test
    os.remove(temp_file)
PermissionError: [WinError 32] 程序無法存取檔案,因為檔案正由另一個程序使用。: '......\\test.txt'

======================================================================
ERROR: test (__main__.Test) (i=2)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "......\test.py", line 25, in test
    os.remove(temp_file)
PermissionError: [WinError 32] 程序無法存取檔案,因為檔案正由另一個程序使用。: '......\\test.txt'

======================================================================
ERROR: test (__main__.Test) (i=3)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "......\test.py", line 25, in test
    os.remove(temp_file)
PermissionError: [WinError 32] 程序無法存取檔案,因為檔案正由另一個程序使用。: '......\\test.txt'

----------------------------------------------------------------------
Ran 1 test in 0.010s

FAILED (errors=3)

Environment

  • Python version: 3.8.1
  • Flask version: 1.1.2
  • Werkzeug version: 1.0.1
@davidism
Copy link
Member

Duplicate of pallets/werkzeug#1785

@davidism davidism marked this as a duplicate of pallets/werkzeug#1785 Aug 20, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants