From d3b035438f30099613c0f7e000bd37fcb872fc37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Thu, 19 Sep 2024 18:48:13 +0200 Subject: [PATCH] Fix segfault when trying to use PyRun_SimpleString() with some imports --- ...to-update_global_state_for_extension.patch | 62 +++++++++++++++++++ python3.13.spec | 9 ++- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 00440-gh-124160-pass-main_tstate-to-update_global_state_for_extension.patch diff --git a/00440-gh-124160-pass-main_tstate-to-update_global_state_for_extension.patch b/00440-gh-124160-pass-main_tstate-to-update_global_state_for_extension.patch new file mode 100644 index 0000000..1e9e1c0 --- /dev/null +++ b/00440-gh-124160-pass-main_tstate-to-update_global_state_for_extension.patch @@ -0,0 +1,62 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: luk1337 +Date: Thu, 19 Sep 2024 18:05:20 +0200 +Subject: [PATCH] 00440: gh-124160: Pass main_tstate to + update_global_state_for_extension() + +(cherry picked from commit 7331d0f70bc9fbac177b76b6ec03486430383425) +--- + .../C_API/2024-09-18-18-40-30.gh-issue-124160.Zy-VKi.rst | 2 ++ + Programs/_testembed.c | 9 +++++++++ + Python/import.c | 2 +- + 3 files changed, 12 insertions(+), 1 deletion(-) + create mode 100644 Misc/NEWS.d/next/C_API/2024-09-18-18-40-30.gh-issue-124160.Zy-VKi.rst + +diff --git a/Misc/NEWS.d/next/C_API/2024-09-18-18-40-30.gh-issue-124160.Zy-VKi.rst b/Misc/NEWS.d/next/C_API/2024-09-18-18-40-30.gh-issue-124160.Zy-VKi.rst +new file mode 100644 +index 0000000000..26e7aef08e +--- /dev/null ++++ b/Misc/NEWS.d/next/C_API/2024-09-18-18-40-30.gh-issue-124160.Zy-VKi.rst +@@ -0,0 +1,2 @@ ++Fix crash when importing modules containing state and single-phase ++initialization in a subinterpreter. +diff --git a/Programs/_testembed.c b/Programs/_testembed.c +index e341f0c6bf..96dbfabd7e 100644 +--- a/Programs/_testembed.c ++++ b/Programs/_testembed.c +@@ -94,6 +94,14 @@ static void _testembed_Py_Initialize(void) + } + + ++static int test_import_in_subinterpreters(void) ++{ ++ _testembed_Py_InitializeFromConfig(); ++ PyThreadState_Swap(Py_NewInterpreter()); ++ return PyRun_SimpleString("import readline"); // gh-124160 ++} ++ ++ + /***************************************************** + * Test repeated initialisation and subinterpreters + *****************************************************/ +@@ -2184,6 +2192,7 @@ static struct TestCase TestCases[] = { + {"test_repeated_init_exec", test_repeated_init_exec}, + {"test_repeated_simple_init", test_repeated_simple_init}, + {"test_forced_io_encoding", test_forced_io_encoding}, ++ {"test_import_in_subinterpreters", test_import_in_subinterpreters}, + {"test_repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters}, + {"test_repeated_init_and_inittab", test_repeated_init_and_inittab}, + {"test_pre_initialization_api", test_pre_initialization_api}, +diff --git a/Python/import.c b/Python/import.c +index 1056ddd370..be00c0b015 100644 +--- a/Python/import.c ++++ b/Python/import.c +@@ -2044,7 +2044,7 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0, + singlephase.m_init = p0; + } + cached = update_global_state_for_extension( +- tstate, info->path, info->name, def, &singlephase); ++ main_tstate, info->path, info->name, def, &singlephase); + if (cached == NULL) { + assert(PyErr_Occurred()); + goto main_finally; diff --git a/python3.13.spec b/python3.13.spec index 4b37f74..247e62f 100644 --- a/python3.13.spec +++ b/python3.13.spec @@ -17,7 +17,7 @@ URL: https://www.python.org/ %global prerel rc2 %global upstream_version %{general_version}%{?prerel} Version: %{general_version}%{?prerel:~%{prerel}} -Release: 2%{?dist} +Release: 3%{?dist} License: Python-2.0.1 @@ -394,6 +394,10 @@ Patch251: 00251-change-user-install-location.patch # gh-122145: Handle an empty AST body when reporting tracebacks Patch439: 00439-gh-122145-handle-an-empty-ast-body-when-reporting-tracebacks.patch +# 00440 # 7c35d4da60c9edc333a7031434bf114660e1ad34 +# gh-124160: Pass main_tstate to update_global_state_for_extension() +Patch440: 00440-gh-124160-pass-main_tstate-to-update_global_state_for_extension.patch + # (New patches go here ^^^) # # When adding new patches to "python" and "python3" in Fedora, EL, etc., @@ -1735,6 +1739,9 @@ CheckPython freethreading # ====================================================== %changelog +* Thu Sep 19 2024 Miro Hrončok - 3.13.0~rc2-3 +- Fix segfault when trying to use PyRun_SimpleString() with some imports + * Wed Sep 18 2024 Miro Hrončok - 3.13.0~rc2-2 - Handle an empty AST body when reporting tracebacks - Fixes: rhbz#2311907