Skip to content

Commit 460cc9e

Browse files
authored
gh-117657: Fix TSan reported data race on ioctl_works (#120175)
1 parent d9b4316 commit 460cc9e

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

Python/fileutils.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
15021502
#else
15031503

15041504
#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
1505-
if (ioctl_works != 0 && raise != 0) {
1505+
if (raise != 0 && _Py_atomic_load_int_relaxed(&ioctl_works) != 0) {
15061506
/* fast-path: ioctl() only requires one syscall */
15071507
/* caveat: raise=0 is an indicator that we must be async-signal-safe
15081508
* thus avoid using ioctl() so we skip the fast-path. */
@@ -1512,7 +1512,9 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
15121512
request = FIOCLEX;
15131513
err = ioctl(fd, request, NULL);
15141514
if (!err) {
1515-
ioctl_works = 1;
1515+
if (_Py_atomic_load_int_relaxed(&ioctl_works) == -1) {
1516+
_Py_atomic_store_int_relaxed(&ioctl_works, 1);
1517+
}
15161518
return 0;
15171519
}
15181520

@@ -1539,7 +1541,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
15391541
with EACCES. While FIOCLEX is safe operation it may be
15401542
unavailable because ioctl was denied altogether.
15411543
This can be the case on Android. */
1542-
ioctl_works = 0;
1544+
_Py_atomic_store_int_relaxed(&ioctl_works, 0);
15431545
}
15441546
/* fallback to fcntl() if ioctl() does not work */
15451547
}

Tools/tsan/suppressions_free_threading.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ race_top:new_reference
3535
race_top:set_contains_key
3636
# https://gist.github.com/colesbury/d13d033f413b4ad07929d044bed86c35
3737
race_top:set_discard_entry
38-
race_top:set_inheritable
3938
race_top:_PyDict_CheckConsistency
4039
race_top:_Py_dict_lookup_threadsafe
4140
race_top:_multiprocessing_SemLock_acquire_impl

0 commit comments

Comments
 (0)