Skip to content

Start token caches over in a forked child - #56

Open
talyaacovi wants to merge 2 commits into
mainfrom
tal/token-cache-fork-reset
Open

talyaacovi wants to merge 2 commits into
mainfrom
tal/token-cache-fork-reset

Conversation

@talyaacovi

@talyaacovi talyaacovi commented Dec 4, 2026 •

Copy link
Copy Markdown

🚀 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 affects multiprocessing workers started with fork, the default on Linux before Python 3.14.

  • A forked child drops an exchange another thread hadn't finished, and does its own on first use. A token fetched before the fork is kept, with its refresh time.

💻 How

Each TokenCache adds itself to a weak set, and an os.register_at_fork hook 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 lock Event.wait() takes even on a set event. AsyncTokenCache is 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.fork while 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.

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>
@CLAassistant

CLAassistant commented Dec 4, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread baseten/client/_token_auth.py Resolved
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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the synchronous token-cache fork-safety changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@talyaacovi
talyaacovi marked this pull request as ready for review December 5, 2026 00:16

This branch has not been deployed

No deployments
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.

3 participants