Skip to content

Commit c08f68d

Browse files
generatedunixname1734921407115435meta-codesync[bot]
authored andcommitted
Sync pre-release CPython main branch from GitHub (2026-07-16)
Summary: Imported python/cpython `3.16.0a0` from upstream rev [`f62050d`](https://www.github.com/python/cpython/commit/f62050d65743f0c895f7e6d665936c4e86aa39d5) (committed 2026-07-16 17:37:00+00:00). # Commit Info - Base: (`3.16.0a0`) - [`b4662e8`](https://www.github.com/python/cpython/commit/b4662e8e3b60a264486130a171047c7381f993f0) (commit date: 2026-07-15 18:05:17+00:00) - Imported: (`3.16.0a0`) - [`f62050d`](https://www.github.com/python/cpython/commit/f62050d65743f0c895f7e6d665936c4e86aa39d5) (commit date: 2026-07-16 17:37:00+00:00) # Noteworthy file changes - Low-signal files (2 added) (NEWS.d, docs, .github) Complete list of added/removed files: https://www.internalfb.com/intern/everpaste/?color=0&handle=GEe_9yWrQAYQhvAIAND7GnewXbodbr0LAAAz Differential Revision: D112487879 fbshipit-source-id: 5954c65fa8fa5df9a541a6ed6244b5f85293526e
1 parent 2a8166c commit c08f68d

5 files changed

Lines changed: 51 additions & 1 deletion

File tree

Lib/asyncio/selector_events.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,9 @@ async def sock_accept(self, sock):
714714
return await fut
715715

716716
def _sock_accept(self, fut, sock):
717+
# gh-153761: _sock_accept must not scheduled with already cancelled future
718+
if fut.done():
719+
return
717720
fd = sock.fileno()
718721
try:
719722
conn, address = sock.accept()

Lib/test/test_asyncio/test_sock_lowlevel.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import socket
22
import asyncio
3+
import select
34
import sys
45
import unittest
56

@@ -257,6 +258,38 @@ async def _basetest_sock_connect_racing(self, listener, sock):
257258

258259
self.skipTest(skip_reason)
259260

261+
async def _basetest_sock_accept_racing(self, listener, sock):
262+
# gh-153761: cancelling sock_accept() must not let a scheduled
263+
# _sock_accept run on the cancelled future.
264+
listener.setblocking(False)
265+
listener.bind(('127.0.0.1', 0))
266+
listener.listen(1)
267+
addr = listener.getsockname()
268+
269+
errors = []
270+
self.loop.set_exception_handler(lambda loop, ctx: errors.append(ctx))
271+
task = asyncio.create_task(self.loop.sock_accept(listener))
272+
await asyncio.sleep(0)
273+
274+
sock.connect(addr)
275+
select.select([listener], [], [], support.SHORT_TIMEOUT)
276+
277+
self.loop.call_soon(task.cancel)
278+
await asyncio.sleep(0)
279+
await asyncio.sleep(0)
280+
281+
with self.assertRaises(asyncio.CancelledError):
282+
await task
283+
self.assertEqual(errors, [])
284+
285+
# The pending connection must survive
286+
conn, conn_addr = await self.loop.sock_accept(listener)
287+
with conn:
288+
self.assertEqual(conn_addr, sock.getsockname())
289+
sock.setblocking(False)
290+
await self.loop.sock_sendall(conn, b'ping')
291+
self.assertEqual(await self.loop.sock_recv(sock, 4), b'ping')
292+
260293
def test_sock_client_racing(self):
261294
with test_utils.run_test_server() as httpd:
262295
sock = socket.socket()
@@ -280,6 +313,16 @@ def test_sock_client_connect_racing(self):
280313
self.loop.run_until_complete(asyncio.wait_for(
281314
self._basetest_sock_connect_racing(listener, sock), 10))
282315

316+
def test_sock_accept_racing(self):
317+
if sys.platform == 'win32':
318+
if isinstance(self.loop, asyncio.ProactorEventLoop):
319+
raise unittest.SkipTest('Not relevant to ProactorEventLoop')
320+
listener = socket.socket()
321+
sock = socket.socket()
322+
with listener, sock:
323+
self.loop.run_until_complete(asyncio.wait_for(
324+
self._basetest_sock_accept_racing(listener, sock), 10))
325+
283326
async def _basetest_huge_content(self, address):
284327
sock = socket.socket()
285328
sock.setblocking(False)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a potential deadlock in :c:func:`PyUnicode_InternFromString` and other
2+
interning functions in the :term:`free-threaded build` when called from C++
3+
static local initializers.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix cancelling :meth:`asyncio.loop.sock_accept` dropping a pending connection.

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14722,7 +14722,7 @@ intern_common(PyInterpreterState *interp, PyObject *s /* stolen */,
1472214722
}
1472314723
#endif
1472414724

14725-
FT_MUTEX_LOCK(INTERN_MUTEX);
14725+
FT_MUTEX_LOCK_FLAGS(INTERN_MUTEX, _Py_LOCK_DONT_DETACH);
1472614726
PyObject *t;
1472714727
{
1472814728
int res = PyDict_SetDefaultRef(interned, s, s, &t);

0 commit comments

Comments
 (0)