Skip to content

Avoid cachedir races - #3353

Merged
bdbaddog merged 4 commits into
SCons:masterfrom
mwichmann:cacheDir
Apr 28, 2019
Merged

bdbaddog merged 4 commits into
SCons:masterfrom
mwichmann:cacheDir

Conversation

@mwichmann

Copy link
Copy Markdown
Collaborator
  1. Add a Py3-only version of the cachedir config read, using exclusive open to avoid races.
  2. In the Py2-only version, add the hack from issue Scons crash when using multiple jobs #3351 to dodge one source of races.

This is an initial try to experiment with.

Signed-off-by: Mats Wichmann [email protected]

Contributor Checklist:

  • I have created a new test or updated the unit tests to cover the new/changed functionality.
  • I have updated master/src/CHANGES.txt directory (and read the README.txt in that directory)
  • I have updated the appropriate documentation

@mwichmann mwichmann changed the title Avoid cachedir races [WIP] Avoid cachedir races Apr 19, 2019
mwichmann added a commit to mwichmann/scons that referenced this pull request Apr 19, 2019
Comment thread src/engine/SCons/CacheDir.py Outdated
# produce a warning. If the directory doesn't exist or is empty,
# write a config file.

from SCons.Util import PY3

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Promote to top of file?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Can do. Or could turn into a slightly wonky feature test instead, based on:

Python 2.7.15 (default, Oct 15 2018, 15:26:09) 
[GCC 8.2.1 20180801 (Red Hat 8.2.1-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.makedirs("foo", exist_ok=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: makedirs() got an unexpected keyword argument 'exist_ok'
>>> 

Comment thread src/engine/SCons/CacheDir.py Outdated
# directory)
if os.path.isdir(path) and len(os.listdir(path)) != 0:
#if os.path.isdir(path) and len(os.listdir(path)) != 0:
if os.path.isdir(path) and len([f for f in os.listdir(path) if os.path.basename(f) != "config"]) != 0:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you get rid of above line, and maybe use Any(os.listdir(path)) ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sure, I can look at that. I just clipped this one from the issue (#3351) so some improvement could happen in the py2.7 case as well.

1. Add a Py3-only version of the cachedir config read,
using exclusive open to avoid races.
2. In the Py2-only version, add the hack from issue SCons#3351
to dodge one source of races.

Signed-off-by: Mats Wichmann <[email protected]>
Simplify the Py2 check for an existing-but-old cachedir
by using any and a generator expression.

Move an import to module scope.

Signed-off-by: Mats Wichmann <[email protected]>
sider complained about spelling in a comment after I
updated the comment so it got to look at those lines
as part of the change.  Fix those.

Signed-off-by: Mats Wichmann <[email protected]>
@bdbaddog
bdbaddog merged commit c3b0bb3 into SCons:master Apr 28, 2019
@mwichmann
mwichmann deleted the cacheDir branch April 29, 2019 13:25
@mwichmann mwichmann changed the title [WIP] Avoid cachedir races Avoid cachedir races Dec 16, 2019
@mtvjr

mtvjr commented Mar 7, 2024

Copy link
Copy Markdown

Looks like this might not completely make things thread safe. We just ran into an issue where it appears the cache file is attempted to be read between file creation and the json.dump call finishing. The json.load call failed due to an empty input file.

13:57:22  Traceback (most recent call last):
13:57:22    File "/usr/lib/python3.6/site-packages/SCons/CacheDir.py", line 195, in _readconfig
13:57:22      self.config = json.load(config)
13:57:22    File "/usr/lib64/python3.6/json/__init__.py", line 299, in load
13:57:22      parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
13:57:22    File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads
13:57:22      return _default_decoder.decode(s)
13:57:22    File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode
13:57:22      obj, end = self.raw_decode(s, idx=_w(s, 0).end())
13:57:22    File "/usr/lib64/python3.6/json/decoder.py", line 357, in raw_decode
13:57:22      raise JSONDecodeError("Expecting value", s, err.value) from None
13:57:22  json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
13:57:22  
13:57:22  During handling of the above exception, another exception occurred:
13:57:22  
13:57:22  Traceback (most recent call last):
13:57:22    File "/usr/lib/python3.6/site-packages/SCons/Taskmaster.py", line 230, in execute
13:57:22      if not t.retrieve_from_cache():
13:57:22    File "/usr/lib/python3.6/site-packages/SCons/Node/FS.py", line 2991, in retrieve_from_cache
13:57:22      return self.get_build_env().get_CacheDir().retrieve(self)
13:57:22    File "/usr/lib/python3.6/site-packages/SCons/Environment.py", line 1033, in get_CacheDir
13:57:22      cd = SCons.CacheDir.CacheDir(path)
13:57:22    File "/usr/lib/python3.6/site-packages/SCons/CacheDir.py", line 159, in __init__
13:57:22      self._readconfig(path)
13:57:22    File "/usr/lib/python3.6/site-packages/SCons/CacheDir.py", line 198, in _readconfig
13:57:22      raise SCons.Errors.SConsEnvironmentError(msg)
13:57:22  SCons.Errors.SConsEnvironmentError: Failed to read cache configuration for /var/lib/scons-ext/cache

@mwichmann

Copy link
Copy Markdown
Collaborator Author

Thanks for the report! Can you dump this into a new issue? The efforts to eliminate races have been around the cache entries themselves, not around the cache config file - since that is created rarely (only "first-time"), it probably gets into trouble less often. We have recently added some locking around the msvc-config cache, maybe the same would be applicable here.

@mwichmann

Copy link
Copy Markdown
Collaborator Author

Issue #4489 opened, let's continue there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants