diff --git a/mempalace/palace.py b/mempalace/palace.py index 7ed315c8ac..cf7b10abc6 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; diff --git a/tests/test_palace_locks.py b/tests/test_palace_locks.py index bf53a9d178..df94f16d8d 100644 --- a/tests/test_palace_locks.py +++ b/tests/test_palace_locks.py @@ -12,10 +12,12 @@ import multiprocessing import os import time +import sys import pytest from mempalace.palace import ( + _write_lock_holder, MineAlreadyRunning, mine_global_lock, mine_palace_lock, @@ -273,6 +275,48 @@ def test_lock_failure_message_names_holder(tmp_path, monkeypatch): holder.join(timeout=5) +def test_write_lock_holder_writes_utf8_bytes_for_non_ascii_argv(tmp_path, monkeypatch): + """Regression #1435: lock-holder identity must be written as UTF-8 bytes. + + The holder byte count and the on-disk bytes must agree even when argv + contains characters that are not representable in a Windows ANSI codepage. + """ + monkeypatch.setattr( + sys, + "argv", + ["mempalace", "mine", "café/北"], + ) + + lock_path = tmp_path / "holder.lock" + lock_path.write_bytes(b"\0stale-holder-identity-that-must-be-truncated") + + with lock_path.open("r+b") as lock_file: + _write_lock_holder(lock_file) + + ident = f"{os.getpid()} {' '.join(sys.argv[:3])}".strip() + assert lock_path.read_bytes() == b"\0" + ident.encode("utf-8") + + +def test_write_lock_holder_is_best_effort_on_unicode_error(monkeypatch): + """Regression #1435: holder-write failures must not block lock acquisition.""" + + class UnicodeFailingLock: + def seek(self, _offset): + pass + + def truncate(self, _size): + pass + + def write(self, _data): + raise UnicodeEncodeError("cp1252", "北", 0, 1, "not representable") + + def flush(self): + pass + + monkeypatch.setattr(sys, "argv", ["mempalace", "mine", "北"]) + _write_lock_holder(UnicodeFailingLock()) + + def test_lock_holder_identity_persists_across_release(tmp_path, monkeypatch): """The holder line is overwritten by each new acquirer, not appended.