From fc384cb513013544c50bf4ed213797339cde3e0e Mon Sep 17 00:00:00 2001 From: Junhyung Lee <70549809+espressolee@users.noreply.github.com> Date: Wed, 21 Oct 2026 03:32:09 +0800 Subject: [PATCH] gh-159864: Fix syslog.openlog() crash with concurrent sys.argv changes (GH-159884) syslog_get_argv() took sys.argv[0] with PyList_GetItem() and increfed it only afterwards, so on the free-threaded build a concurrent replacement of sys.argv[0] could free it in between. Use PyList_GetItemRef() instead. (cherry picked from commit e5e0748abd27e39655cb8dc313a82cad6563c54f) Co-authored-by: Junhyung Lee <70549809+espressolee@users.noreply.github.com> Co-authored-by: Lazizbek Ergashev <20501725+lazerg@users.noreply.github.com> --- Lib/test/test_free_threading/test_syslog.py | 23 +++++++++++++++++++ ...-10-20-12-30-00.gh-issue-159864.4FZ6MR.rst | 2 ++ Modules/syslogmodule.c | 3 +-- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-10-20-12-30-00.gh-issue-159864.4FZ6MR.rst diff --git a/Lib/test/test_free_threading/test_syslog.py b/Lib/test/test_free_threading/test_syslog.py index b374a98b96e6de..fb43a591564613 100644 --- a/Lib/test/test_free_threading/test_syslog.py +++ b/Lib/test/test_free_threading/test_syslog.py @@ -1,6 +1,8 @@ +import sys import unittest import threading +from test import support from test.support import import_helper, threading_helper from test.support.threading_helper import run_concurrently @@ -39,6 +41,27 @@ def worker(): nthreads=NTHREADS, ) + def test_racing_openlog_argv(self): + # gh-159864: openlog() without an ident reads sys.argv[0] while + # another thread replaces it. + argv = ["prog"] + + def opener(): + for _ in range(1000): + syslog.openlog() + + def writer(): + for i in range(1000): + argv[0] = f"prog-{i}" + + with support.swap_attr(sys, "argv", argv): + try: + run_concurrently( + worker_func=[opener] * (NTHREADS - 1) + [writer], + ) + finally: + syslog.closelog() + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/Library/2026-10-20-12-30-00.gh-issue-159864.4FZ6MR.rst b/Misc/NEWS.d/next/Library/2026-10-20-12-30-00.gh-issue-159864.4FZ6MR.rst new file mode 100644 index 00000000000000..e5b9b02a27cb97 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-20-12-30-00.gh-issue-159864.4FZ6MR.rst @@ -0,0 +1,2 @@ +Fix a crash in :func:`syslog.openlog` under the free-threaded build when +:data:`sys.argv` is modified concurrently. diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index cdf76cf2d1acc8..d7acefa6542a03 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -106,8 +106,7 @@ syslog_get_argv(void) return NULL; } - scriptobj = PyList_GetItem(argv, 0); - Py_XINCREF(scriptobj); + scriptobj = PyList_GetItemRef(argv, 0); Py_DECREF(argv); if (scriptobj == NULL) { PyErr_Clear();