Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GIL warnings and a few thread-safety issues in free-threaded CPython #2428

Merged
merged 2 commits into from
Jun 24, 2024

Conversation

colesbury
Copy link
Contributor

Summary

Description

This adds PyUnstable_Module_SetGIL calls to allow psutil to run with the GIL disabled in the CPython free-threading builds. This also fixes a few thread-safety that could cause problems with the GIL disabled:

  • 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.

- 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.

Signed-off-by: Sam Gross <[email protected]>
return py_retlist;

error:
free(argv);
Copy link
Owner

@giampaolo giampaolo Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be?

if (argv != NULL)
     free(argv);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

free() handles NULL pointers, but I can add the if (argv != NULL) guard if you prefer that style.

https://en.cppreference.com/w/c/memory/free#:~:text=If%20ptr%20is%20a%20null%20pointer%2C%20the%20function%20does%20nothing

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.
Side story: years ago psutil received a CVE due to a double free(), so I believe I started using this style since then.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double free is a separate issue: to avoid that you can use the coding style:

free(p);
p = NULL;

@giampaolo giampaolo merged commit 73af407 into giampaolo:master Jun 24, 2024
13 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

psutil fails (segfault) on Mac OS in the free-threaded (no GIL) version of Python 3.13
3 participants