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

Windows shared lock with Python 3 #64

Closed
barry-gallagher opened this issue May 18, 2021 · 11 comments
Closed

Windows shared lock with Python 3 #64

barry-gallagher opened this issue May 18, 2021 · 11 comments

Comments

@barry-gallagher
Copy link

In portalocker.py for SHARED locks the code has a sys.major version conditional that uses msvcrt flags on Python 3. The problem is that win32file.LockFileEx is being used so the flags don't match, and a non-blocking flag will block instead. Just remove the "if sys.version" and always use the win32con flags since msvcrt isn't used for SHARED.

@wolph
Copy link
Owner

wolph commented Sep 14, 2021

Sorry for the really slow response, I didn't get an email about this issue.

I'm honestly a bit hesitant to change this behaviour at this point for 2 reasons:

  1. I can't easily test the possible scenarios
  2. It breaks backwards compatibility and some people might expect these results.

It does sound like a serious bug though... but I'm wondering if the better fix isn't to replace the win32file locking with msvcrt instead and remove win32file completely.

@barry-gallagher
Copy link
Author

barry-gallagher commented Sep 17, 2021 via email

@martin-eder-zeiss
Copy link

@wolph

Are there any updates about this? The shared lock feature is completely broken for me right now.

@wolph
Copy link
Owner

wolph commented Jun 30, 2022

I've done some testing in the mean time and I believe the proposed solution is probably the best one. Even though it might break backwards compatibility in a few select cases.

I'll create a new release within the next few days

@wolph wolph closed this as completed in 89e2e32 Jul 4, 2022
@wolph
Copy link
Owner

wolph commented Jul 5, 2022

@martin-eder-zeiss Can you test the current develop branch? I think I've fixed it but I'm somewhat worried I might have broken something...

It seems to work flawlessly for me on a Windows machine though, but I might still be missing other test cases

@martin-eder-zeiss
Copy link

martin-eder-zeiss commented Jul 5, 2022

I am not familiar with Windows file locking mechanisms, but is it intended that this does not throw an AlreadyLocked exception?

portalocker.lock(file, portalocker.constants.LOCK_EX)
portalocker.lock(file, portalocker.constants.LOCK_SH | portalocker.constants.LOCK_NB)

How can I get a shared lock when there is already an exclusive one?

@wolph
Copy link
Owner

wolph commented Jul 5, 2022

I tested it like this, and that seems to work as expected:

import portalocker


with portalocker.Lock('x', flags=portalocker.LOCK_EX):
    print('lock A')
    with portalocker.Lock('x', flags=portalocker.LOCK_SH | portalocker.LOCK_NB):
        print('lock B')

And I tried this as well:

import portalocker

with open('x', 'w') as a:
    portalocker.lock(a, portalocker.LOCK_EX)
    print('lock A')

    with open('x', 'w') as b:
        portalocker.lock(b, portalocker.LOCK_SH | portalocker.LOCK_NB)
        print('lock B')

Could it be that you reused the same file handle for both? So something like this:

import portalocker

with open('x', 'w') as a:
    portalocker.lock(a, portalocker.LOCK_EX)
    print('lock A')

    portalocker.lock(a, portalocker.LOCK_SH | portalocker.LOCK_NB)
    print('lock B')

@martin-eder-zeiss
Copy link

yep, I reused the file handle. Nevermind, everything seems to work now.

@egorvdot
Copy link

egorvdot commented Aug 1, 2022

Hello @wolph. When the fix will be released?

@wolph
Copy link
Owner

wolph commented Aug 1, 2022

Hello @wolph. When the fix will be released?

I've released version 2.5.0 about 3 weeks ago. Is it still broken for you?

@egorvdot
Copy link

egorvdot commented Aug 1, 2022

Hello @wolph. When the fix will be released?

I've released version 2.5.0 about 3 weeks ago. Is it still broken for you?

Sorry, I got confused in my venv-s. I see updates in the latest release. Thanks!

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

4 participants