GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-140947: fix contextvars handling for server tasks in asyncio by kumaraditya303 · Pull Request #141158 · python/cpython

Conversation

@kumaraditya303

@kumaraditya303 kumaraditya303 commented Nov 6, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • Issue: context variables can leak out of asyncio.Task #140947

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@kumaraditya303
properly propagate contextvars for server protocols
a7bf339
@bedevere-app bedevere-app Bot mentioned this pull request Nov 6, 2025
context variables can leak out of asyncio.Task #140947
Closed
@Kludex

Kludex commented Nov 17, 2025

Copy link
Copy Markdown

Any chance this can be considered a security issue?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kumaraditya303

kumaraditya303 commented Nov 17, 2025

Copy link
Copy Markdown
Contributor Author

Any chance this can be considered a security issue?

There are a lot of moving parts in fixing this and this PR only fixes the non-ssl part, ssl remains remains to be fixed.
IMO it is too risky to backport to security branches, I am in favor of backport to bug-fixes branches after a careful review.

Kludex reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pmeier pmeier mentioned this pull request Nov 21, 2025
explicitly start ASGI run with empty context Kludex/uvicorn#2742
Merged
2 of 3 tasks
kumaraditya303 added 4 commits December 2, 2025 11:50
@kumaraditya303
fix _add_writer
40a8f7c
@kumaraditya303
Merge branch 'main' of https://github.com/python/cpython into asyncio…
d786ca0
…-contextvars-fix
@kumaraditya303
Merge branch 'main' of https://github.com/python/cpython into asyncio…
6990bb9
…-contextvars-fix
@kumaraditya303
refactor 1
f845484
@kumaraditya303
kumaraditya303 force-pushed the asyncio-contextvars-fix branch from 6354b14 to f845484 Compare January 13, 2026 14:20
kumaraditya303 added 6 commits January 13, 2026 19:56
@kumaraditya303
all tests pass on mac
f556a84
@kumaraditya303
temp fix windows
cbbf0c6
@kumaraditya303
Merge branch 'asyncio-contextvars-fix' of https://github.com/kumaradi…
ac7e954
…tya303/cpython; branch 'main' of https://github.com/python/cpython into asyncio-contextvars-fix
@kumaraditya303
add tests
b344e60
@kumaraditya303
fix tests
6a8e2bd
@kumaraditya303
add more windows tests
ec0ed9b
@kumaraditya303
kumaraditya303 marked this pull request as ready for review March 21, 2026 06:50
@kumaraditya303
kumaraditya303 requested review from 1st1, asvetlov and willingc as code owners March 21, 2026 06:50
@bedevere-app bedevere-app Bot added the awaiting core review label Mar 21, 2026
kumaraditya303 and others added 5 commits March 21, 2026 13:08
@kumaraditya303
fix ssl part
09df55e
@kumaraditya303
fix tests
edcedbb
@kumaraditya303
fix windows tests
e1ebe67
@kumaraditya303
Merge branch 'asyncio-contextvars-fix' of https://github.com/kumaradi…
c42e01c
…tya303/cpython into asyncio-contextvars-fix
@blurb-it
📜🤖 Added by blurb_it.
a2039d6
@kumaraditya303 kumaraditya303 added topic-asyncio 🔨 test-with-buildbots Test PR w/ buildbots; report in status section labels Mar 21, 2026
@bedevere-bot

bedevere-bot commented Mar 21, 2026

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @kumaraditya303 for commit a2039d6 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F141158%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Mar 21, 2026
@vstinner

vstinner commented Mar 21, 2026

Copy link
Copy Markdown
Member

I restarted the buildbot server. It seems like buildbot workers are now working properly again.

