]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-117440: Make `syslog` thread-safe in free-threaded builds (#117441)
authorSam Gross <colesbury@gmail.com>
Tue, 2 Apr 2024 14:44:26 +0000 (10:44 -0400)
committerGitHub <noreply@github.com>
Tue, 2 Apr 2024 14:44:26 +0000 (10:44 -0400)
Use critical sections to protect access to the syslog module.

Modules/clinic/syslogmodule.c.h
Modules/syslogmodule.c

index 58b0ea57b065b393c52f2510e65fd0158831cae3..77cf24ea5ba9ca0898c190fe481d86d206486c6d 100644 (file)
@@ -6,6 +6,7 @@ preserve
 #  include "pycore_gc.h"          // PyGC_Head
 #  include "pycore_runtime.h"     // _Py_ID()
 #endif
+#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
 #include "pycore_modsupport.h"    // _PyArg_UnpackKeywords()
 
 PyDoc_STRVAR(syslog_openlog__doc__,
@@ -88,7 +89,9 @@ syslog_openlog(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObje
         goto exit;
     }
 skip_optional_pos:
+    Py_BEGIN_CRITICAL_SECTION(module);
     return_value = syslog_openlog_impl(module, ident, logopt, facility);
+    Py_END_CRITICAL_SECTION();
 
 exit:
     return return_value;
@@ -129,7 +132,9 @@ syslog_syslog(PyObject *module, PyObject *args)
             PyErr_SetString(PyExc_TypeError, "syslog.syslog requires 1 to 2 arguments");
             goto exit;
     }
+    Py_BEGIN_CRITICAL_SECTION(module);
     return_value = syslog_syslog_impl(module, group_left_1, priority, message);
+    Py_END_CRITICAL_SECTION();
 
 exit:
     return return_value;
@@ -150,7 +155,13 @@ syslog_closelog_impl(PyObject *module);
 static PyObject *
 syslog_closelog(PyObject *module, PyObject *Py_UNUSED(ignored))
 {
-    return syslog_closelog_impl(module);
+    PyObject *return_value = NULL;
+
+    Py_BEGIN_CRITICAL_SECTION(module);
+    return_value = syslog_closelog_impl(module);
+    Py_END_CRITICAL_SECTION();
+
+    return return_value;
 }
 
 PyDoc_STRVAR(syslog_setlogmask__doc__,
@@ -251,4 +262,4 @@ syslog_LOG_UPTO(PyObject *module, PyObject *arg)
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=86ca2fd84b2da98e input=a9049054013a1b77]*/
+/*[clinic end generated code: output=8d25899bd31969d3 input=a9049054013a1b77]*/
index 62c7816f891ee281b0f9d579d0cf5a3334f12e53..cb3f2b03990cb84a817271f1f00e41e39bffe1e2 100644 (file)
@@ -132,6 +132,7 @@ syslog_get_argv(void)
 
 
 /*[clinic input]
+@critical_section
 syslog.openlog
 
     ident: unicode = NULL
@@ -144,7 +145,7 @@ Set logging options of subsequent syslog() calls.
 static PyObject *
 syslog_openlog_impl(PyObject *module, PyObject *ident, long logopt,
                     long facility)
-/*[clinic end generated code: output=5476c12829b6eb75 input=8a987a96a586eee7]*/
+/*[clinic end generated code: output=5476c12829b6eb75 input=ee700b8786f81c23]*/
 {
     // Since the sys.openlog changes the process level state of syslog library,
     // this operation is only allowed for the main interpreter.
@@ -189,6 +190,7 @@ syslog_openlog_impl(PyObject *module, PyObject *ident, long logopt,
 
 
 /*[clinic input]
+@critical_section
 syslog.syslog
 
     [
@@ -205,7 +207,7 @@ Send the string message to the system logger.
 static PyObject *
 syslog_syslog_impl(PyObject *module, int group_left_1, int priority,
                    const char *message)
-/*[clinic end generated code: output=c3dbc73445a0e078 input=ac83d92b12ea3d4e]*/
+/*[clinic end generated code: output=c3dbc73445a0e078 input=6588ddb0b113af8e]*/
 {
     if (PySys_Audit("syslog.syslog", "is", priority, message) < 0) {
         return NULL;
@@ -243,6 +245,7 @@ syslog_syslog_impl(PyObject *module, int group_left_1, int priority,
 
 
 /*[clinic input]
+@critical_section
 syslog.closelog
 
 Reset the syslog module values and call the system library closelog().
@@ -250,7 +253,7 @@ Reset the syslog module values and call the system library closelog().
 
 static PyObject *
 syslog_closelog_impl(PyObject *module)
-/*[clinic end generated code: output=97890a80a24b1b84 input=fb77a54d447acf07]*/
+/*[clinic end generated code: output=97890a80a24b1b84 input=167f489868bd5a72]*/
 {
     // Since the sys.closelog changes the process level state of syslog library,
     // this operation is only allowed for the main interpreter.