Skip to content

Commit

Permalink
Implement file locking on cache directory
Browse files Browse the repository at this point in the history
This change should prevent concurrency problems related to use of
the cache directory.
  • Loading branch information
ssbarnea committed Aug 8, 2022
1 parent 97cd9ba commit 40e68b6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ repos:
- ansible-compat>=2.2.0
- ansible-core
- enrich
- filelock
- flaky
- pytest
- rich>=11.0.0
Expand All @@ -157,6 +158,7 @@ repos:
- ansible-core
- docutils
- enrich
- filelock
- flaky
- jsonschema>=4.9.0
- pytest
Expand Down
11 changes: 6 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ cffi==1.15.1
charset-normalizer==2.1.0
click==8.1.3
commonmark==0.9.1
coverage==6.4.2
coverage==6.4.3
cryptography==37.0.4
dill==0.3.5.1
docutils==0.16
enrich==1.2.7
execnet==1.9.0
flake8==5.0.2
filelock==3.7.1
flake8==5.0.4
flaky==3.7.0
idna==3.3
imagesize==1.4.1
importlib-metadata==4.12.0
iniconfig==1.1.1
isort==5.10.1
jinja2==3.1.2
jsonschema==4.9.0
jsonschema==4.9.1
lazy-object-proxy==1.7.1
markdown-it-py==2.1.0
markupsafe==2.1.1
Expand All @@ -51,7 +52,7 @@ platformdirs==2.5.2
pluggy==1.0.0
psutil==5.9.1
py==1.11.0
pycodestyle==2.9.0
pycodestyle==2.9.1
pycparser==2.21
pyflakes==2.5.0
pygments==2.12.0
Expand Down Expand Up @@ -84,7 +85,7 @@ sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
subprocess-tee==0.3.5
tomli==2.0.1
tomlkit==0.11.1
tomlkit==0.11.2
typing-extensions==4.3.0
urllib3==1.26.11
wcmatch==8.4
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ install_requires =
ansible-compat>=2.2.0 # GPLv3
ansible-core>=2.12.0 # GPLv3
enrich>=1.2.6
filelock # The Unlicense
jsonschema>=4.9.0 # MIT, version needed for improved errors
packaging
pyyaml
Expand Down
7 changes: 7 additions & 0 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from ansible_compat.config import ansible_version
from ansible_compat.prerun import get_cache_dir
from enrich.console import should_do_markup
from filelock import FileLock

from ansiblelint import cli
from ansiblelint._mockings import _perform_mockings_cleanup
Expand Down Expand Up @@ -104,6 +105,10 @@ def initialize_options(arguments: Optional[List[str]] = None) -> None:
options.configured = True
options.cache_dir = get_cache_dir(options.project_dir)

# add a lock file so we do not have two instances running inside at the same time
options.cache_dir_lock = FileLock(f"{options.cache_dir}/.lock")
options.cache_dir_lock.acquire()


def _do_list(rules: "RulesCollection") -> int:
# On purpose lazy-imports to avoid pre-loading Ansible
Expand Down Expand Up @@ -228,6 +233,8 @@ def main(argv: Optional[List[str]] = None) -> int: # noqa: C901
app.render_matches(result.matches)

_perform_mockings_cleanup()
options.cache_dir_lock.release()
os.unlink(options.cache_dir_lock.lock_file)

return app.report_outcome(result, mark_as_success=mark_as_success)

Expand Down

0 comments on commit 40e68b6

Please sign in to comment.