Skip to content

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

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions Include/internal/pycore_pyatomic_ft_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ extern "C" {
_Py_atomic_load_uintptr_acquire(&value)
#define FT_ATOMIC_LOAD_PTR_RELAXED(value) \
_Py_atomic_load_ptr_relaxed(&value)
#define FT_ATOMIC_LOAD_INT(value) _Py_atomic_load_int(&value)
#define FT_ATOMIC_STORE_INT(value, new_value) \
_Py_atomic_store_int(&value, new_value)
#define FT_ATOMIC_LOAD_UINT8(value) \
_Py_atomic_load_uint8(&value)
#define FT_ATOMIC_STORE_UINT8(value, new_value) \
Expand Down Expand Up @@ -70,6 +73,8 @@ extern "C" {
#define FT_ATOMIC_LOAD_PTR_ACQUIRE(value) value
#define FT_ATOMIC_LOAD_UINTPTR_ACQUIRE(value) value
#define FT_ATOMIC_LOAD_PTR_RELAXED(value) value
#define FT_ATOMIC_LOAD_INT(value) value
#define FT_ATOMIC_STORE_INT(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_UINT8(value) value
#define FT_ATOMIC_STORE_UINT8(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_UINT8_RELAXED(value) value
Expand Down
7 changes: 4 additions & 3 deletions Python/fileutils.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Python.h"
#include "pycore_fileutils.h" // fileutils definitions
#include "pycore_runtime.h" // _PyRuntime
#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_*
#include "osdefs.h" // SEP

#include <stdlib.h> // mbstowcs()
Expand Down Expand Up @@ -1502,7 +1503,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
#else

#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
if (ioctl_works != 0 && raise != 0) {
if (raise != 0 && FT_ATOMIC_LOAD_INT(ioctl_works) != 0) {
/* fast-path: ioctl() only requires one syscall */
/* caveat: raise=0 is an indicator that we must be async-signal-safe
* thus avoid using ioctl() so we skip the fast-path. */
Expand All @@ -1512,7 +1513,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
request = FIOCLEX;
err = ioctl(fd, request, NULL);
if (!err) {
ioctl_works = 1;
FT_ATOMIC_STORE_INT(ioctl_works, 1);
return 0;
}

Expand All @@ -1539,7 +1540,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
with EACCES. While FIOCLEX is safe operation it may be
unavailable because ioctl was denied altogether.
This can be the case on Android. */
ioctl_works = 0;
FT_ATOMIC_STORE_INT(ioctl_works, 0);
}
/* fallback to fcntl() if ioctl() does not work */
}
Expand Down
Loading