Skip to content

Fix Context in-place mutability bypass via inherited dict methods#5399

Merged
xrmx merged 8 commits into
open-telemetry:mainfrom
pranaysb:fix/context-immutability
Jul 14, 2026
Merged

Fix Context in-place mutability bypass via inherited dict methods#5399
xrmx merged 8 commits into
open-telemetry:mainfrom
pranaysb:fix/context-immutability

Conversation

@pranaysb

@pranaysb pranaysb commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes a bug where Context instances could be mutated in place via inherited CPython dict methods (update, clear, pop, etc.), violating the Context MUST be immutable specification contract.

The Context object currently subclasses dict and enforces immutability for direct assignment by overriding __setitem__ to raise ValueError. 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 dict base class while explicitly overriding the remaining mutating methods to raise ValueError, aligning them with the existing __setitem__ behavior. Tests have been expanded to independently verify that every dict mutation method raises correctly.

Fixes #5398

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • Expanded test_context_is_immutable to explicitly cover update, setdefault, pop, popitem, clear, __delitem__, and __ior__.
  • Validated method signature compatibility using pyright.
  • Ran standard test suite via tox -e py312-test-opentelemetry-api

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@pranaysb
pranaysb requested a review from a team as a code owner July 7, 2026 07:39
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 7, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: pranaysb / name: pranaysb (955cb73)

@github-project-automation github-project-automation Bot moved this to Approved PRs in Python PR digest Jul 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 dict methods on Context (e.g., update, clear, pop, setdefault, __ior__) to raise ValueError.
  • 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.

Comment thread opentelemetry-api/src/opentelemetry/context/context.py
Comment thread opentelemetry-api/tests/context/test_context.py
@lzchen
lzchen enabled auto-merge July 13, 2026 21:53
@lzchen
lzchen added this pull request to the merge queue Jul 13, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 13, 2026
@lzchen
lzchen added this pull request to the merge queue Jul 13, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 13, 2026
@emdneto emdneto moved this from Approved PRs to Ready for merge in Python PR digest Jul 13, 2026
@xrmx
xrmx enabled auto-merge July 14, 2026 07:50
@xrmx
xrmx added this pull request to the merge queue Jul 14, 2026
Merged via the queue into open-telemetry:main with commit 67a2abd Jul 14, 2026
560 checks passed
@github-project-automation github-project-automation Bot moved this from Ready for merge to Done in Python PR digest Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Bug]:Context is mutable via inherited dict methods despite immutability contract

7 participants