#262: Files remain open when using tempfile
------------------------------+---------------------------------------------
 Reporter:  bthiell           |       Owner:     
     Type:  defect            |      Status:  new
 Priority:  major             |   Milestone:     
Component:  *general*         |     Version:     
 Keywords:  tempfile mkstemp  |  
------------------------------+---------------------------------------------
 I found the following piece of code in several places in Invenio:
 {{{
 import tempfile
 file_descriptor, path = tempfile.mkstemp(...)
 f = open(path, 'w')
 # do something
 f.close()
 }}}

 This is a serious problem because one thinks that the file has been
 correctly closed. For processes opening numerous files (such as bibindex
 e.g.), it is a blocking problem because after some time the process won't
 be able to open any more files.

 The documentation for `tempfile.mkstemp`
 ([http://docs.python.org/library/tempfile.html 1]) says:
 > mkstemp() returns a tuple containing an OS-level handle to an open file
 (as would be returned by os.open()) and the absolute pathname of that
 file, in that order.

 The cleanest way to solve this problem is then to use low-level IO
 functions provided by the module `os` (such as `os.read(fd)` or
 `os.write(fd, 'text')`) and then close the file using `os.close(fd)`.

 I will fix bibdocfile as part of #252 but this problem might concern the
 following files:
 * modules/bibclassify/lib/bibclassify_text_extractor.py
 * modules/bibexport/lib/bibexport_method_fieldexporter_dblayer.py
 * modules/bibformat/lib/bibformat_engine.py
 * modules/bibharvest/lib/oai_harvest_admin.py
 * modules/bibharvest/lib/oai_harvest_daemon.py
 * modules/bibharvest/lib/oai_repository_updater.py
 * modules/elmsubmit/lib/elmsubmit_misc.py
 * modules/miscutil/lib/invenio_connector.py
 * modules/miscutil/lib/shellutils.py
 * modules/webstyle/lib/webinterface_handler_wsgi_utils.py
 * modules/websubmit/lib/bibdocfilecli.py
 * modules/websubmit/lib/websubmit_file_converter.py
 * modules/websubmit/lib/wsm_pdftk_plugin.py
 * modules/websubmit/lib/wsm_pyexiv2_plugin.py

 Note that I did not check if the problem is present in these files (these
 are the files using `tempfile.mkstemp`), but it is likely that it is in
 there.

-- 
Ticket URL: <http://invenio-software.org/ticket/262>
Invenio <http://invenio-software.org>

Reply via email to