Skip to content

Commit

Permalink
gh-119461: Fix ThreadedVSOCKSocketStreamTest (#119465)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
vstinner authored May 23, 2024
1 parent be1dfcc commit e94dbe4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ def _have_socket_qipcrtr():

def _have_socket_vsock():
"""Check whether AF_VSOCK sockets are supported on this host."""
ret = get_cid() is not None
return ret
cid = get_cid()
return (cid is not None)


def _have_socket_bluetooth():
Expand Down Expand Up @@ -520,8 +520,6 @@ 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,
"This test can only be run on a virtual guest.")
class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest):

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

def testStream(self):
Expand Down Expand Up @@ -2515,6 +2516,7 @@ def testVSOCKConstants(self):
socket.SO_VM_SOCKETS_BUFFER_MAX_SIZE
socket.VMADDR_CID_ANY
socket.VMADDR_PORT_ANY
socket.VMADDR_CID_LOCAL
socket.VMADDR_CID_HOST
socket.VM_SOCKETS_INVALID_VERSION
socket.IOCTL_VM_SOCKETS_GET_LOCAL_CID
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``socket.VMADDR_CID_LOCAL`` constant. Patch by Victor Stinner.
1 change: 1 addition & 0 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7596,6 +7596,7 @@ socket_exec(PyObject *m)
ADD_INT_CONST(m, "SO_VM_SOCKETS_BUFFER_MAX_SIZE", 2);
ADD_INT_CONST(m, "VMADDR_CID_ANY", 0xffffffff);
ADD_INT_CONST(m, "VMADDR_PORT_ANY", 0xffffffff);
ADD_INT_CONST(m, "VMADDR_CID_LOCAL", 1);
ADD_INT_CONST(m, "VMADDR_CID_HOST", 2);
ADD_INT_CONST(m, "VM_SOCKETS_INVALID_VERSION", 0xffffffff);
ADD_INT_CONST(m, "IOCTL_VM_SOCKETS_GET_LOCAL_CID", _IO(7, 0xb9));
Expand Down

0 comments on commit e94dbe4

Please sign in to comment.