Skip to content

cache_dir_lock in main is always None, so the lockfile never gets cleaned up #4030

@guppy0130

Description

@guppy0130
Summary

cache_dir_lock is initialized as None here, in the global scope I believe:

cache_dir_lock: None | FileLock = None

In initialize_options, a new, frame-only cache_dir_lock is created here:

cache_dir_lock = FileLock( # pylint: disable=redefined-outer-name
f"{options.cache_dir}/.lock",
)
try:
cache_dir_lock.acquire(timeout=180)

I think pylint was correct to indicate that the global name was redefined. This variable isn't marked as global and so the cache_dir_lock in initialize_options is local to initialize_options only.

The value of cache_dir_lock (the global one) is still None later on, so this conditional is always falsy and the lockfile therefore never gets cleaned up:

if cache_dir_lock:
cache_dir_lock.release()
pathlib.Path(cache_dir_lock.lock_file).unlink(missing_ok=True)

I believe this behavior is introduced by c6bd9a7.

I'd love to PR to resolve this, but I don't know if you prefer the global approach or just returning the lock from initialize_options.

Issue Type
  • Bug Report
OS / ENVIRONMENT
$ ansible-lint --version
ansible-lint 24.2.0 using ansible-core:2.16.3 ansible-compat:4.1.11 ruamel-yaml:0.18.6 ruamel-yaml-clib:0.2.8
  • ansible installation method: pip
  • ansible-lint installation method: pip
STEPS TO REPRODUCE

In one session

$ rm -r ~/.cache/ansible-compat/*
$ watch -n 1 'ls ~/.cache/ansible-compat/**/.lock'  # the quotes are so the shell under watch will expand it, not your current shell

and in another session

$ pushd /tmp
$ mkdir blah
$ vim main.yml  # see below for MCVE playbook, but it can really be any playbook
$ ansible-lint

watch the lockfile get created when ansible-lint is run, and then it shouldn't get cleaned up.

Desired Behavior

The cache_dir_lock that main uses is not None, either by the global keyword or initialize_options returning it to its caller, main; in which case, the lock can be correctly cleaned up after ansible-lint finishes.

Actual Behavior

Ansible-lint doesn't clean up the lockfile, because cache_dir_lock is None in main.

Please give some details of what is happening. Include a minimum complete
verifiable example
with:

  • minimized playbook to reproduce the error
- name: "MCVE"
  gather_facts: false
  hosts: "localhost"
  tasks:
    - name: "Hello world"
      ansible.builtin.debug:
        msg: "hello world"
  • the output of running ansible-lint including the command line used
Passed: 0 failure(s), 0 warning(s) on 1 files. Last profile that met the validation criteria was 'production'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions