This commit is contained in:
2026-03-21 23:49:32 -07:00
parent f8c98b39bd
commit 3e922f7b3a
3 changed files with 65 additions and 29 deletions

View File

@@ -20,7 +20,7 @@ This helper is intentionally minimal: one request at a time, last-write-wins.
from __future__ import annotations
MEDEIA_MPV_HELPER_VERSION = "2026-03-22.4"
MEDEIA_MPV_HELPER_VERSION = "2026-03-22.5"
import argparse
import json
@@ -1322,36 +1322,72 @@ def main(argv: Optional[list[str]] = None) -> int:
# path used by this helper (which comes from the running MPV instance).
os.environ["MEDEIA_MPV_IPC"] = str(args.ipc)
if platform.system() == "Windows":
try:
sibling_pids = [
pid
for pid in _windows_list_pipeline_helper_pids(str(args.ipc))
if pid and pid != os.getpid()
]
if sibling_pids:
_append_helper_log(
f"[helper] terminating older helper pids for ipc={args.ipc}: {','.join(str(pid) for pid in sibling_pids)}"
)
_windows_kill_pids(sibling_pids)
time.sleep(0.25)
except Exception as exc:
_append_helper_log(
f"[helper] failed to terminate older helpers: {type(exc).__name__}: {exc}"
)
lock_wait_deadline = time.time() + min(max(1.5, float(args.timeout or 0.0)), 8.0)
lock_wait_logged = False
last_killed_pid_signature = ""
_lock_fh = None
# Ensure single helper instance per ipc.
_lock_fh = _acquire_ipc_lock(str(args.ipc))
if _lock_fh is None:
_append_helper_log(
f"[helper] another instance already holds lock for ipc={args.ipc}; exiting"
)
return 0
while _lock_fh is None:
if platform.system() == "Windows":
try:
sibling_pids = [
pid
for pid in _windows_list_pipeline_helper_pids(str(args.ipc))
if pid and pid != os.getpid()
]
if sibling_pids:
signature = ",".join(str(pid) for pid in sibling_pids)
if signature != last_killed_pid_signature:
_append_helper_log(
f"[helper] terminating older helper pids for ipc={args.ipc}: {signature}"
)
last_killed_pid_signature = signature
_windows_kill_pids(sibling_pids)
time.sleep(0.35)
except Exception as exc:
_append_helper_log(
f"[helper] failed to terminate older helpers: {type(exc).__name__}: {exc}"
)
_lock_fh = _acquire_ipc_lock(str(args.ipc))
if _lock_fh is not None:
break
if not lock_wait_logged:
_append_helper_log(
f"[helper] waiting for helper lock release ipc={args.ipc}"
)
lock_wait_logged = True
if time.time() >= lock_wait_deadline:
_append_helper_log(
f"[helper] another instance still holds lock for ipc={args.ipc}; exiting after wait"
)
return 0
time.sleep(0.20)
try:
_append_helper_log(
f"[helper] version={MEDEIA_MPV_HELPER_VERSION} started ipc={args.ipc}"
)
try:
_lock_fh.seek(0)
_lock_fh.truncate()
_lock_fh.write(
json.dumps(
{
"pid": os.getpid(),
"version": MEDEIA_MPV_HELPER_VERSION,
"ipc": str(args.ipc),
"started_at": int(time.time()),
},
ensure_ascii=False,
)
)
_lock_fh.flush()
except Exception:
pass
try:
_append_helper_log(
f"[helper] file={Path(__file__).resolve()} cwd={Path.cwd().resolve()}"