diff --git a/laya/agent.py b/laya/agent.py index 9a9f59c4e..08b3236e2 100644 --- a/laya/agent.py +++ b/laya/agent.py @@ -1,6 +1,7 @@ """High-level inference runtime for laya System 1 decision models.""" import json import os +import tempfile import threading import warnings from typing import Any, Dict, List, Optional, Union @@ -46,13 +47,30 @@ def _fix_tokenizer_config(path: str): tcfg["extra_special_tokens"] = {"extra_%d" % i: t for i, t in enumerate(extra)} changed = True if changed: - # HuggingFace snapshots are symlinks into a shared blob store. Writing through the + # HuggingFace snapshots are symlinks into a shared blob store, so writing through the # link would truncate a file shared with other revisions and processes, race - # concurrent loads, and desync the hub's cache metadata. Detach the local file first. - if os.path.islink(cfg_file): - os.unlink(cfg_file) - with open(cfg_file, "w") as f: - json.dump(tcfg, f, indent=2) + # concurrent loads, and desync the hub's cache metadata. Write to a temporary file in + # the same directory and `os.replace` it into place: the snapshot entry becomes a + # regular file and is swapped atomically, so a concurrent load never sees a missing + # or half-written config. + cfg_dir = os.path.dirname(cfg_file) + fd, tmp_file = tempfile.mkstemp(dir=cfg_dir, prefix=".tokenizer_config.", suffix=".tmp") + try: + with os.fdopen(fd, "w") as f: + json.dump(tcfg, f, indent=2) + # mkstemp creates the file 0600; keep the mode the cache file had so a shared + # cache stays readable to the same users as before. + try: + os.chmod(tmp_file, os.stat(cfg_file).st_mode & 0o777) + except OSError: + pass + os.replace(tmp_file, cfg_file) + except BaseException: + try: + os.unlink(tmp_file) + except OSError: + pass + raise except Exception as e: # Do not swallow this silently: if the patch did not apply, AutoTokenizer may fail later # with a confusing error and no hint that the config was the cause. diff --git a/tests/test_tokenizer_cache.py b/tests/test_tokenizer_cache.py index 2c7f5704b..6209f8548 100644 --- a/tests/test_tokenizer_cache.py +++ b/tests/test_tokenizer_cache.py @@ -2,7 +2,8 @@ `snapshot_download` lays snapshots out as symlinks into a shared `blobs/` store, so opening the snapshot file for writing truncates the shared blob. This test builds that exact layout -and asserts the blob survives. +and asserts the blob survives and the snapshot is swapped in atomically, with no temporary +file left behind. """ import os import shutil @@ -46,6 +47,10 @@ def check(name, got, want): check("snapshot/is now a regular file", os.path.islink(link), False) check("snapshot/carries the patch", '"PreTrainedTokenizerFast"' in open(link).read(), True) check("snapshot/drops backend+is_local as intended", '"backend": "x"' not in open(link).read(), True) +check("snapshot/no temporary file left behind", + [n for n in os.listdir(snap_dir) if n.startswith(".tokenizer_config.")], []) +check("snapshot/preserves the file mode", + os.stat(link).st_mode & 0o777, os.stat(blob).st_mode & 0o777) # a second call is a no-op and must not disturb anything either _fix_tokenizer_config(os.path.dirname(snap_dir)) @@ -67,6 +72,34 @@ def check(name, got, want): ) check("bad-json/file left as written", open(bad_file).read(), "{ not json") +# if the atomic swap fails, the temp file must be cleaned up and the original left intact +fail_snap = os.path.join(root, "fail", "tokenizer") +os.makedirs(fail_snap) +fail_link = os.path.join(fail_snap, "tokenizer_config.json") +os.symlink(blob, fail_link) + +real_replace = os.replace + + +def _boom(*args, **kwargs): + raise OSError("replace failed") + + +os.replace = _boom +try: + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter("always") + _fix_tokenizer_config(os.path.dirname(fail_snap)) +finally: + os.replace = real_replace + +check("replace-failure/warns instead of raising", + any(issubclass(w.category, RuntimeWarning) for w in caught), True) +check("replace-failure/no temp file left behind", + [n for n in os.listdir(fail_snap) if n.startswith(".tokenizer_config.")], []) +check("replace-failure/original symlink intact", os.path.islink(fail_link), True) +check("replace-failure/blob untouched", open(blob).read(), ORIGINAL) + shutil.rmtree(root, ignore_errors=True) print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))