Skip to content

Commit 8772c4f

Browse files
author
ShresthSamyak
committed
fix(macos): cache CoreFoundation init failure
1 parent 5e271c0 commit 8772c4f

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

mpv.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
_core_foundation = None
3838
_cf_runloop_default_mode = None
39+
_core_foundation_init_failed = False
3940
if os.name == 'nt':
4041
# Note: mpv-2.dll with API version 2 corresponds to mpv v0.35.0. Most things should work with the fallback, too.
4142
names = ['mpv-2.dll', 'libmpv-2.dll', 'mpv-1.dll']
@@ -1056,7 +1057,13 @@ def _wait_for_future(self, result, timeout):
10561057

10571058
global _core_foundation
10581059
global _cf_runloop_default_mode
1060+
global _core_foundation_init_failed
1061+
10591062
start_time = time.monotonic()
1063+
1064+
if _core_foundation_init_failed:
1065+
return result.result(timeout)
1066+
10601067
try:
10611068
if _core_foundation is None or _cf_runloop_default_mode is None:
10621069
core_foundation_path = ctypes.util.find_library('CoreFoundation')
@@ -1087,6 +1094,7 @@ def _wait_for_future(self, result, timeout):
10871094
except ShutdownError:
10881095
raise
10891096
except Exception as e:
1097+
_core_foundation_init_failed = True
10901098
# Fallback if CoreFoundation loading fails: use standard waiting behavior
10911099
warn(f"Failed to load CoreFoundation for mpv macOS event loop: {e}", RuntimeWarning)
10921100
if timeout is not None:

tests/test_mpv.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,8 +1016,10 @@ def test_macos_wait_for_future(self):
10161016
# Reset globals from mpv module to force re-initialization
10171017
orig_cf = mpv._core_foundation
10181018
orig_mode = mpv._cf_runloop_default_mode
1019+
orig_cf_failed = mpv._core_foundation_init_failed
10191020
mpv._core_foundation = None
10201021
mpv._cf_runloop_default_mode = None
1022+
mpv._core_foundation_init_failed = False
10211023

10221024
def resolve_future():
10231025
time.sleep(0.1)
@@ -1059,8 +1061,10 @@ def test_macos_wait_for_future_timeout(self):
10591061

10601062
orig_cf = mpv._core_foundation
10611063
orig_mode = mpv._cf_runloop_default_mode
1064+
orig_cf_failed = mpv._core_foundation_init_failed
10621065
mpv._core_foundation = None
10631066
mpv._cf_runloop_default_mode = None
1067+
mpv._core_foundation_init_failed = False
10641068

10651069
from concurrent.futures import TimeoutError
10661070
try:
@@ -1071,6 +1075,7 @@ def test_macos_wait_for_future_timeout(self):
10711075
finally:
10721076
mpv._core_foundation = orig_cf
10731077
mpv._cf_runloop_default_mode = orig_mode
1078+
mpv._core_foundation_init_failed = orig_cf_failed
10741079

10751080
@mock.patch('sys.platform', 'darwin')
10761081
def test_macos_wait_for_future_fallback(self):
@@ -1091,8 +1096,10 @@ def test_macos_wait_for_future_fallback(self):
10911096

10921097
orig_cf = mpv._core_foundation
10931098
orig_mode = mpv._cf_runloop_default_mode
1099+
orig_cf_failed = mpv._core_foundation_init_failed
10941100
mpv._core_foundation = None
10951101
mpv._cf_runloop_default_mode = None
1102+
mpv._core_foundation_init_failed = False
10961103

10971104
def resolve_future():
10981105
time.sleep(0.1)
@@ -1112,3 +1119,4 @@ def resolve_future():
11121119
thread.join(timeout=1.0)
11131120
mpv._core_foundation = orig_cf
11141121
mpv._cf_runloop_default_mode = orig_mode
1122+
mpv._core_foundation_init_failed = orig_cf_failed

0 commit comments

Comments
 (0)