You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: Shared Mutable Class-Level context Dict in Confidence #114
because__init__never assigns self.context = {}, all instances share the same dict object at the class level. Any call to put_context on one instance mutates that shared dict, silently contaminating every other Confidence instance in the process.
impact
context set on one Confidence instance leaks into all other instances.
child instances created via with_context are safe (they assign a new dict), but the root instance and any sibling roots
are not.
in a multi-tenant or multi-user scenario, this is a data isolation failure.
reproduction
fromconfidence.confidenceimportConfidencea=Confidence("secret-a")
b=Confidence("secret-b")
a.put_context("user", "alice")
print(b.context) # {"user": "alice"} — wrong, b is contaminated
Bug confirmed. b was never touched, but it has Alice's context because both instances share the same class-level dict.
fix
context (critical)
add self.context: Dict[str, FieldType] = {} inside __init__, and change the async_client default to None with a guard in the body.
async_client (secondary) Change the default from httpx.AsyncClient() (evaluated once at class definition time, shared across all instances) to None, instantiating a fresh httpx.AsyncClient() per instance in the body of __init__. This prevents shared client state under concurrent use.
Regression test added tests/test_confidence.py::TestConfidence::test_context_is_isolated_per_instance — creates two root instances, mutates one, and asserts the other is unaffected.
Activity
nicklasl commentedon May 26, 2026
Thanks a lot for the detailed report!
Feel free to open the PR and we'll take a look 👍.
I've been considering removing the
put_contextfunction for some time so we'll probably do that as well.ausbernard commentedon May 26, 2026
of course! that makes sense, what made you want to remove the function?
PR opened, fixed black formatting. 👍
nicklasl commentedon Jun 5, 2026
fixed in #115