Skip to content

fix(agent): write tokenizer_config atomically via a temp file + os.replace - #267

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
emiliano-go:fix/tokenizer-config-atomic-write
Sep 23, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
emiliano-go:fix/tokenizer-config-atomic-write

Conversation

@emiliano-go

@emiliano-go emiliano-go commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

What

Follow-up to #107, as NandhaKishorM suggested. _fix_tokenizer_config detached the snapshot symlink and then wrote the file in place, so between the unlink and the write a concurrent load could see no config at all.

Now it writes to a temp file in the same directory and os.replaces it onto the snapshot entry. The replace is atomic, so a reader only ever sees the old file or the complete new one, and the entry becomes a regular file so the shared blob is still untouched. If the swap fails, the temp file is removed and the original is left as it was. The file mode is preserved too (mkstemp creates 0600, the cache file is usually 0644).

Verification

tests/test_tokenizer_cache.py (14 checks, no weights downloaded):

  • blob byte-identical, snapshot is a regular file, carries the patch, drops backend/is_local
  • no .tokenizer_config.*.tmp left behind
  • file mode matches the original
  • a forced os.replace failure warns, cleans up the temp file, and leaves the symlink and blob intact
  • malformed config still warns instead of raising

Also ran test_lazy_import, test_load_errors, test_download, test_criteria, test_router, test_packaging, plus ruff and compileall.

…place

_fix_tokenizer_config detached the snapshot symlink and then wrote the file in
place, so a concurrent load could see no config for a moment. Write to a temp
file in the same directory and os.replace it, which swaps the snapshot entry
atomically and never exposes a missing or half-written file. The temp file is
removed if the swap fails, and the original file mode is preserved.
@NandhaKishorM

Copy link
Copy Markdown
Owner

Thank you for the quick follow-up to #107. Writing to a temp file and os.replace-ing it closes the window where a concurrent load could see no config, and preserving the file mode is a nice detail. Merging.

@NandhaKishorM
NandhaKishorM merged commit a8de2c5 into NandhaKishorM:main Sep 23, 2026
14 checks passed
baninaveen pushed a commit to baninaveen/laya that referenced this pull request Sep 24, 2026
…place (NandhaKishorM#267)

_fix_tokenizer_config detached the snapshot symlink and then wrote the file in
place, so a concurrent load could see no config for a moment. Write to a temp
file in the same directory and os.replace it, which swaps the snapshot entry
atomically and never exposes a missing or half-written file. The temp file is
removed if the swap fails, and the original file mode is preserved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants