From cdf554c6b374f203d41a1fe46987dbd16277e767 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Wed, 15 Apr 2026 15:41:03 +0200 Subject: [PATCH] [3.12] gh-145548: Use VMADDR_CID_LOCAL in VSOCK socket tests (GH-145589) (GH-145809) * [3.12] gh-145548: Use VMADDR_CID_LOCAL in VSOCK socket tests (GH-145589) (GH-145594) Prefer VMADDR_CID_LOCAL instead of VMADDR_CID_ANY for bind() in the server. Skip the test if bind() fails with EADDRNOTAVAIL. Log vsock CID in test.pythoninfo. (cherry picked from commit 6c8c72f7feb4207c62ac857443943e61977d6a94) (cherry picked from commit 16dbbe5) * [3.13] gh-145548: Don't use VMADDR_CID_LOCAL from `socket` (GH-145735) VMADDR_CID_LOCAL was added to `socekt` in 3.14. The test needs a local constant in setUp(), as in clientSetUp(). (cherry picked from commit e378eda980ff786eb2211738aac0b6f76da88f91) Co-authored-by: Petr Viktorin Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Co-authored-by: Victor Stinner --- Lib/test/pythoninfo.py | 4 ++++ Lib/test/test_socket.py | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Lib/test/pythoninfo.py b/Lib/test/pythoninfo.py index 74ebb5e5b8a292..11affd0cee1de2 100644 --- a/Lib/test/pythoninfo.py +++ b/Lib/test/pythoninfo.py @@ -722,6 +722,10 @@ def collect_test_socket(info_add): if name.startswith('HAVE_')] copy_attributes(info_add, test_socket, 'test_socket.%s', attributes) + # Get IOCTL_VM_SOCKETS_GET_LOCAL_CID of /dev/vsock + cid = test_socket.get_cid() + info_add('test_socket.get_cid', cid) + def collect_support(info_add): try: diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 5ea8c8e62cdafd..b7950f7301cae6 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -472,8 +472,8 @@ def clientTearDown(self): @unittest.skipIf(WSL, 'VSOCK does not work on Microsoft WSL') @unittest.skipUnless(HAVE_SOCKET_VSOCK, 'VSOCK sockets required for this test.') -@unittest.skipUnless(get_cid() != 2, # VMADDR_CID_HOST - "This test can only be run on a virtual guest.") +@unittest.skipIf(get_cid() == getattr(socket, 'VMADDR_CID_HOST', 2), + "This test can only be run on a virtual guest.") class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest): def __init__(self, methodName='runTest'): @@ -483,7 +483,16 @@ def __init__(self, methodName='runTest'): def setUp(self): self.serv = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM) self.addCleanup(self.serv.close) - self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT)) + cid = get_cid() + if cid in (socket.VMADDR_CID_HOST, socket.VMADDR_CID_ANY): + cid = VMADDR_CID_LOCAL + try: + self.serv.bind((cid, VSOCKPORT)) + except OSError as exc: + if exc.errno == errno.EADDRNOTAVAIL: + self.skipTest(f"bind() failed with {exc!r}") + else: + raise self.serv.listen() self.serverExplicitReady() self.serv.settimeout(support.LOOPBACK_TIMEOUT)