Merge "linkat: Raise error if we run out of retries"

This commit is contained in:
Zuul 2025-03-07 22:21:36 +00:00 committed by Gerrit Code Review
commit 5074410a9d
2 changed files with 14 additions and 1 deletions

View File

@ -887,12 +887,16 @@ def link_fd_to_path(fd, target_path, dirs_created=0, retries=2, fsync=True):
the newly created directories.
"""
dirpath = os.path.dirname(target_path)
for _junk in range(0, retries):
attempts = 0
while True:
attempts += 1
try:
linkat(linkat.AT_FDCWD, "/proc/self/fd/%d" % (fd),
linkat.AT_FDCWD, target_path, linkat.AT_SYMLINK_FOLLOW)
break
except IOError as err:
if attempts > retries:
raise
if err.errno == errno.ENOENT:
dirs_created = makedirs_count(dirpath)
elif err.errno == errno.EEXIST:

View File

@ -2346,6 +2346,15 @@ cluster_dfw1 = http://dfw1.host/v1/
self.fail("Expecting IOError exception")
self.assertTrue(_m_linkat.called)
def test_link_fd_to_path_runs_out_of_retries(self):
_m_linkat = mock.Mock(
side_effect=IOError(errno.ENOENT, os.strerror(errno.ENOENT)))
with mock.patch('swift.common.utils.linkat', _m_linkat), \
self.assertRaises(IOError) as caught:
utils.link_fd_to_path(0, '/path', 1)
self.assertEqual(caught.exception.errno, errno.ENOENT)
self.assertEqual(3, len(_m_linkat.mock_calls))
@requires_o_tmpfile_support_in_tmp
@with_tempdir
def test_linkat_race_dir_not_exists(self, tempdir):