diff --git a/tests/test_world_build_output_lock.py b/tests/test_world_build_output_lock.py index 7b461a2f7..52f11014c 100644 --- a/tests/test_world_build_output_lock.py +++ b/tests/test_world_build_output_lock.py @@ -105,6 +105,20 @@ class WorldBuildOutputLockTests(unittest.TestCase): self.assertFalse(os.path.exists(os.path.join(out, "world.json"))) self.assertFalse(os.path.exists(os.path.join(out, "chunks"))) + def test_case_and_unicode_variants_share_a_lock(self): + with tempfile.TemporaryDirectory() as tmp: + out = os.path.join(tmp, "Wörld") + alias = os.path.join(tmp, "wo\u0308RLD") + unrelated = os.path.join(tmp, "other") + with _lock_held_by_child(tmp, out): + with self.assertRaisesRegex(RuntimeError, "another world_build is using"): + with wb._exclusive_output(alias): + pass + with wb._exclusive_output(unrelated): + pass + with wb._exclusive_output(alias): + pass + def test_short_name_spelling_of_a_held_output_is_refused(self): if os.name != "nt": self.skipTest("8.3 short names are Windows only") diff --git a/tools/world_build.py b/tools/world_build.py index 894c212fc..c67beed1d 100644 --- a/tools/world_build.py +++ b/tools/world_build.py @@ -38,6 +38,7 @@ import stat import statistics import sys import tempfile +import unicodedata try: import fcntl @@ -1272,9 +1273,12 @@ def _exclusive_output(out_dir: str): # and a successful one must contain only chunks/ and world.json. Key it on the resolved # path so every spelling of one folder (8.3 names, junctions, symlinks) shares a lock; for # a folder that does not exist yet, realpath resolves the part that does and appends the rest. - key = hashlib.sha256( - os.path.normcase(os.path.realpath(out_dir)).encode("utf-8", "surrogatepass") - ).hexdigest()[:20] + # Fold conservatively on every platform: the output volume may ignore case or + # Unicode normalization even when os.path.normcase does not (APFS, for example). + # Distinct folders that fold alike can contend, but cannot publish concurrently. + resolved = os.path.normcase(os.path.realpath(out_dir)) + lock_name = unicodedata.normalize("NFD", unicodedata.normalize("NFD", resolved).casefold()) + key = hashlib.sha256(lock_name.encode("utf-8", "surrogatepass")).hexdigest()[:20] lock_path = os.path.join(tempfile.gettempdir(), _WORLD_BUILD_LOCK + key) with open(lock_path, "a+b") as fh: fd = fh.fileno()