Fix Context in-place mutability bypass via inherited dict methods#5399
Merged
Conversation
|
|
herin049
approved these changes
Jul 7, 2026
xrmx
approved these changes
Jul 10, 2026
carlosalberto
approved these changes
Jul 10, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens opentelemetry.context.context.Context immutability while preserving its dict-subclass API surface, addressing a spec-contract violation where CPython’s built-in dict mutators could bypass __setitem__ and mutate Context in place.
Changes:
- Override additional mutating
dictmethods onContext(e.g.,update,clear,pop,setdefault,__ior__) to raiseValueError. - Expand the existing immutability test to assert these mutation paths raise.
- Add a changelog entry documenting the fix.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| opentelemetry-api/src/opentelemetry/context/context.py | Adds overrides for mutating dict APIs to enforce Context immutability. |
| opentelemetry-api/tests/context/test_context.py | Expands immutability assertions to cover more mutation methods. |
| .changelog/5399.fixed | Notes the fix in the changelog. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
lzchen
approved these changes
Jul 13, 2026
lzchen
enabled auto-merge
July 13, 2026 21:53
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Jul 13, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Jul 13, 2026
emdneto
approved these changes
Jul 13, 2026
xrmx
enabled auto-merge
July 14, 2026 07:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a bug where
Contextinstances could be mutated in place via inherited CPythondictmethods (update,clear,pop, etc.), violating theContext MUST be immutablespecification contract.The
Contextobject currently subclassesdictand enforces immutability for direct assignment by overriding__setitem__to raiseValueError. However, CPython's C-level dictionary methods bypass Python's magic method overrides, allowing for silent in-place mutation.This fix maintains the backwards-compatible
dictbase class while explicitly overriding the remaining mutating methods to raiseValueError, aligning them with the existing__setitem__behavior. Tests have been expanded to independently verify that everydictmutation method raises correctly.Fixes #5398
Type of change
How Has This Been Tested?
test_context_is_immutableto explicitly coverupdate,setdefault,pop,popitem,clear,__delitem__, and__ior__.pyright.tox -e py312-test-opentelemetry-apiDoes This PR Require a Contrib Repo Change?
Checklist: