]> jfr.im git - yt-dlp.git/commitdiff
[utils] Fix file locking for AOSP (#2714)
authorJustin Keogh <redacted>
Thu, 3 Mar 2022 13:09:00 +0000 (13:09 +0000)
committerGitHub <redacted>
Thu, 3 Mar 2022 13:09:00 +0000 (05:09 -0800)
Closes #2080, #2670

Authored by: jakeogh

yt_dlp/utils.py

index 1532210f39847499d57059d4fe322b4818d096f6..8e9a7dbc8c9da88531ee2853e33057ce6810dca8 100644 (file)
@@ -2141,18 +2141,28 @@ def _unlock_file(f):
             raise OSError('Unlocking file failed: %r' % ctypes.FormatError())
 
 else:
-    # Some platforms, such as Jython, is missing fcntl
     try:
         import fcntl
 
         def _lock_file(f, exclusive, block):
-            fcntl.flock(f,
-                        fcntl.LOCK_SH if not exclusive
-                        else fcntl.LOCK_EX if block
-                        else fcntl.LOCK_EX | fcntl.LOCK_NB)
+            try:
+                fcntl.flock(f,
+                            fcntl.LOCK_SH if not exclusive
+                            else fcntl.LOCK_EX if block
+                            else fcntl.LOCK_EX | fcntl.LOCK_NB)
+            except BlockingIOError:
+                raise
+            except OSError:  # AOSP does not have flock()
+                fcntl.lockf(f,
+                            fcntl.LOCK_SH if not exclusive
+                            else fcntl.LOCK_EX if block
+                            else fcntl.LOCK_EX | fcntl.LOCK_NB)
 
         def _unlock_file(f):
-            fcntl.flock(f, fcntl.LOCK_UN)
+            try:
+                fcntl.flock(f, fcntl.LOCK_UN)
+            except OSError:
+                fcntl.lockf(f, fcntl.LOCK_UN)
 
     except ImportError:
         UNSUPPORTED_MSG = 'file locking is not supported on this platform'