Skip to content

Commit 0b9d037

Browse files
committed
Preserve errno across thread-state attachment in release builds
Keep the existing _PyThreadState_Attach errno save/restore in release builds as well as debug builds. A release free-threaded attachment can wait for GC or critical-section resumption and change errno, causing valid directory EOF and cold FileFinder operations to raise spurious EAGAIN. Add a public directory/scandir/FileFinder regression under real child-local GC, without native diagnostic modules or third-party code. The identical test fails on current main 3.16 before the four-guard-line change and passes afterwards. Original IO, GC, threading and import suites also pass on the native musl release FT source build. No installed SDK is modified.
1 parent 9d4a35c commit 0b9d037

2 files changed

Lines changed: 82 additions & 4 deletions

File tree

‎Lib/test/test_free_threading/test_os.py‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,90 @@
1010
NTHREADS = 10
1111

1212

13+
def _directory_scan_during_gc(directory, names):
14+
import gc
15+
import importlib.machinery
16+
import json
17+
import os
18+
import threading
19+
import time
20+
21+
expected = set(names)
22+
barrier = threading.Barrier(5)
23+
stop = threading.Event()
24+
errors = []
25+
collections = []
26+
27+
def collect():
28+
barrier.wait(timeout=15)
29+
while not stop.is_set() and len(collections) < 512:
30+
collections.append(gc.collect())
31+
time.sleep(0.0001)
32+
33+
def scan():
34+
barrier.wait(timeout=15)
35+
for _ in range(256):
36+
try:
37+
assert set(os.listdir(directory)) == expected
38+
with os.scandir(directory) as entries:
39+
assert {entry.name for entry in entries} == expected
40+
finder = importlib.machinery.FileFinder(
41+
directory,
42+
(importlib.machinery.SourceFileLoader,
43+
importlib.machinery.SOURCE_SUFFIXES),
44+
)
45+
assert finder.find_spec('fixture') is not None
46+
except BaseException as error:
47+
errors.append(repr(error))
48+
49+
collector = threading.Thread(target=collect)
50+
workers = [threading.Thread(target=scan) for _ in range(4)]
51+
collector.start()
52+
for worker in workers:
53+
worker.start()
54+
try:
55+
for worker in workers:
56+
worker.join(timeout=30)
57+
finally:
58+
stop.set()
59+
collector.join(timeout=15)
60+
assert not collector.is_alive()
61+
assert all(not worker.is_alive() for worker in workers)
62+
assert collections
63+
return json.dumps(errors)
64+
65+
1366
@threading_helper.requires_working_threading()
1467
class TestOs(unittest.TestCase):
68+
def test_directory_io_during_subinterpreter_gc(self):
69+
from concurrent import interpreters
70+
from concurrent.futures import ThreadPoolExecutor
71+
import json
72+
from pathlib import Path
73+
import tempfile
74+
75+
names = ('fixture.py',) + tuple(f'file_{n:02d}.txt'
76+
for n in range(32))
77+
with tempfile.TemporaryDirectory() as directory:
78+
for name in names:
79+
Path(directory, name).write_text('VALUE = 73\n')
80+
for _ in range(16):
81+
children = [interpreters.create() for _ in range(4)]
82+
try:
83+
with ThreadPoolExecutor(max_workers=4) as pool:
84+
futures = [pool.submit(child.call,
85+
_directory_scan_during_gc,
86+
directory, names)
87+
for child in children]
88+
results = [future.result(timeout=90)
89+
for future in futures]
90+
finally:
91+
for child in children:
92+
child.close()
93+
for result in results:
94+
errors = json.loads(result)
95+
self.assertFalse(errors, errors[:3])
96+
1597
@unittest.skipUnless(sysconfig.get_config_var('_Py_HAVE_STRERROR_R'),
1698
'need _Py_HAVE_STRERROR_R macro')
1799
def test_strerror(self):

‎Python/pystate.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,11 +2383,9 @@ tstate_wait_attach(PyThreadState *tstate)
23832383
void
23842384
_PyThreadState_Attach(PyThreadState *tstate)
23852385
{
2386-
#if defined(Py_DEBUG)
23872386
// This is called from PyEval_RestoreThread(). Similar
23882387
// to it, we need to ensure errno doesn't change.
23892388
int err = errno;
2390-
#endif
23912389

23922390
_Py_EnsureTstateNotNULL(tstate);
23932391
if (current_fast_get() != NULL) {
@@ -2434,9 +2432,7 @@ _PyThreadState_Attach(PyThreadState *tstate)
24342432
_PyStats_Attach((_PyThreadStateImpl *)tstate);
24352433
#endif
24362434

2437-
#if defined(Py_DEBUG)
24382435
errno = err;
2439-
#endif
24402436
}
24412437

24422438
static void

0 commit comments

Comments
 (0)