Skip to content
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
17 changes: 17 additions & 0 deletions Lib/test/test_free_threading/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug report is about concurrent.interpreters (sub-interpreters), but this test uses regular threads (without sub-interpreters). I'm not sure that the test checks for non regression.

On Linux at least, errno is per-thread, and so calling gc.collect()+os.listdir() in parallel (on Free Threading) should be just fine without your fix (don't fail).


@unittest.skipUnless(sysconfig.get_config_var('_Py_HAVE_STRERROR_R'),
'need _Py_HAVE_STRERROR_R macro')
def test_strerror(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 0 additions & 8 deletions Python/ceval_gil.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
17 changes: 12 additions & 5 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include "pycore_time.h" // _PyTime_Init()
#include "pycore_uniqueid.h" // _PyObject_FinalizePerThreadRefcounts()

#ifdef MS_WINDOWS
# include <windows.h>
#endif


/* --------------------------------------------------------------------------
CAUTION
Expand Down Expand Up @@ -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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyEval_RestoreThread() does:

    int err = GetLastError();
(...)
    SetLastError(err);

If we save/restore errno in _PyThreadState_Attach(), I would suggest to also save/restore the Windows last error (move the code from PyEval_RestoreThread()).


_Py_EnsureTstateNotNULL(tstate);
if (current_fast_get() != NULL) {
Expand Down Expand Up @@ -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
}

Expand Down
Loading