From aa34046ed5206eb47984d6218c64fd35f504a887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cbhuvi27=E2=80=9D?= Date: Sun, 21 Jun 2026 16:16:38 +0530 Subject: [PATCH 1/2] gh-151773: NULL-check token_new() in PyContextVar_Set Avoid Py_DECREF(NULL) when token_new() fails under memory pressure. --- .../next/Core/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst | 2 ++ Python/context.c | 3 +++ 2 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Core/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst diff --git a/Misc/NEWS.d/next/Core/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst b/Misc/NEWS.d/next/Core/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst new file mode 100644 index 000000000000000..8a35521ef59816d --- /dev/null +++ b/Misc/NEWS.d/next/Core/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst @@ -0,0 +1,2 @@ +Fix a crash in :func:`contextvars.ContextVar.set` when +:func:`!token_new` fails under memory pressure. diff --git a/Python/context.c b/Python/context.c index 593e6ef90037cfa..4678054ff3ad743 100644 --- a/Python/context.c +++ b/Python/context.c @@ -362,6 +362,9 @@ PyContextVar_Set(PyObject *ovar, PyObject *val) Py_XINCREF(old_val); PyContextToken *tok = token_new(ctx, var, old_val); Py_XDECREF(old_val); + if (tok == NULL) { + return NULL; + } if (contextvar_set(var, val)) { Py_DECREF(tok); From b77c4d6b9357fee7c1cd525fed7c2287ffd0b222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cbhuvi27=E2=80=9D?= Date: Mon, 22 Jun 2026 07:39:29 +0530 Subject: [PATCH 2/2] gh-151773: Move NEWS entry to Core_and_Builtins There is no Core section in Misc/NEWS.d/next/. --- .../next/Core/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst | 2 -- .../2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/Core/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst diff --git a/Misc/NEWS.d/next/Core/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst b/Misc/NEWS.d/next/Core/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst deleted file mode 100644 index 8a35521ef59816d..000000000000000 --- a/Misc/NEWS.d/next/Core/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a crash in :func:`contextvars.ContextVar.set` when -:func:`!token_new` fails under memory pressure. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst new file mode 100644 index 000000000000000..b4193c5e15ffef8 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst @@ -0,0 +1,2 @@ +Fix a crash in :func:`contextvars.ContextVar.set` when memory allocation +fails.