Skip to content

Commit e94dbe4

Browse files
authored
pythongh-119461: Fix ThreadedVSOCKSocketStreamTest (python#119465)
Add socket.VMADDR_CID_LOCAL constant. Fix ThreadedVSOCKSocketStreamTest: if get_cid() returns the host address or the "any" address, use the local communication address (loopback): VMADDR_CID_LOCAL. On Linux 6.9, apparently, the /dev/vsock device is now available but get_cid() returns VMADDR_CID_ANY (-1).
1 parent be1dfcc commit e94dbe4

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

Lib/test/test_socket.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ def _have_socket_qipcrtr():
160160

161161
def _have_socket_vsock():
162162
"""Check whether AF_VSOCK sockets are supported on this host."""
163-
ret = get_cid() is not None
164-
return ret
163+
cid = get_cid()
164+
return (cid is not None)
165165

166166

167167
def _have_socket_bluetooth():
@@ -520,8 +520,6 @@ def clientTearDown(self):
520520
@unittest.skipIf(WSL, 'VSOCK does not work on Microsoft WSL')
521521
@unittest.skipUnless(HAVE_SOCKET_VSOCK,
522522
'VSOCK sockets required for this test.')
523-
@unittest.skipUnless(get_cid() != 2,
524-
"This test can only be run on a virtual guest.")
525523
class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest):
526524

527525
def __init__(self, methodName='runTest'):
@@ -543,6 +541,9 @@ def clientSetUp(self):
543541
self.cli = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
544542
self.addCleanup(self.cli.close)
545543
cid = get_cid()
544+
if cid in (socket.VMADDR_CID_HOST, socket.VMADDR_CID_ANY):
545+
# gh-119461: Use the local communication address (loopback)
546+
cid = socket.VMADDR_CID_LOCAL
546547
self.cli.connect((cid, VSOCKPORT))
547548

548549
def testStream(self):
@@ -2515,6 +2516,7 @@ def testVSOCKConstants(self):
25152516
socket.SO_VM_SOCKETS_BUFFER_MAX_SIZE
25162517
socket.VMADDR_CID_ANY
25172518
socket.VMADDR_PORT_ANY
2519+
socket.VMADDR_CID_LOCAL
25182520
socket.VMADDR_CID_HOST
25192521
socket.VM_SOCKETS_INVALID_VERSION
25202522
socket.IOCTL_VM_SOCKETS_GET_LOCAL_CID
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``socket.VMADDR_CID_LOCAL`` constant. Patch by Victor Stinner.

Modules/socketmodule.c

+1
Original file line numberDiff line numberDiff line change
@@ -7596,6 +7596,7 @@ socket_exec(PyObject *m)
75967596
ADD_INT_CONST(m, "SO_VM_SOCKETS_BUFFER_MAX_SIZE", 2);
75977597
ADD_INT_CONST(m, "VMADDR_CID_ANY", 0xffffffff);
75987598
ADD_INT_CONST(m, "VMADDR_PORT_ANY", 0xffffffff);
7599+
ADD_INT_CONST(m, "VMADDR_CID_LOCAL", 1);
75997600
ADD_INT_CONST(m, "VMADDR_CID_HOST", 2);
76007601
ADD_INT_CONST(m, "VM_SOCKETS_INVALID_VERSION", 0xffffffff);
76017602
ADD_INT_CONST(m, "IOCTL_VM_SOCKETS_GET_LOCAL_CID", _IO(7, 0xb9));

0 commit comments

Comments
 (0)