Skip to content

context= dict is stored by reference and leaks between containers #568

Closed

Description

@lesnik512

This was generated by AI during a pre-4.0 audit of main at 9cd88e7.

Observation

container.py:89: ContextRegistry(context=context or {}) keeps the caller's dict. Reproduced:

ctx = {Req: r}
c1 = root.build_child_container(context=ctx)
c2 = root.build_child_container(context=ctx)
c1.set_context(User, "alice")
# c2 sees User; ctx now contains User

A module-level dict reused per request leaks set_context values across requests.

Proposal

Copy the dict on construction (dict(context)). Costs one small dict copy per child build; measure G6/G7. Observable change for code relying on the mutation, so before 4.0.

Acceptance

Failing test first; set_context on one container is invisible to siblings and to the caller's dict.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingready-for-agentFully specified, ready for an AFK agent

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions