Skip to content

AI turns documents or topics into real, native PowerPoint decks—with native shapes, transitions and animations, data-backed charts and tables on demand, audio narration from speaker notes, and support for your own .pptx templates. · by Hugo He

fix(preview): tolerate corrupt preview lock pids

hugohe30b1ac97ad62b
diff --git a/skills/ppt-master/scripts/confirm_ui/server.py b/skills/ppt-master/scripts/confirm_ui/server.py
index 37800166..6e257e4e 100644
--- a/skills/ppt-master/scripts/confirm_ui/server.py
+++ b/skills/ppt-master/scripts/confirm_ui/server.py
@@ -56,7 +56,9 @@ if str(_SCRIPTS_DIR) not in sys.path:
from console_encoding import configure_utf8_stdio # noqa: E402
from server_common import ( # noqa: E402
claim_lock as _claim_lock,
+ clear_lock as _clear_lock,
find_free_port as _find_free_port,
+ lock_pid as _lock_pid,
popen_detached as _popen_detached,
process_alive as _process_alive,
read_lock as _read_lock,
@@ -222,7 +224,7 @@ def _live_lock(lock_file: Path) -> Optional[dict]:
existing = _read_lock(lock_file)
if not existing:
return None
- if _process_alive(int(existing.get('pid', 0) or 0)):
+ if _process_alive(_lock_pid(existing)):
return existing
return None
@@ -515,7 +517,7 @@ def _wait_only_for_result(
return 0
lock = _read_lock(lock_file)
- pid = int((lock or {}).get('pid', 0) or 0)
+ pid = _lock_pid(lock)
if not pid or not _process_alive(pid):
logger.error('confirm server is no longer running before stage=%s was confirmed', target_stage)
return 1
@@ -543,10 +545,10 @@ def _shutdown_existing(lock_file: Path) -> int:
if not existing:
logger.info('no confirm server running — nothing to stop')
return 0
- pid = int(existing.get('pid', 0) or 0)
+ pid = _lock_pid(existing)
port = existing.get('port')
if not _process_alive(pid):
- _release_lock(lock_file)
+ _clear_lock(lock_file)
logger.info('confirm server already stopped; cleared stale lock')
return 0
# Graceful first: the server flushes and releases its own lock.
@@ -570,7 +572,7 @@ def _shutdown_existing(lock_file: Path) -> int:
os.kill(pid, signal.SIGTERM)
except OSError:
pass
- _release_lock(lock_file)
+ _clear_lock(lock_file)
logger.info('confirm server stopped (pid=%s)', pid)
return 0
@@ -973,7 +975,7 @@ def main(argv: Optional[list[str]] = None) -> int:
if args.daemon:
lock_file = project_path / LOCK_FILE_NAME
existing = _read_lock(lock_file)
- if existing and _process_alive(int(existing.get('pid', 0))):
+ if existing and _process_alive(_lock_pid(existing)):
existing_pid = existing.get('pid', '?')
existing_port = existing.get('port', '?')
logger.error(
diff --git a/skills/ppt-master/scripts/server_common.py b/skills/ppt-master/scripts/server_common.py
index 92a5c650..058b2a94 100644
--- a/skills/ppt-master/scripts/server_common.py
+++ b/skills/ppt-master/scripts/server_common.py
@@ -9,7 +9,7 @@ cross-platform process-liveness check and the claim/read/release lock logic so
the two servers cannot drift apart.
Usage:
- from server_common import process_alive, read_lock, claim_lock, release_lock, find_free_port
+ from server_common import process_alive, read_lock, lock_pid, claim_lock, release_lock, clear_lock, find_free_port
Dependencies:
None (only uses standard library)
@@ -76,7 +76,7 @@ def popen_detached(
return subprocess.Popen(args, creationflags=base_flags, **kwargs)
-def process_alive(pid: int) -> bool:
+def process_alive(pid: object) -> bool:
"""Return True if a process with this pid is reachable.
On POSIX, ``os.kill(pid, 0)`` succeeds when the process exists even without
@@ -84,7 +84,11 @@ def process_alive(pid: int) -> bool:
alive. On Windows there is no ``os.kill(pid, 0)`` equivalent, so probe via
``OpenProcess`` + ``WaitForSingleObject``.
"""
- if pid <= 0:
+ try:
+ pid_int = int(pid)
+ except (TypeError, ValueError):
+ return False
+ if pid_int <= 0:
return False
if os.name == 'nt':
import ctypes
@@ -114,7 +118,7 @@ def process_alive(pid: int) -> bool:
handle = kernel32.OpenProcess(
process_query_limited_information | synchronize,
False,
- pid,
+ pid_int,
)
if not handle:
return ctypes.get_last_error() == 5 # ERROR_ACCESS_DENIED
@@ -129,7 +133,7 @@ def process_alive(pid: int) -> bool:
kernel32.CloseHandle(handle)
try:
- os.kill(pid, 0)
+ os.kill(pid_int, 0)
except ProcessLookupError:
return False
except PermissionError:
@@ -148,6 +152,20 @@ def read_lock(lock_file: Path) -> Optional[dict]:
return None
+def lock_pid(lock: Optional[dict]) -> int:
+ """Return a valid pid from a lock dict, or 0 if absent/corrupt."""
+ if not lock:
+ return 0
+ raw_pid = lock.get('pid', 0)
+ if isinstance(raw_pid, bool):
+ return 0
+ if isinstance(raw_pid, int):
+ return raw_pid if raw_pid > 0 else 0
+ if isinstance(raw_pid, str) and raw_pid.strip().isdigit():
+ return int(raw_pid.strip())
+ return 0
+
+
def claim_lock(lock_file: Path, port: int) -> Optional[dict]:
"""Try to claim the per-project preview slot.
@@ -156,7 +174,7 @@ def claim_lock(lock_file: Path, port: int) -> Optional[dict]:
A stale lock (pointing at a dead pid) is silently overwritten.
"""
existing = read_lock(lock_file)
- if existing and process_alive(int(existing.get('pid', 0))):
+ if existing and process_alive(lock_pid(existing)):
return existing
lock_file.write_text(
json.dumps({'pid': os.getpid(), 'port': port}),
@@ -169,7 +187,15 @@ def release_lock(lock_file: Path) -> None:
"""Best-effort cleanup: only delete the lock if it still names *us*."""
try:
current = read_lock(lock_file)
- if current and int(current.get('pid', 0)) == os.getpid():
+ if lock_pid(current) == os.getpid():
lock_file.unlink(missing_ok=True)
except OSError:
pass
+
+
+def clear_lock(lock_file: Path) -> None:
+ """Best-effort cleanup for a lock already proven stale by the caller."""
+ try:
+ lock_file.unlink(missing_ok=True)
+ except OSError:
+ pass
diff --git a/skills/ppt-master/scripts/svg_editor/server.py b/skills/ppt-master/scripts/svg_editor/server.py
index 005a440f..c7358152 100644
--- a/skills/ppt-master/scripts/svg_editor/server.py
+++ b/skills/ppt-master/scripts/svg_editor/server.py
@@ -62,7 +62,9 @@ if str(_ROOT_SCRIPTS_DIR) not in sys.path:
from console_encoding import configure_utf8_stdio # noqa: E402
from server_common import ( # noqa: E402
claim_lock as _claim_lock,
+ clear_lock as _clear_lock,
find_free_port as _find_free_port,
+ lock_pid as _lock_pid,
popen_detached as _popen_detached,
process_alive as _process_alive,
read_lock as _read_lock,
@@ -129,8 +131,7 @@ def _cache_put(cache: dict, lock: threading.Lock, path: str, mtime: float, value
cache[path] = (mtime, value)
-# Lock / liveness helpers are shared with confirm_ui via server_common
-# (imported above as _process_alive / _read_lock / _claim_lock / _release_lock).
+# Lock / liveness helpers are shared with confirm_ui via server_common.
def _inline_icons(content: str) -> tuple[str, list[dict]]:
@@ -889,7 +890,7 @@ def _legacy_live_lock(project_path: Path) -> Optional[dict]:
"""Return a live legacy root lock, if one exists."""
legacy_lock = project_path / LEGACY_LOCK_FILE_NAME
existing = _read_lock(legacy_lock)
- if existing and _process_alive(int(existing.get('pid', 0))):
+ if existing and _process_alive(_lock_pid(existing)):
return existing
return None
@@ -906,13 +907,13 @@ def _shutdown_existing(project_path: Path) -> int:
logger.info('no live preview server running — nothing to stop')
return 0
- pid = int(existing.get('pid', 0) or 0)
+ pid = _lock_pid(existing)
try:
port = int(existing.get('port', 0) or 0)
except (TypeError, ValueError):
port = 0
if not _process_alive(pid):
- _release_lock(lock_file)
+ _clear_lock(lock_file)
logger.info('live preview already stopped; cleared stale lock')
return 0
@@ -937,7 +938,7 @@ def _shutdown_existing(project_path: Path) -> int:
os.kill(pid, signal.SIGTERM)
except OSError:
pass
- _release_lock(lock_file)
+ _clear_lock(lock_file)
logger.info('live preview server stopped (pid=%s)', pid)
return 0
@@ -1073,7 +1074,7 @@ def main(argv: Optional[list[str]] = None) -> int:
if args.daemon:
existing = _read_lock(lock_file)
- if existing and _process_alive(int(existing.get('pid', 0))):
+ if existing and _process_alive(_lock_pid(existing)):
existing_pid = existing.get('pid', '?')
existing_port = existing.get('port', '?')
logger.error(