Repository navigation
Start token caches over in a forked child - #56
Open
talyaacovi wants to merge 2 commits into
Open
talyaacovi wants to merge 2 commits into
talyaacovi wants to merge 2 commits into
Conversation
A child forked while another thread was minting waited forever on that mint, which never finishes in the child. It now mints its own, and keeps a token minted before the fork. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Retaining a completed mint’s event across fork can cause the child’s next token() call to hang on an inherited internal lock.
1 open issue
What changed in this PR
This PR makes synchronous token caches recover after a fork, preventing children from waiting on token exchanges left running in the parent.
Changes:
- Tracks live caches and resets their locks and unfinished exchanges after fork.
- Preserves previously cached tokens.
- Adds fork regression tests; asynchronous caches remain unchanged.
| File | Description |
|---|---|
| tests/client/test_token_auth.py | Tests child-side minting and cached-token preservation. |
| baseten/client/_token_auth.py | Adds weak cache tracking and child-side reset hooks. |
Review effort: Balanced
Event.wait() takes the event's own lock even when the event is set, so a child forked while another thread held it waited forever on its first token() call. The child now gets a fresh, set event, and keeps the mint's token and refresh time. The fork test helper also narrows os.fork for type checking on Windows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
talyaacovi
marked this pull request as ready for review
December 5, 2026 00:16
This branch has not been deployed
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.

🚀 What
A process that forks while another thread is exchanging for a short-lived token, for example with
WorkloadIdentityCredentials, leaves the child waiting forever on its first request. The child gets a copy of the cache's note that an exchange is in progress, but not the thread running it, so the exchange never finishes in the child. This affectsmultiprocessingworkers started with fork, the default on Linux before Python 3.14.💻 How
Each
TokenCacheadds itself to a weak set, and anos.register_at_forkhook gives every cache in the child a new lock. An unfinished mint is cleared, and a finished one gets a fresh, set event, since another thread may have held the lockEvent.wait()takes even on a set event.AsyncTokenCacheis unchanged, since an event loop can't be used after a fork.A client's own HTTP state isn't fork-safe, so a child should create its own client. It can reuse the credentials.
🔬 Testing
Unit tests call
os.forkwhile a mint is in progress, and while another thread holds a finished mint's event lock. Both fail without the change. Not run against a live environment.