Skip to content

The cache lock cannot be removed on Windows 10 if placed inside the user home directory. #42

Closed
@olegvereshchynsky

Description

@olegvereshchynsky

I'm trying to create a TokenCache on Windows 10 and store a cache at

~/.myapp/token.cache

As a result the lockfile is created at:

~/.myapp/token.cache.lockfile

However this lockfile is not removed in CrossPlatLock.__exit__ and it is completely blocked by the operationg system (no visible process) untill the further reboot. It looks like the file is not yet unlocked when being removed (I may be wrong) creating some kind of a hanging file handle.

Here is a simple way to reproduce:

path = os.path.expanduser('~/.myapp/test.lockfile')
with CrossPlatLock(path):
    print('hi')
# One more time
with CrossPlatLock(path):
    print('hi')
    
<ipython-input-117-002d28c082a3> in <module>                                           
----> 1 with CrossPlatLock(path):                                                      
      2     print('hi')                                                                
      3                                                                                
                                                                                       
<ipython-input-116-89428a9dd969> in __enter__(self)                                    
     11         pid = os.getpid()                                                      
     12                                                                                
---> 13         self._fh = open(self._lockpath, 'wb+', buffering=0)                    
     14         portalocker.lock(self._fh, portalocker.LOCK_EX)                        
     15         self._fh.write('{} {}'.format(pid, sys.argv[0]).encode('utf-8'))       
                                                                                       
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\XXXXYYY/.myapp/test.lockfile'

Explicitly unlocking the file before closing it solves the issue:

portalocker.unlock(self._fh)

I know that portalocker claims that the explicit unlock is not necessary, but would it be possible to add it to prevent the problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions