Skip to content

gh-117657: Fix data races for set_inheritable in fileutils.c with free-threaded builds - #120458

Closed
aisk wants to merge 1 commit into
python:mainfrom
aisk:tsan-set-inheritable
Closed

aisk wants to merge 1 commit into
python:mainfrom
aisk:tsan-set-inheritable

Conversation

@aisk

@aisk aisk commented Jun 13, 2024 •

Copy link
Copy Markdown
Member

ioctl_works in set_inheritable function is a static variable which has three possible values:

Value Meaning
-1 Initial value when the process starts.
0 ioctl cannot be used for this functionality; fcntl is used as a fallback.
1 ioctl works in the current environment, and we should continue to use it.

When it is 0, using ioctl as a fast path will always be skipped.

When it is -1 or 1, ioctl will be used as a fast path to perform the functionality, and it will be set to 0 or 1 based on the result of whether ioctl raises an error or not. And if ioctl failed, use fnctl as a slow path fallback.

Following this logic, it is safe to call set_inheritable with multiple threads in a free-threaded build. The fast path can be safely used even if it leads to failure. Therefore, we can simply use atomic operations to read or write ioctl_works's value to resolve data races.

@aisk

aisk commented Jun 13, 2024

Copy link
Copy Markdown
Member Author

Sorry, just noticed that this is duplicated with #120175.

@aisk aisk closed this Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant