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

is_singleton=True with nested lock hangs #331

Closed
yugokato opened this issue May 28, 2024 · 1 comment · Fixed by #334
Closed

is_singleton=True with nested lock hangs #331

yugokato opened this issue May 28, 2024 · 1 comment · Fixed by #334

Comments

@yugokato
Copy link

I noticed the following code hangs. Is this not a supported usage?

from filelock import FileLock

with FileLock('.lock', is_singleton=True):
    with FileLock('.lock', is_singleton=True):
        ...

This works fine:

from filelock import FileLock

lock1 = FileLock('.lock', is_singleton=True)
lock2 = FileLock('.lock', is_singleton=True)
with lock1:
    with lock2:
        ...

Environment:

  • Python 3.11.9
  • filelock==3.14.0
  • platform=MacOS Sonoma 14.5
@yugokato yugokato changed the title is_singleton=True with second lock hangs is_singleton=True with nested lock hangs May 28, 2024
@ethanbb
Copy link
Contributor

ethanbb commented Jun 11, 2024

I think this is happening because the __init__ method of BaseFileLock is always called when a new FileLock is constructed, even if it's just returning an existing instance. This sets self._context to a new object. In particular, in the nested situation, the context whose counter was incremented by the first call to acquire will be discarded and replaced with a new context; thus, it will not know that the lock has already been acquired and will try to open the lock file again, but will be unable to.

My humble suggestion would be to avoid this by only calling the contents of __init__ once when the FileLock is actually constructed. This could be done by renaming it to something like _initialize and calling it within __new__ after each call to super().__new__.

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 a pull request may close this issue.

2 participants