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 1/3] 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 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; From c49c10f6e462645f7a7d598ff1ddc4f355bf5a58 Mon Sep 17 00:00:00 2001 From: fatkobra <55045047+fatkobra@users.noreply.github.com> Date: Sun, 10 May 2026 16:56:10 +0200 Subject: [PATCH 2/3] Add regression tests for UTF-8 lock holder writes --- tests/test_palace_locks.py | 56 ++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/tests/test_palace_locks.py b/tests/test_palace_locks.py index bf53a9d178..b4d2fbc259 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, @@ -197,9 +199,9 @@ def test_reentrant_same_thread_passes_through(tmp_path, monkeypatch): child = ctx.Process(target=_try_acquire_expect_busy, args=(palace, result_q)) try: child.start() - assert ( - result_q.get(timeout=10) == "busy" - ), "outer lock should still be held by parent after inner re-entrant exit" + assert result_q.get(timeout=10) == "busy", ( + "outer lock should still be held by parent after inner re-entrant exit" + ) child.join(timeout=5) assert child.exitcode == 0 finally: @@ -265,14 +267,56 @@ def test_lock_failure_message_names_holder(tmp_path, monkeypatch): pytest.fail("second acquire of same palace should have raised") msg = str(excinfo.value) - assert ( - f"PID {holder_pid}" in msg - ), f"lock-failure message must name the holder PID; got: {msg!r}" + assert f"PID {holder_pid}" in msg, ( + f"lock-failure message must name the holder PID; got: {msg!r}" + ) finally: open(release, "w").close() 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. From 419a34c5c584bb179fb543a386b4d61f180e9671 Mon Sep 17 00:00:00 2001 From: Igor Lins e Silva <4753812+igorls@users.noreply.github.com> Date: Sun, 17 May 2026 15:01:14 -0300 Subject: [PATCH 3/3] style(tests): restore ruff 0.4.x assert formatting in test_palace_locks (#1438) The PR reformatted two pre-existing assertions with a newer ruff (0.5+ `assert X, (\n msg\n)` style) that CI's pinned ruff (>=0.4.0,<0.5) rejects, failing `ruff format --check .`. Revert those two unrelated blocks to develop's 0.4.x form; the genuine #1435 fix and its new regression tests are untouched. --- tests/test_palace_locks.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_palace_locks.py b/tests/test_palace_locks.py index b4d2fbc259..df94f16d8d 100644 --- a/tests/test_palace_locks.py +++ b/tests/test_palace_locks.py @@ -199,9 +199,9 @@ def test_reentrant_same_thread_passes_through(tmp_path, monkeypatch): child = ctx.Process(target=_try_acquire_expect_busy, args=(palace, result_q)) try: child.start() - assert result_q.get(timeout=10) == "busy", ( - "outer lock should still be held by parent after inner re-entrant exit" - ) + assert ( + result_q.get(timeout=10) == "busy" + ), "outer lock should still be held by parent after inner re-entrant exit" child.join(timeout=5) assert child.exitcode == 0 finally: @@ -267,9 +267,9 @@ def test_lock_failure_message_names_holder(tmp_path, monkeypatch): pytest.fail("second acquire of same palace should have raised") msg = str(excinfo.value) - assert f"PID {holder_pid}" in msg, ( - f"lock-failure message must name the holder PID; got: {msg!r}" - ) + assert ( + f"PID {holder_pid}" in msg + ), f"lock-failure message must name the holder PID; got: {msg!r}" finally: open(release, "w").close() holder.join(timeout=5)