Kludex reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kumaraditya303
fix tests on machine without ssl
69874d5
@kumaraditya303
kumaraditya303 merged commit 60fbc20 into python:main Mar 21, 2026
85 of 87 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting core review label Mar 21, 2026
@kumaraditya303
kumaraditya303 deleted the asyncio-contextvars-fix branch March 21, 2026 12:14
CuriousLearner added a commit to CuriousLearner/cpython that referenced this pull request Mar 22, 2026
@CuriousLearner
Merge remote-tracking branch 'upstream/main' into fix-pythongh-138577
2691ad9
* upstream/main: (1475 commits)
  Docs: replace all `datetime` imports with `import datetime as dt` (python#145640)
  pythongh-146153: Use `frozendict` in pure python fallback for `curses.has_key` (python#146154)
  pythongh-138234: clarify returncode behavior for subprocesses created with `shell=True` (python#138536)
  pythongh-140947: fix contextvars handling for server tasks in asyncio  (python#141158)
  pythonGH-100108: Add async generators best practices section (python#141885)
  pythonGH-145667: Merge `GET_ITER` and `GET_YIELD_FROM_ITER` (pythonGH-146120)
  pythongh-146228: Better fork support in cached FastPath (python#146231)
  pythongh-146227: Fix wrong type in _Py_atomic_load_uint16 in pyatomic_std.h (pythongh-146229)
  pythongh-145980: Fix copy/paste mistake in binascii.c (python#146230)
  pythongh-146092: Raise MemoryError on allocation failure in _zoneinfo (python#146165)
  pythongh-91279: Note `SOURCE_DATE_EPOCH` support in `ZipFile.writestr()` doc (python#139396)
  pythongh-146196: Fix Undefined Behavior in _PyUnicodeWriter_WriteASCIIString() (python#146201)
  pythongh-143930: Reject leading dashes in webbrowser URLs
  pythongh-145916: Soft-deprecate ctypes.util.find_library (pythonGH-145919)
  pythongh-146205: Check the errno with != 0 in close impls in select module (python#146206)
  pythongh-146171: Fix nested AttributeError suggestions (python#146188)
  pythongh-146099: Optimize _GUARD_CODE_VERSION+IP via function version symbols (pythonGH-146101)
  pythongh-145980: Add support for alternative alphabets in the binascii module (pythonGH-145981)
  pythongh-145754: Update signature retrieval in unittest.mock to use forwardref annotation format (python#145756)
  pythongh-145177: Add emscripten run --test, uses test args from config.toml (python#146160)
  ...
@pablogsal pablogsal mentioned this pull request Apr 4, 2026
gh-130273: Fix traceback color output with unicode characters #142529
Merged
@Kludex Kludex mentioned this pull request Apr 21, 2026
Revert empty context for ASGI runs Kludex/uvicorn#2911
Merged
4 tasks done
Kludex added a commit to Kludex/uvicorn that referenced this pull request Apr 21, 2026
@Kludex
Add --reset-contextvars flag to isolate ASGI request context
7875420
Reintroduces the behavior from #2742 as an opt-in flag. When set, each
ASGI request runs in a fresh contextvars.Context, which works around
the asyncio context leak in python/cpython#140947
(expected to be fixed upstream for Python >=3.15 via python/cpython#141158).

Default is off so context set in the lifespan or by external
instrumentation (e.g. OpenTelemetry eBPF propagation) remains visible
to ASGI handlers.
@Kludex Kludex mentioned this pull request Apr 21, 2026
Add --reset-contextvars flag to isolate ASGI request context Kludex/uvicorn#2912
Merged
6 tasks done
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@kumaraditya303 @ljfp
pythongh-140947: fix contextvars handling for server tasks in asyncio (…
97e41ae
…python#141158)
@adubovik adubovik mentioned this pull request Sep 18, 2026
fix: start each request from its own OTEL trace and log context epam/ai-dial-sdk#450
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@1st1 1st1 Awaiting requested review from 1st1 1st1 is a code owner
@asvetlov asvetlov Awaiting requested review from asvetlov asvetlov is a code owner
@willingc willingc Awaiting requested review from willingc willingc is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

topic-asyncio

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@kumaraditya303 @Kludex @bedevere-bot @vstinner
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.