From 46afba7b9324bc9492c3527d0fe47dd74f1f598c Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 16 May 2026 11:37:34 +0300 Subject: [PATCH 1/3] gh-149816: Fix a race condition in `_PyBytes_FromList` with free-threading (#149909) --- .../2026-05-16-11-03-54.gh-issue-149816.X_gqMT.rst | 1 + Objects/bytesobject.c | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-05-16-11-03-54.gh-issue-149816.X_gqMT.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-05-16-11-03-54.gh-issue-149816.X_gqMT.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-05-16-11-03-54.gh-issue-149816.X_gqMT.rst new file mode 100644 index 000000000000000..d35f0857a1aefe8 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-05-16-11-03-54.gh-issue-149816.X_gqMT.rst @@ -0,0 +1 @@ +Fix a race condition in ``_PyBytes_FromList`` in free-threading mode. diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index cd8417e25839161..6dd32b7079a765b 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -11,6 +11,7 @@ #include "pycore_global_objects.h"// _Py_GET_GLOBAL_OBJECT() #include "pycore_initconfig.h" // _PyStatus_OK() #include "pycore_long.h" // _PyLong_DigitValue +#include "pycore_list.h" // _PyList_GetItemRef #include "pycore_object.h" // _PyObject_GC_TRACK #include "pycore_pymem.h" // PYMEM_CLEANBYTE #include "pycore_strhex.h" // _Py_strhex_with_sep() @@ -2991,8 +2992,10 @@ _PyBytes_FromList(PyObject *x) size = _PyBytesWriter_GetAllocated(writer); for (Py_ssize_t i = 0; i < PyList_GET_SIZE(x); i++) { - PyObject *item = PyList_GET_ITEM(x, i); - Py_INCREF(item); + PyObject *item = _PyList_GetItemRef((PyListObject *)x, i); + if (item == NULL) { + goto error; + } Py_ssize_t value = PyNumber_AsSsize_t(item, NULL); Py_DECREF(item); if (value == -1 && PyErr_Occurred()) From 436a6f289402553ca2bf1308881d85861e5d8a22 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 16 May 2026 10:50:55 +0200 Subject: [PATCH 2/3] gh-149879: Fix test_os on Cygwin (#149910) --- Lib/test/test_os/test_os.py | 26 +++++++++++++++++++------- Lib/test/test_os/test_posix.py | 16 +++++++++++++--- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index 7e670e5a139d999..e71c28424e095f2 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -1867,7 +1867,9 @@ def test_walk_bad_dir2(self): walk_it = self.walk(self.tmp1_path, follow_symlinks=True) if self.is_fwalk: - self.assertRaises(NotADirectoryError, next, walk_it) + with self.assertRaises(OSError) as cm: + next(walk_it) + self.assertIn(cm.exception.errno, (errno.ENOTDIR, errno.EINVAL)) self.assertRaises(StopIteration, next, walk_it) @unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') @@ -2269,6 +2271,8 @@ def test_chown_with_root(self): @requires_non_root_user @unittest.skipUnless(len(all_users) > 1, "test needs and more than one user") + @unittest.skipIf(sys.platform == 'cygwin', + 'chown() can set any uid on Cygwin') def test_chown_without_permission(self): uid_1, uid_2 = all_users[:2] gid = os.stat(os_helper.TESTFN).st_gid @@ -4051,10 +4055,11 @@ def test_timerfd_non_blocking(self): initial_expiration = 0.1 os.timerfd_settime(fd, initial=initial_expiration, interval=0) - # read() raises OSError with errno is EAGAIN for non-blocking timer. - with self.assertRaises(OSError) as ctx: - self.read_count_signaled(fd) - self.assertEqual(ctx.exception.errno, errno.EAGAIN) + if sys.platform != 'cygwin': + # read() raises OSError with errno is EAGAIN for non-blocking timer. + with self.assertRaises(OSError) as ctx: + self.read_count_signaled(fd) + self.assertEqual(ctx.exception.errno, errno.EAGAIN) # Wait more than 0.1 seconds time.sleep(initial_expiration + 0.1) @@ -4235,12 +4240,19 @@ def test_timerfd_ns_initval(self): # 2nd call next_expiration_ns, interval_ns2 = os.timerfd_settime_ns(fd, initial=initial_expiration_ns, interval=interval_ns) - self.assertEqual(interval_ns2, interval_ns) + CYGWIN = (sys.platform == 'cygwin') + if not CYGWIN: + self.assertEqual(interval_ns2, interval_ns) + else: + self.assertEqual(interval_ns2, 0) self.assertEqual(next_expiration_ns, initial_expiration_ns) # timerfd_gettime next_expiration_ns, interval_ns2 = os.timerfd_gettime_ns(fd) - self.assertEqual(interval_ns2, interval_ns) + if not CYGWIN: + self.assertEqual(interval_ns2, interval_ns) + else: + self.assertEqual(interval_ns2, 0) self.assertLessEqual(next_expiration_ns, initial_expiration_ns) self.assertAlmostEqual(next_expiration_ns, initial_expiration_ns, delta=limit_error) diff --git a/Lib/test/test_os/test_posix.py b/Lib/test/test_os/test_posix.py index 0e8495a4eff2ed4..d0a662a091829e8 100644 --- a/Lib/test/test_os/test_posix.py +++ b/Lib/test/test_os/test_posix.py @@ -142,8 +142,8 @@ def test_initgroups(self): self.assertRaises(TypeError, posix.initgroups, "foo", 3, object()) # If a non-privileged user invokes it, it should fail with OSError - # EPERM. - if os.getuid() != 0: + # EPERM. On Cygwin, initgroups(name, 13) does not fail. + if os.getuid() != 0 and sys.platform != 'cygwin': try: name = pwd.getpwuid(posix.getuid()).pw_name except KeyError: @@ -597,7 +597,9 @@ def test_sysconf(self): posix.sysconf(1.23) arg_max = posix.sysconf("SC_ARG_MAX") - self.assertGreater(arg_max, 0) + # SC_ARG_MAX is -1 on Cygwin + if sys.platform != 'cygwin': + self.assertGreater(arg_max, 0) self.assertEqual( posix.sysconf(posix.sysconf_names["SC_ARG_MAX"]), arg_max) @@ -1943,6 +1945,14 @@ def test_no_such_executable(self): # directories in the $PATH that are not accessible. except (FileNotFoundError, PermissionError) as exc: self.assertEqual(exc.filename, no_such_executable) + + # On Cygwin, os.posix_spawn() creates a child process even if the + # executable doesn't exist. We have to reap this process. + if sys.platform == 'cygwin': + for _ in support.sleeping_retry(support.SHORT_TIMEOUT): + pid, status = os.waitpid(-1, os.WNOHANG) + if pid != 0: + break else: pid2, status = os.waitpid(pid, 0) self.assertEqual(pid2, pid) From a7ed0c9e1dee1397e80e2e0d90d110155da2bc30 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 16 May 2026 12:02:19 +0200 Subject: [PATCH 3/3] gh-149879: Fix test_socket on Cygwin (#149913) Disable SCM_RIGHTS, recvmsg(), sendmsg() and sethostname() on Cygwin. --- Lib/test/test_socket.py | 7 ++++--- Modules/socketmodule.c | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 47830d0e9645efc..5b4c88793f8e234 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1454,7 +1454,7 @@ def testIPv6toString(self): assertInvalid('1:2:3:4:5:6:') assertInvalid('1:2:3:4:5:6:7:8:0') # bpo-29972: inet_pton() doesn't fail on AIX - if not AIX: + if not AIX and sys.platform != 'cygwin': assertInvalid('1:2:3:4:5:6:7:8:') self.assertEqual(b'\x00' * 12 + b'\xfe\x2a\x17\x40', @@ -2001,7 +2001,8 @@ def test_getfqdn_filter_localhost(self): self.assertEqual(socket.getfqdn(), socket.getfqdn("::")) @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') - @unittest.skipIf(sys.platform == 'win32', 'does not work on Windows') + @unittest.skipIf(sys.platform in ('win32', 'cygwin'), + 'does not work on Windows') @unittest.skipIf(AIX, 'Symbolic scope id does not work') @unittest.skipUnless(hasattr(socket, 'if_nameindex'), "test needs socket.if_nameindex()") @support.skip_android_selinux('if_nameindex') @@ -2035,7 +2036,7 @@ def test_getaddrinfo_ipv6_scopeid_numeric(self): self.assertEqual(sockaddr, ('ff02::1de:c0:face:8d', 1234, 0, ifindex)) @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') - @unittest.skipIf(sys.platform == 'win32', 'does not work on Windows') + @unittest.skipIf(sys.platform in ('win32', 'cygwin'), 'does not work on Windows') @unittest.skipIf(AIX, 'Symbolic scope id does not work') @unittest.skipUnless(hasattr(socket, 'if_nameindex'), "test needs socket.if_nameindex()") @support.skip_android_selinux('if_nameindex') diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index f5993fc8fdaab28..442834a556efc66 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -654,10 +654,6 @@ class _socket.socket "PySocketSockObject *" "clinic_state()->sock_type" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=2db2489bd2219fd8]*/ -#define clinic_state() (find_module_state_by_def(type)) -#include "clinic/socketmodule.c.h" -#undef clinic_state - /* XXX There's a problem here: *static* functions are not supposed to have a Py prefix (or use CapitalizedWords). Later... */ @@ -688,6 +684,18 @@ class _socket.socket "PySocketSockObject *" "clinic_state()->sock_type" #define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0) #endif +// SCM_RIGHTS, sendmsg(), recvmsg() and sethostname() don't work properly on +// Cygwin: disable these features. +#ifdef __CYGWIN__ +# undef CMSG_LEN +# undef SCM_RIGHTS +# undef HAVE_SETHOSTNAME +#endif + +#define clinic_state() (find_module_state_by_def(type)) +#include "clinic/socketmodule.c.h" +#undef clinic_state + static PyObject* select_error(void) {