Skip to content

Commit a6fc7d1

Browse files
committed
Improve SuperPMI script robustness
1. Be more robust to temp directory removal failure If we fail to remove a temporary directory, don't crash. Log the failure and the set of directories and files still remaining, and continue. We have seen this failure in at least one Linux x64 PMI coreclr_tests SuperPMI collection: ``` [Errno 39] Directory not empty: '/datadisks/disk1/work/B18E0979/t/tmpov3b4_qa' ``` 2. Limit the length of file names created by `make_safe_filename`. We were creating file names out of full path names, where those paths contained long temporary directory paths, and thus we were exceeding the maximum allowed file name component.
1 parent d1f4d23 commit a6fc7d1

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/coreclr/scripts/jitutil.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,20 @@ def __enter__(self):
5050
def __exit__(self, exc_type, exc_val, exc_tb):
5151
os.chdir(self.cwd)
5252
if not self._skip_cleanup:
53-
shutil.rmtree(self.mydir)
53+
try:
54+
shutil.rmtree(self.mydir)
55+
except Exception as ex:
56+
logging.warning("Warning: failed to remove directory \"%s\": %s", self.mydir, ex)
57+
# Print out all the remaining files and directories, in case that provides useful information
58+
# for diagnosing the failure. If there is an exception doing this, ignore it.
59+
try:
60+
for dirpath, dirnames, filenames in os.walk(self.mydir):
61+
for dir_name in dirnames:
62+
logging.warning(" Remaining directory: \"%s\"", os.path.join(dirpath, dir_name))
63+
for file_name in filenames:
64+
logging.warning(" Remaining file: \"%s\"", os.path.join(dirpath, file_name))
65+
except Exception:
66+
pass
5467

5568
class ChangeDir:
5669
""" Class to temporarily change to a given directory. Use with "with".
@@ -255,6 +268,8 @@ def is_nonzero_length_file(fpath):
255268

256269
def make_safe_filename(s):
257270
""" Turn a string into a string usable as a single file name component; replace illegal characters with underscores.
271+
Also, limit the length of the file name to avoid creating illegally long file names. This is done by taking a
272+
suffix of the name no longer than the maximum allowed file name length.
258273
259274
Args:
260275
s (str) : string to convert to a file name
@@ -267,7 +282,12 @@ def safe_char(c):
267282
return c
268283
else:
269284
return "_"
270-
return "".join(safe_char(c) for c in s)
285+
# Typically, a max filename length is 256, but let's limit it far below that, because callers typically add additional
286+
# strings to this.
287+
max_allowed_file_name_length = 150
288+
s = "".join(safe_char(c) for c in s)
289+
s = s[-max_allowed_file_name_length:]
290+
return s
271291

272292

273293
def find_in_path(name, pathlist, match_func=os.path.isfile):

0 commit comments

Comments
 (0)