Visitar URL original
gh-158929: Fix subinterpreter thread safety issues in `syslog` by ZeroIntensity · Pull Request #159000 · python/cpython · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix subinterpreter thread-safety issues in syslog.
  • Loading branch information
ZeroIntensity committed Oct 8, 2026
commit b0d71c2c19000aac3fc398cd0333e71fd8a71091
16 changes: 6 additions & 10 deletions Modules/syslogmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module syslog
/* only one instance, only one syslog, so globals should be ok,
* these fields are writable from the main interpreter only. */
static PyObject *S_ident_o = NULL; // identifier, held by openlog()
static char S_log_open = 0;
static int S_log_open = 0;

static inline int
is_main_interpreter(void)
Expand Down Expand Up @@ -189,8 +189,8 @@ syslog_openlog_impl(PyObject *module, PyObject *ident, long logopt,
}

openlog(ident_str, logopt, facility);
S_log_open = 1;
Py_XSETREF(S_ident_o, ident);
_Py_atomic_store_int(&S_log_open, 1);

Py_RETURN_NONE;
}
Expand Down Expand Up @@ -222,7 +222,7 @@ syslog_syslog_impl(PyObject *module, int group_left_1, int priority,
}

/* if log is not opened, open it now */
if (!S_log_open) {
if (!_Py_atomic_load_int(&S_log_open)) {
if (!is_main_interpreter()) {
PyErr_SetString(PyExc_RuntimeError, "subinterpreter can't use syslog.syslog() "
"until the syslog is opened by the main interpreter");
Expand All @@ -235,10 +235,6 @@ syslog_syslog_impl(PyObject *module, int group_left_1, int priority,
Py_DECREF(openlog_ret);
}

/* Incref ident, because it can be decrefed if syslog.openlog() is
* called when the GIL is released.
*/
PyObject *ident = Py_XNewRef(S_ident_o);
#ifdef __APPLE__
// gh-98178: On macOS, libc syslog() is not thread-safe
syslog(priority, "%s", message);
Expand All @@ -247,7 +243,6 @@ syslog_syslog_impl(PyObject *module, int group_left_1, int priority,
syslog(priority, "%s", message);
Py_END_ALLOW_THREADS;
#endif
Py_XDECREF(ident);
Py_RETURN_NONE;
}

Expand All @@ -273,10 +268,11 @@ syslog_closelog_impl(PyObject *module)
if (PySys_Audit("syslog.closelog", NULL) < 0) {
return NULL;
}
if (S_log_open) {

int was_open = _Py_atomic_exchange_int(&S_log_open, 0);
if (was_open) {
closelog();
Py_CLEAR(S_ident_o);
S_log_open = 0;
}
Py_RETURN_NONE;
}
Expand Down