{"record":{"id":"033e657c5741995f","repo":"yt-dlp/yt-dlp","slug":"unlocking-file-failed-ctypes-formaterror-r","errorCode":null,"errorMessage":"Unlocking file failed: {ctypes.FormatError()!r}","messagePattern":"Unlocking file failed: (.+?)","errorType":"exception","errorClass":"OSError","httpStatus":null,"severity":"error","filePath":"yt_dlp/utils/_utils.py","lineNumber":1588,"sourceCode":"\n    def _lock_file(f, exclusive, block):\n        overlapped = OVERLAPPED()\n        overlapped.Offset = 0\n        overlapped.OffsetHigh = 0\n        overlapped.hEvent = 0\n        f._lock_file_overlapped_p = ctypes.pointer(overlapped)\n\n        if not LockFileEx(msvcrt.get_osfhandle(f.fileno()),\n                          (0x2 if exclusive else 0x0) | (0x0 if block else 0x1),\n                          0, whole_low, whole_high, f._lock_file_overlapped_p):\n            # NB: No argument form of \"ctypes.FormatError\" does not work on PyPy\n            raise BlockingIOError(f'Locking file failed: {ctypes.FormatError(ctypes.GetLastError())!r}')\n\n    def _unlock_file(f):\n        assert f._lock_file_overlapped_p\n        handle = msvcrt.get_osfhandle(f.fileno())\n        if not UnlockFileEx(handle, 0, whole_low, whole_high, f._lock_file_overlapped_p):\n            raise OSError(f'Unlocking file failed: {ctypes.FormatError()!r}')\n\nelse:\n    try:\n        import fcntl\n\n        def _lock_file(f, exclusive, block):\n            flags = fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH\n            if not block:\n                flags |= fcntl.LOCK_NB\n            try:\n                fcntl.flock(f, flags)\n            except BlockingIOError:\n                raise\n            except OSError:  # AOSP does not have flock()\n                fcntl.lockf(f, flags)\n\n        def _unlock_file(f):\n            with contextlib.suppress(OSError):","sourceCodeStart":1570,"sourceCodeEnd":1606,"githubUrl":"https://github.com/yt-dlp/yt-dlp/blob/81ecd58b1394793e6da9998cc19fdb45657f1685/yt_dlp/utils/_utils.py#L1570-L1606","documentation":"Error \"Unlocking file failed: {ctypes.FormatError()!r}\" thrown in yt-dlp/yt-dlp.","triggerScenarios":"Thrown at yt_dlp/utils/_utils.py:1588 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["The file handle was already closed or the lock region was invalid; this usually indicates a prior error, so check earlier log output.","Retry the operation; if it persists, ensure no external tool modified or locked the file concurrently."],"exampleFix":"Check the return value of UnlockFileEx: if not ctypes.windll.kernel32.UnlockFileEx(...): raise OSError(f'Unlocking file failed: {ctypes.FormatError()!r}')","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"81ecd58b1394793e6da9998cc19fdb45657f1685","analyzedAt":"2026-08-22T12:21:25.439Z","schemaVersion":2},"datasetVersion":"2026-08-22T14:17:55.899Z"}