Skip to content

Fix symlinking race condition #2765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions readthedocs/core/symlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

from readthedocs.builds.models import Version
from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.core.utils import safe_makedirs
from readthedocs.projects import constants
from readthedocs.projects.models import Domain
from readthedocs.projects.utils import run
Expand Down Expand Up @@ -100,19 +101,19 @@ def sanity_check(self):
if os.path.islink(self.project_root) and not self.project.single_version:
self._log("Removing single version symlink")
os.unlink(self.project_root)
os.makedirs(self.project_root)
safe_makedirs(self.project_root)
elif (self.project.single_version and
not os.path.islink(self.project_root) and
os.path.exists(self.project_root)):
shutil.rmtree(self.project_root)
elif not os.path.lexists(self.project_root):
os.makedirs(self.project_root)
safe_makedirs(self.project_root)

# CNAME root directories
if not os.path.lexists(self.CNAME_ROOT):
os.makedirs(self.CNAME_ROOT)
safe_makedirs(self.CNAME_ROOT)
if not os.path.lexists(self.PROJECT_CNAME_ROOT):
os.makedirs(self.PROJECT_CNAME_ROOT)
safe_makedirs(self.PROJECT_CNAME_ROOT)

def run(self):
"""
Expand Down Expand Up @@ -177,7 +178,7 @@ def symlink_subprojects(self):
if rels.count():
# Don't creat the `projects/` directory unless subprojects exist.
if not os.path.exists(self.subproject_root):
os.makedirs(self.subproject_root)
safe_makedirs(self.subproject_root)
for rel in rels:
# A mapping of slugs for the subproject URL to the actual built
# documentation
Expand All @@ -194,7 +195,7 @@ def symlink_subprojects(self):
)
symlink_dir = os.sep.join(symlink.split(os.path.sep)[:-1])
if not os.path.lexists(symlink_dir):
os.makedirs(symlink_dir)
safe_makedirs(symlink_dir)
run('ln -nsf %s %s' % (docs_dir, symlink))

# Remove old symlinks
Expand All @@ -219,7 +220,7 @@ def symlink_translations(self):
if os.path.islink(language_dir):
os.unlink(language_dir)
if not os.path.lexists(language_dir):
os.makedirs(language_dir)
safe_makedirs(language_dir)

for (language, slug) in translations.items():
self._log(u"Symlinking translation: {0}->{1}".format(language, slug))
Expand Down Expand Up @@ -271,7 +272,7 @@ def symlink_versions(self):
version_queryset = self.get_version_queryset()
if version_queryset.count():
if not os.path.exists(version_dir):
os.makedirs(version_dir)
safe_makedirs(version_dir)
for version in version_queryset:
self._log(u"Symlinking Version: %s" % version)
symlink = os.path.join(version_dir, version.slug)
Expand Down
14 changes: 14 additions & 0 deletions readthedocs/core/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import errno
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this was brought in for this PR, it isn't used.

import getpass
import logging
import os
Expand Down Expand Up @@ -153,3 +154,16 @@ def slugify(value, *args, **kwargs):


slugify = allow_lazy(slugify, six.text_type, SafeText)


def safe_makedirs(directory_name):
"""
Makedirs has an issue where it has a race condition around
checking for a directory and then creating it.
This catches the exception in this case.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The failure case isn't clear, perhaps expand on what this is protecting against.

"""

try:
os.makedirs(directory_name)
except OSError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this strictly catching makedirs created dirs that already exist? It seems that OSError would be thrown in a number of cases that we might care about.

pass