Skip to content

fix: isolate context dict per Confidence instance to prevent shared mutable state#115

Merged
nicklasl merged 2 commits into
spotify:mainfrom
ausbernard:fix/shared-mutable-context-dict
May 26, 2026
Merged

fix: isolate context dict per Confidence instance to prevent shared mutable state#115
nicklasl merged 2 commits into
spotify:mainfrom
ausbernard:fix/shared-mutable-context-dict

Conversation

@ausbernard
Copy link
Copy Markdown
Contributor

Description:

Summary

  • Moves context from a class-level attribute to an instance-level attribute initialized in __init__, preventing all
    Confidence instances from sharing the same dict
  • Changes async_client default from a mutable class-level default argument (httpx.AsyncClient()) to None, instantiating a
    fresh client per instance — fixing the same class of mutable-default-argument bug
  • Adds a regression test to assert that context mutations on one instance do not leak to another

Root cause

context: Dict[str, FieldType] = {} was declared at class scope, making it a single shared dict across all instances. Any call to put_context on one Confidence object would silently mutate the context for every other instance in the same process. The same issue existed for async_client, which was evaluated once at class definition time and shared.

Test plan - see issue:

  • test_context_is_isolated_per_instance passes — confirms the fix
  • Existing test suite passes — no regressions in flag resolution or telemetry behavior

@nicklasl
Copy link
Copy Markdown
Member

Looks good. Let's just make it green 👍

@ausbernard
Copy link
Copy Markdown
Contributor Author

👍 I will fix my black formatting real quick and push back up - hold on one sec.

@ausbernard
Copy link
Copy Markdown
Contributor Author

✅ done: re-pushing to branch.

confidence-sdk-python on  fix/shared-mutable-context-dict [!] via  v3.11.9 
❯ black --check confidence --exclude="telemetry_pb2.py|_version.py"                                                (base) 
All done! ✨ 🍰 ✨
7 files would be left unchanged.

Copy link
Copy Markdown
Member

@nicklasl nicklasl left a comment

Choose a reason for hiding this comment

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

Thanks!

@nicklasl nicklasl merged commit 3fd546f into spotify:main May 26, 2026
5 checks passed
@ausbernard ausbernard deleted the fix/shared-mutable-context-dict branch May 26, 2026 16:12
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