Skip to content
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

Validate and maybe prune interpreter cache run over run #7225

Merged
merged 5 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
15 changes: 15 additions & 0 deletions src/python/pants/backend/python/interpreter_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def select_interpreter_for_targets(self, targets):
def _interpreter_from_path(self, path, filters=()):
try:
executable = os.readlink(os.path.join(path, 'python'))
if not os.path.exists(executable):
if os.path.dirname(path) == self._cache_dir:
Copy link
Contributor

Choose a reason for hiding this comment

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

This is always True in production use from my reading of call-sites in this file. How about re-structuring this private function to allow killing this misleading conditional. Maybe re-name to _interpreter_from_relpath and do the path = os.path.joint(self._cache_dir, relpath) here allowing unambiguous removal of the conditional.

self._purge_interpreter(path)
return None
except OSError:
return None
interpreter = PythonInterpreter.from_binary(executable, include_site_extras=False)
Expand Down Expand Up @@ -251,3 +255,14 @@ def _resolve_and_link(self, interpreter, requirement, target_link):
_safe_link(target_location, target_link)
logger.debug(' installed {}'.format(target_location))
return Package.from_href(target_location)

def _purge_interpreter(self, interpreter_dir):
try:
logger.info('Detected stale interpreter `{}` in the interpreter cache, purging.'
.format(interpreter_dir))
shutil.rmtree(interpreter_dir, ignore_errors=True)
except Exception as e:
logger.warn(
'Caught exception {!r} during interpreter purge. Please run `./pants clean-all`!'
.format(e)
)
14 changes: 14 additions & 0 deletions src/python/pants/backend/python/tasks/select_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def execute(self):
interpreter_path_file = self._interpreter_path_file(target_set_id)
if not os.path.exists(interpreter_path_file):
self._create_interpreter_path_file(interpreter_path_file, python_tgts)
else:
if self._detect_and_purge_invalid_interpreter(interpreter_path_file):
self._create_interpreter_path_file(interpreter_path_file, python_tgts)

interpreter = self._get_interpreter(interpreter_path_file)
self.context.products.register_data(PythonInterpreter, interpreter)
Expand All @@ -95,6 +98,17 @@ def _create_interpreter_path_file(self, interpreter_path_file, targets):
def _interpreter_path_file(self, target_set_id):
return os.path.join(self.workdir, target_set_id, 'interpreter.info')

def _detect_and_purge_invalid_interpreter(self, interpreter_path_file):
with open(interpreter_path_file, 'r') as infile:
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of re-creating the file reading logic, how about something like:

interpreter = self._get_interpreter(interpreter_path_file)
if not os.path.exists(os.readlink(interpreter.binary)):
  ...

lines = infile.readlines()
binary = lines[0].strip()
if not os.path.exists(binary):
self.context.log.info('Stale interpreter reference detected: {}, removing reference and '
'selecting a new interpreter.'.format(binary))
os.remove(interpreter_path_file)
return True
return False

@staticmethod
def _get_interpreter(interpreter_path_file):
with open(interpreter_path_file, 'r') as infile:
Expand Down