Unit test test.test_socket.ThreadedVSOCKSocketStreamTest.testStream hangs on WSL2 ubuntu 22.04 with Python 3.12.0a4+

unit test test.test_socket.ThreadedVSOCKSocketStreamTest.testStream hangs on WSL2 ubuntu 22.04 with Python 3.12.0a4+.

Added some print and it seems vsock does not work on WSL though /dev/vsock exists.

peter@Xiaomi-ShaWuJing:~/repo/cpython$ 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 output:

./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/main.py", line 274, in runTests
    self.result = testRunner.run(self.test)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/peter/repo/cpython/Lib/unittest/runner.py", line 208, in run
    test(result)
  File "/home/peter/repo/cpython/Lib/unittest/suite.py", line 84, in __call__
    return self.run(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/peter/repo/cpython/Lib/unittest/suite.py", line 122, in run
    test(result)
  File "/home/peter/repo/cpython/Lib/unittest/suite.py", line 84, in __call__
    return self.run(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/peter/repo/cpython/Lib/unittest/suite.py", line 122, in run
    test(result)
  File "/home/peter/repo/cpython/Lib/unittest/case.py", line 678, in __call__
    return self.run(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/peter/repo/cpython/Lib/unittest/case.py", line 619, in run
    self._callSetUp()
  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
sys:1: ResourceWarning: unclosed <socket.socket fd=3, family=40, type=1, proto=0, laddr=(4294967295, 1234)>
sys:1: ResourceWarning: unclosed <socket.socket fd=5, family=40, type=1, proto=0, laddr=(4294967295, 4294967295)>
Exception ignored in: <generator object wait_threads_exit at 0x7ff8bd4ae900>
Traceback (most recent call last):
  File "/home/peter/repo/cpython/Lib/test/support/threading_helper.py", line 92, in wait_threads_exit
  File "/home/peter/repo/cpython/Lib/test/support/__init__.py", line 739, in gc_collect
ImportError: sys.meta_path is None, Python is likely shutting down

Have you seen this test pass on any version of python?

No, I haven’t tested other versions yet.

But it seems wsl2 does not officially support vsock yet as per the post below. Maybe we should skip this test case for WSL for now?

Microsoft has not yet announced any official way to use the VSOCK for WSL2. Hence, we're using a heuristic method for locating the virtual machine for WSL2 and setting up X410 for the VSOCK. ...

I was just able to build python 3.10.9+ and confirm it is the same behaviour.

And Windows version is Windows 10 home 19044.2486.

Please fix the title as it is not accurate. This a wsl 2 issue not a python 3.12 issue.

Feel free to change it if you can. I can’t.