For long (multi-day) training runs, temporary files that got created at the

start of the run might have gotten cleaned up by the OS before the process
finishes. So don't assume they're still around, in the atexit handlers.

PiperOrigin-RevId: 306364079
Change-Id: Ic456f05027d45d6b7699de2e1028da1e31dc0f8a
This commit is contained in:
A. Unique TensorFlower 2020-04-13 20:13:39 -07:00 committed by TensorFlower Gardener
parent d6c51d18b7
commit 23d51b67d0
2 changed files with 22 additions and 1 deletions

View File

@ -23,6 +23,7 @@ from __future__ import division
from __future__ import print_function
import atexit
import errno
import importlib
import os
import sys
@ -33,6 +34,19 @@ from tensorflow.python.autograph.pyct import parser
from tensorflow.python.autograph.utils import compat_util
def _remove_file(file_name):
"""Remove a file, if it exists."""
try:
os.remove(file_name)
except OSError as e:
if e.errno == errno.ENOENT:
# The file disappeared. Ignore this. Temporary files might get
# cleaned up, especially if they reside in /tmp.
pass
else:
raise
def load_source(source, delete_on_exit):
"""Loads the given source code as a Python module."""
# TODO(mdan): Drop the linter verride once the CI stops running Py2.
@ -43,7 +57,7 @@ def load_source(source, delete_on_exit):
f.write(source)
if delete_on_exit:
atexit.register(lambda: os.remove(file_name))
atexit.register(lambda: _remove_file(file_name))
spec = importlib.util.spec_from_file_location(module_name, file_name)
module = importlib.util.module_from_spec(spec)

View File

@ -19,6 +19,7 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import textwrap
import gast
@ -105,6 +106,12 @@ class LoaderTest(test.TestCase):
self.assertEqual(
module.f.__doc__, '日本語 Δθₜ ← Δθₜ₋₁ + ∇Q(sₜ, aₜ)(rₜ + γₜ₊₁ max Q(⋅))')
def test_cleanup(self):
test_source = textwrap.dedent('')
_, filename = loader.load_source(test_source, delete_on_exit=True)
# Clean up the file before loader.py tries to remove it, to check that the
# latter can deal with that situation.
os.unlink(filename)
if __name__ == '__main__':
test.main()