From 2c75c6faf6c5c54514837dfd06a199eca79a9d8a Mon Sep 17 00:00:00 2001 From: fatkobra <55045047+fatkobra@users.noreply.github.com> Date: Sun, 10 May 2026 16:45:09 +0200 Subject: [PATCH] Fix mine palace lock holder encoding --- mempalace/palace.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/mempalace/palace.py b/mempalace/palace.py index 7ed315c..cf7b10a 100644 --- a/mempalace/palace.py +++ b/mempalace/palace.py @@ -387,7 +387,10 @@ def _read_lock_holder(lock_file) -> str: """Read the prior holder's identity from the lock-file body, best-effort.""" try: lock_file.seek(_LOCK_SENTINEL_BYTES) - content = lock_file.read().strip() + content = lock_file.read() + if isinstance(content, bytes): + content = content.decode("utf-8", errors="replace") + content = content.strip() except OSError: return "another writer (identity not recorded)" if not content: @@ -404,11 +407,12 @@ def _write_lock_holder(lock_file) -> None: """ try: ident = f"{os.getpid()} {' '.join(sys.argv[:3])}".strip() + ident_bytes = ident.encode("utf-8") lock_file.seek(_LOCK_SENTINEL_BYTES) - lock_file.truncate(_LOCK_SENTINEL_BYTES + len(ident.encode("utf-8"))) - lock_file.write(ident) + lock_file.truncate(_LOCK_SENTINEL_BYTES + len(ident_bytes)) + lock_file.write(ident_bytes) lock_file.flush() - except OSError: + except (OSError, UnicodeError): pass @@ -470,7 +474,7 @@ def mine_palace_lock(palace_path: str): os.close(fd) except FileExistsError: pass - lf = open(lock_path, "r+") + lf = open(lock_path, "r+b") acquired = False try: # Lock byte 0 explicitly. msvcrt.locking is byte-position dependent;