diff --git a/Lib/test/test_free_threading/test_os.py b/Lib/test/test_free_threading/test_os.py index 72582da29015e93..5e0cb81630d58ab 100644 --- a/Lib/test/test_free_threading/test_os.py +++ b/Lib/test/test_free_threading/test_os.py @@ -12,6 +12,23 @@ @threading_helper.requires_working_threading() class TestOs(unittest.TestCase): + def test_listdir_during_gc(self): + import gc + from pathlib import Path + import tempfile + + with tempfile.TemporaryDirectory() as directory: + expected = {f"entry_{n}" for n in range(32)} + for name in expected: + Path(directory, name).touch() + + def worker(): + for _ in range(256): + gc.collect() + self.assertEqual(set(os.listdir(directory)), expected) + + run_concurrently(worker, nthreads=4) + @unittest.skipUnless(sysconfig.get_config_var('_Py_HAVE_STRERROR_R'), 'need _Py_HAVE_STRERROR_R macro') def test_strerror(self): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-10-18-17-09.gh-issue-161769.wL6r9A.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-10-18-17-09.gh-issue-161769.wL6r9A.rst new file mode 100644 index 000000000000000..f2425c70daa5cbd --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-10-18-17-09.gh-issue-161769.wL6r9A.rst @@ -0,0 +1,3 @@ +Preserve ``errno`` across thread-state attachment in release builds, +preventing spurious directory I/O errors on free-threaded builds during +concurrent garbage collection. diff --git a/Python/ceval_gil.c b/Python/ceval_gil.c index 91c75cdd2ed4133..30aadae0db4435c 100644 --- a/Python/ceval_gil.c +++ b/Python/ceval_gil.c @@ -651,16 +651,8 @@ PyEval_SaveThread(void) void PyEval_RestoreThread(PyThreadState *tstate) { -#ifdef MS_WINDOWS - int err = GetLastError(); -#endif - _Py_EnsureTstateNotNULL(tstate); _PyThreadState_Attach(tstate); - -#ifdef MS_WINDOWS - SetLastError(err); -#endif } diff --git a/Python/pystate.c b/Python/pystate.c index ce7197c4e054283..4bf39490766baa5 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -27,6 +27,10 @@ #include "pycore_time.h" // _PyTime_Init() #include "pycore_uniqueid.h" // _PyObject_FinalizePerThreadRefcounts() +#ifdef MS_WINDOWS +# include +#endif + /* -------------------------------------------------------------------------- CAUTION @@ -2383,11 +2387,13 @@ tstate_wait_attach(PyThreadState *tstate) void _PyThreadState_Attach(PyThreadState *tstate) { -#if defined(Py_DEBUG) - // This is called from PyEval_RestoreThread(). Similar - // to it, we need to ensure errno doesn't change. - int err = errno; + // Reattachment may wait for GC and change errno, even after a successful + // system call (gh-161769). Preserve the caller's error state in release + // builds as well as debug builds. +#ifdef MS_WINDOWS + DWORD last_error = GetLastError(); #endif + int err = errno; _Py_EnsureTstateNotNULL(tstate); if (current_fast_get() != NULL) { @@ -2434,8 +2440,9 @@ _PyThreadState_Attach(PyThreadState *tstate) _PyStats_Attach((_PyThreadStateImpl *)tstate); #endif -#if defined(Py_DEBUG) errno = err; +#ifdef MS_WINDOWS + SetLastError(last_error); #endif }