Skip to content

Unit test test.test_socket.ThreadedVSOCKSocketStreamTest.testStream hangs on WSL2 ubuntu #101384

Closed
@peterjpxie

Description

@peterjpxie

Bug report

unit test test.test_socket.ThreadedVSOCKSocketStreamTest.testStream hangs on WSL2 ubuntu.

I added some print and it seems vsock does not work on WSL though /dev/vsock exists, HAVE_SOCKET_VSOCK() == True.

$ ls /dev/vsock
/dev/vsock

Modified test_socket.py snippet:

@unittest.skipIf(fcntl is None, "need fcntl")
@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'):
        print('__init__')
        unittest.TestCase.__init__(self, methodName=methodName)
        ThreadableTest.__init__(self)

    def setUp(self):
        print('setUp')
        self.serv = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
        self.addCleanup(self.serv.close)
        self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT))
        self.serv.listen()
        self.serverExplicitReady()
        print('Before serv.accept')
        self.conn, self.connaddr = self.serv.accept()
        print('After serv.accept')
        self.addCleanup(self.conn.close)
        print('end - setUp')

    def clientSetUp(self):
        print('clientSetup')
        time.sleep(0.1)
        self.cli = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
        self.addCleanup(self.cli.close)
        cid = get_cid()
        print('cid: ', cid)
        print('before cli.connect')
        self.cli.connect((cid, VSOCKPORT))
        print('end - clientSetup')

    def testStream(self):
        print('testStream')
        msg = self.conn.recv(1024)
        self.assertEqual(msg, MSG)

    def _testStream(self):
        self.cli.send(MSG)
        self.cli.close()

Test outputs:

./python -m unittest -v test.test_socket.ThreadedVSOCKSocketStreamTest.testStream
__init__
testStream (test.test_socket.ThreadedVSOCKSocketStreamTest.testStream) ... setUp
Before serv.accept
clientSetup
cid:  4294967295
before cli.connect
^CTraceback (most recent call last):
  File "/home/peter/repo/cpython/Lib/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/peter/repo/cpython/Lib/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "/home/peter/repo/cpython/Lib/unittest/__main__.py", line 18, in <module>
    main(module=None)
  File "/home/peter/repo/cpython/Lib/unittest/main.py", line 102, in __init__
    self.runTests()
  ...
  File "/home/peter/repo/cpython/Lib/unittest/case.py", line 576, in _callSetUp
    self.setUp()
  File "/home/peter/repo/cpython/Lib/test/test_socket.py", line 376, in _setUp
    self.__setUp()
  File "/home/peter/repo/cpython/Lib/test/test_socket.py", line 520, in setUp
    self.conn, self.connaddr = self.serv.accept()
                               ^^^^^^^^^^^^^^^^^^
  File "/home/peter/repo/cpython/Lib/socket.py", line 295, in accept
    fd, addr = self._accept()
               ^^^^^^^^^^^^^^
KeyboardInterrupt

Your environment

  • CPython versions tested on:
    both Python 3.12.0a4+ and Python 3.10.9+
  • Operating system and architecture:
    Windows 10 home 19044.2486 + WSL2 Ubuntu 22.04
uname -r
5.15.79.1-microsoft-standard-WSL2
>>> platform.release()
'5.15.79.1-microsoft-standard-WSL2'

cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04 LTS"

Suggestions

  • Make it work and pass on WSL2
  • Skip the test on WSL2
  • Add a timeout so it does not hang at least

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions