Skip to content

Commit

Permalink
Fix GIL warnings and a few thread-safety issues in free-threaded CPython
Browse files Browse the repository at this point in the history
- The temporary `argv` C array is no longer global in OpenBSD's
  proc_cmdline
- The `maxcpus` variable is no longer global in FreeBSD's per_cpu_times.
  • Loading branch information
colesbury committed Jun 24, 2024
1 parent 9c84c85 commit f0a5465
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions psutil/_psutil_aix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,9 @@ void init_psutil_aix(void)
PyObject *module = PyModule_Create(&moduledef);
#else
PyObject *module = Py_InitModule("_psutil_aix", PsutilMethods);
#endif
#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
#endif
PyModule_AddIntConstant(module, "version", PSUTIL_VERSION);

Expand Down
4 changes: 4 additions & 0 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ static PyMethodDef mod_methods[] = {
if (mod == NULL)
INITERR;

#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
#endif

if (PyModule_AddIntConstant(mod, "version", PSUTIL_VERSION)) INITERR;
// process status constants

Expand Down
4 changes: 4 additions & 0 deletions psutil/_psutil_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ static PyMethodDef mod_methods[] = {
if (mod == NULL)
INITERR;

#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
#endif

if (PyModule_AddIntConstant(mod, "version", PSUTIL_VERSION)) INITERR;
if (PyModule_AddIntConstant(mod, "DUPLEX_HALF", DUPLEX_HALF)) INITERR;
if (PyModule_AddIntConstant(mod, "DUPLEX_FULL", DUPLEX_FULL)) INITERR;
Expand Down
4 changes: 4 additions & 0 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ static PyMethodDef mod_methods[] = {
if (mod == NULL)
INITERR;

#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
#endif

if (psutil_setup() != 0)
INITERR;

Expand Down
4 changes: 4 additions & 0 deletions psutil/_psutil_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,10 @@ static PyMethodDef mod_methods[] = {
if (mod == NULL)
INITERR;

#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
#endif

#if defined(PSUTIL_BSD) || \
defined(PSUTIL_OSX) || \
defined(PSUTIL_SUNOS) || \
Expand Down
4 changes: 4 additions & 0 deletions psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,10 @@ void init_psutil_sunos(void)
if (module == NULL)
INITERROR;

#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED);
#endif

if (psutil_setup() != 0)
INITERROR;

Expand Down
4 changes: 4 additions & 0 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ void init_psutil_windows(void)
if (module == NULL)
INITERROR;

#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED);
#endif

if (psutil_setup() != 0)
INITERROR;
if (psutil_set_se_debug() != 0)
Expand Down
2 changes: 1 addition & 1 deletion psutil/arch/freebsd/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ For reference, here's the git history with original(ish) implementations:

PyObject *
psutil_per_cpu_times(PyObject *self, PyObject *args) {
static int maxcpus;
int maxcpus;
int mib[2];
int ncpu;
size_t len;
Expand Down
4 changes: 3 additions & 1 deletion psutil/arch/openbsd/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ PyObject *
psutil_proc_cmdline(PyObject *self, PyObject *args) {
pid_t pid;
int mib[4];
static char **argv;
char **argv = NULL;
char **p;
size_t argv_size = 128;
PyObject *py_retlist = PyList_New(0);
Expand Down Expand Up @@ -189,9 +189,11 @@ psutil_proc_cmdline(PyObject *self, PyObject *args) {
Py_DECREF(py_arg);
}

free(argv);
return py_retlist;

error:
free(argv);
Py_XDECREF(py_arg);
Py_DECREF(py_retlist);
return NULL;
Expand Down

0 comments on commit f0a5465

Please sign in to comment.