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

Require explicit name for BoundedSemaphore()? #87

Closed
dhoulder opened this issue Sep 12, 2023 · 9 comments
Closed

Require explicit name for BoundedSemaphore()? #87

dhoulder opened this issue Sep 12, 2023 · 9 comments

Comments

@dhoulder
Copy link

Say there are two unrelated applications on the same host, and they both use the default name for BoundedSemaphore().

For example:

semaphore = portalocker.BoundedSemaphore(n)

Won't they both try and use the same set of lock files and interfere with each other?

To ensure backwards compatibility, you could create a new class NamedBoundedSemaphore with a required name argument and deprecate BoundedSemaphore

@wolph
Copy link
Owner

wolph commented Sep 16, 2023

You are largely correct but you've glossed over a small detail, the directory is randomized by the tempfile.gettempdir() function:

def __init__(
self,
maximum: int,
name: str = 'bounded_semaphore',
filename_pattern: str = '{name}.{number:02d}.lock',
directory: str = tempfile.gettempdir(),
timeout: typing.Optional[float] = DEFAULT_TIMEOUT,
check_interval: typing.Optional[float] = DEFAULT_CHECK_INTERVAL,
fail_when_locked: typing.Optional[bool] = True,
):
self.maximum = maximum
self.name = name
self.filename_pattern = filename_pattern
self.directory = directory
self.lock: typing.Optional[Lock] = None
super().__init__(
timeout=timeout,
check_interval=check_interval,
fail_when_locked=fail_when_locked,
)

In practice you will never have 2 unrelated applications colliding :)

@dhoulder
Copy link
Author

tempfile.gettempdir() always returns /tmp for me (Ubuntu, not setting TMPDIR, TEMP or TMP). See https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir

>>> import tempfile
>>> tempfile.gettempdir()
'/tmp'

@wolph
Copy link
Owner

wolph commented Sep 16, 2023

Oops... in that case I have made a wrong assumption a long time ago and created a semaphore that could cause issues in some cases.

I'm in the process of creating a new release anyhow so I'll make sure to add your suggested NamedBoundedSemaphore class

@dhoulder
Copy link
Author

No worries. Happens to the best of us 😄
Thanks for all your hard work.

@wolph
Copy link
Owner

wolph commented Sep 16, 2023

As a backstory, I'm guessing this made me think it was random. On my laptop I get this:

>>> tempfile.gettempdir()
'/var/folders/y7/n86bj8vn48vftzwyzdwdpvtc0000gn/T'

Seemed pretty random to me ;)

@dhoulder
Copy link
Author

Hmm… Is that inside some kind of container or WSL on Windows or something?

@wolph
Copy link
Owner

wolph commented Sep 16, 2023

No... that's on OS X

@dhoulder
Copy link
Author

OK, so that seems to be a per-user $TMPDIR. I think my original observation mostly still holds: Two unrelated applications running as the same user on the same host could interfere with each other.

@wolph
Copy link
Owner

wolph commented Sep 16, 2023

Your observation is correct indeed. If I start 2 completely unrelated applications I get the same results.

Furthermore:

$ echo $TMPDIR
/var/folders/y7/n86bj8vn48vftzwyzdwdpvtc0000gn/T/

I really should have read the Python manual, it's pretty clear about this :X

@wolph wolph closed this as completed in 1021d1f Sep 16, 2023
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

No branches or pull requests

2 participants