Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent feature flags from leaking between clients #355

Merged
merged 3 commits into from
Aug 30, 2023

Conversation

imjoehaines
Copy link
Contributor

Goal

Currently feature flags are stored in the same place across all Client instances, meaning every client shares the same list of feature flags (though they are stored in a ContextVar/thread local so don't leak between async contexts/threads)

This PR introduces a new ContextLocalState API that allows storing things separately per-client and per-context. This works by storing a WeakKeyDictionary in a ContextVar with Client instances as keys and a dict as values. The WeakKeyDictionary ensures that that when Clients are garbage collected their contextual state is collected too

In the future this can be extended to cover other contextual data (breadcrumbs, sessions etc…) but that would require a lot of refactoring and could be considered a BC break so will be implemented in a future PR for the next major version

Previously you had to manage the delegate being None if one didn't
already exist in the current context. This made the API a bit awkward as
every use of it required the same check & creation of a new delegate

Now the delegate is created in ContextLocalState directly so users don't
need to worry about it possibly returning None
@imjoehaines imjoehaines merged commit 7d8ae2b into next Aug 30, 2023
20 checks passed
@imjoehaines imjoehaines deleted the fix-feature-flag-client-leaks branch August 30, 2023 07:56
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