https://github.com/python/cpython/commit/ed5bcda8ded8de36136a61c32eafb0fe51de7cd3
commit: ed5bcda8ded8de36136a61c32eafb0fe51de7cd3
branch: 3.14
author: Peter Bierma <[email protected]>
committer: ZeroIntensity <[email protected]>
date: 2026-10-21T21:26:39Z
summary:

[3.14] gh-159864: Fix syslog.openlog() crash with concurrent sys.argv changes 
(GH-159884) (GH-159954)

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 <[email protected]>
Co-authored-by: Lazizbek Ergashev <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-10-20-12-30-00.gh-issue-159864.4FZ6MR.rst
M Lib/test/test_syslog.py
M Modules/syslogmodule.c

diff --git a/Lib/test/test_syslog.py b/Lib/test/test_syslog.py
index e6e3ccab35fa9f..f801c51ef1b25d 100644
--- a/Lib/test/test_syslog.py
+++ b/Lib/test/test_syslog.py
@@ -52,6 +52,29 @@ def test_openlog_noargs(self):
         syslog.openlog()
         syslog.syslog('test message from python test_syslog')
 
+    @threading_helper.requires_working_threading()
+    def test_racing_openlog_argv(self):
+        # gh-159864: openlog() without an ident reads sys.argv[0] while
+        # another thread replaces it.
+        argv = ["prog"]
+        nthreads = 32
+
+        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:
+                threading_helper.run_concurrently(
+                    worker_func=[opener] * (nthreads - 1) + [writer],
+                )
+            finally:
+                syslog.closelog()
+
     @threading_helper.requires_working_threading()
     def test_syslog_threaded(self):
         start = threading.Event()
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 7efbf2dad029af..f4416a3ea81aad 100644
--- a/Modules/syslogmodule.c
+++ b/Modules/syslogmodule.c
@@ -107,8 +107,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();

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to