Repository navigation
Conversation
On Windows os.rename raises FileExistsError if the target exists, so when several processes populate an empty cache at the same time every process but the first fails (and leaves its .tmp file behind). With a user-set TIKTOKEN_CACHE_DIR the error is re-raised to the caller. os.replace is atomic on every platform and overwrites the target.
mangeshraut712
approved these changes
Nov 3, 2026
mangeshraut712
left a comment
There was a problem hiding this comment.
os.replace is the right call here: it is atomic on POSIX and on Windows guarantees replacement semantics, where os.rename raises FileExistsError when the destination exists. The race test (both processes writing the same cache path, one losing the rename) reproduces the reported failure on Windows and the NoLeftOverTmp check pins the cleanup invariant. Small, correct, and well tested.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
read_file_cachedwrites the download to a.tmpfile and thenos.renames it onto the cache path. On Windowsos.renamefails if the target already exists, so when several processes populate an empty cache at the same time, every process except the first raises:With a user-set
TIKTOKEN_CACHE_DIRtheexcept OSErrorbranch re-raises, so this surfaces to callers; the losing processes also leave their.tmpfiles behind. On Linux/macOSos.renameoverwrites silently, which is why it only shows up on Windows.I hit it running a test suite with
pytest-xdist(llama-index setsTIKTOKEN_CACHE_DIR), and it reproduces outside of that: 6 processes callingtiktoken.get_encoding("cl100k_base")against an empty cache dir on Windows 11 / Python 3.12 / tiktoken 0.14.0 gives 1okand 5FileExistsErroron every run.os.replaceis atomic and overwrites on every platform, so this just switches to it. The added test simulates another process finishing first (it writes the cache file inside the patchedread_file) and fails withFileExistsErroron Windows without the change.