Repository: Pyomo/pyomo

Issue:
pyomo.common.tempfiles.TempfileManager raises FileNotFoundError
I've recently started using pyomo.common.tempfiles.TempfileManager to set the temporary directory in a package, as in https://pyomo.readthedocs.io/en/stable/working_models.html#changing-the-temporary-directory. I was previously using TempfileManager from PyUtilib for about 4 years. 

My tests now fail. Here is some of the trace:

  File "/home/user/anaconda38/lib/python3.8/site-packages/pyomo/opt/base/solvers.py", line 571, in solve
    self._presolve(*args, **kwds)
  File "/home/user/anaconda38/lib/python3.8/site-packages/pyomo/solvers/plugins/solvers/CPLEX.py", line 349, in _presolve
    ILMLicensedSystemCallSolver._presolve(self, *args, **kwds)
  File "/home/user/anaconda38/lib/python3.8/site-packages/pyomo/opt/solver/shellcmd.py", line 197, in _presolve
    OptSolver._presolve(self, *args, **kwds)
  File "/home/user/anaconda38/lib/python3.8/site-packages/pyomo/opt/base/solvers.py", line 668, in _presolve
    self._convert_problem(args,
  File "/home/user/anaconda38/lib/python3.8/site-packages/pyomo/opt/base/solvers.py", line 738, in _convert_problem
    return convert_problem(args,
  File "/home/user/anaconda38/lib/python3.8/site-packages/pyomo/opt/base/convert.py", line 105, in convert_problem
    problem_files, symbol_map = converter.apply(*tmp, **tmpkw)
  File "/home/user/anaconda38/lib/python3.8/site-packages/pyomo/solvers/plugins/converter/model.py", line 72, in apply
    problem_filename = TempfileManager.\
  File "/home/user/anaconda38/lib/python3.8/site-packages/pyomo/common/tempfiles.py", line 67, in create_tempfile
    ans = tempfile.mkstemp(suffix=suffix, prefix=prefix, text=text, dir=dir)
  File "/home/user/anaconda38/lib/python3.8/tempfile.py", line 332, in mkstemp
    return _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  File "/home/user/anaconda38/lib/python3.8/tempfile.py", line 250, in _mkstemp_inner
    fd = _os.open(file, flags, 0o600)
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpc6s9g6hf/tmpjoxugx27.pyomo.lp'

If I run the tests individually on the commandline, then the FileNotFoundError does not occur.

If I run all the tests from a script, then the FileNotFoundError does occur.

If I run all the tests from the same script, but change the order of the tests, then the FileNotFoundError still occurs but during a different test.

Note that in all tests, I'm not acutally setting TempfileManager.tempdir. It appears in a method, but this method is not called during these tests. So just the import "from pyomo.common.tempfiles import TempfileManager" is being run.

Now if I change my code so that "TempfileManager.tempdir = None" is always called for each test, then the FileNotFoundError no longer occurs.

Can you help?

I'm using Python 3.8.5 from Anaconda, Pyomo 5.7.2 and Ubuntu 18.04 LTS.

Thanks,
Jason


Discussion:
Thanks for reporting this. Is there any chance you could either paste your script or point me to where it's located so I can try to reproduce the error?

Resolving patch:
diff --git a/pyomo/common/tempfiles.py b/pyomo/common/tempfiles.py
--- a/pyomo/common/tempfiles.py
+++ b/pyomo/common/tempfiles.py
@@ -34,9 +34,8 @@
 class TempfileManagerClass:
     """A class that manages temporary files."""
 
-    tempdir = None
-
     def __init__(self, **kwds):
+        self.tempdir = None
         self._tempfiles = [[]]
         self._ctr = -1
 
@@ -189,4 +188,5 @@ def pop(self, remove=True):
         if len(self._tempfiles) == 0:
             self._tempfiles = [[]]
 
+
 TempfileManager = TempfileManagerClass()


Test patch: