Bug #77160 file is not closed and warning is generated
Submitted: 26 May 2015 14:27 Modified: 17 Jul 2015 8:31
Reporter: Miroslav Balaz Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / Python Severity:S3 (Non-critical)
Version: OS:Any
Assigned to: CPU Architecture:Any

[26 May 2015 14:27] Miroslav Balaz
Description:
When using LOAD DATA LOCAL INFILE, a file is opened for reading, but is not closed, and warning like this is displayed:"...Python3\lib\site-packages\mysql\connector\connection.py:598: ResourceWarning: unclosed file <_io.BufferedReader name=..."

How to repeat:
it can be even seen in source.

Suggested fix:
--- a/lib/mysql/connector/connection.py
+++ b/lib/mysql/connector/connection.py
@@ -366,9 +366,11 @@ class MySQLConnection(MySQLConnectionAbstract):
                     "MySQL Connection not available.")
             raise errors.InterfaceError(
                 "File '{0}' could not be read".format(filename))
-
-        return self._handle_ok(self._send_data(data_file,
+        try:
+            return self._handle_ok(self._send_data(data_file,
                                                send_empty_packet=True))
+        finally:
+            data_file.close()

     def _handle_result(self, packet):
         """Handle a MySQL Result
[17 Jul 2015 8:31] Andrii Nikitin
It doesn't look as actual problem because of parameter  closefd , which is Set to True by default : 

https://docs.python.org/3.3/library/functions.html#open

So the file will be closed eventually by GC

At the same time I Verify it to suppress warning and make sure that files are closed explicitly. According to:

https://bugs.python.org/issue10093
and
https://docs.python.org/3/whatsnew/3.2.html

"the delay in deallocating the object could produce various issues".

E.g. on some filesystems another process may meet issues deleting file immediately after executing LOAD DATA if python still has it open (before GC has chance to release it)