avocado icon indicating copy to clipboard operation
avocado copied to clipboard

fix: Check if the port is available via socket.connect

Open PaulYuuu opened this issue 11 months ago • 2 comments

The current implementation doesn't accurately determine if the port is in use. We need to attempt a connection to the port rather than just attempting to bind to it.

Also, fix some typo in the doc string.

Signed-off-by: Yihuang Yu [email protected]

PaulYuuu avatar Mar 21 '24 10:03 PaulYuuu

Sorry @clebergnu, I forgot to add a comment to introduce the background.

I submitted this PR due to a kernel bug, it may be related to https://github.com/torvalds/linux/commit/28044fc1d4953b07acec0da4d2fc4784c57ea6fb. So I think it's worth improving this function.

The reproducer is:

# /usr/libexec/qemu-kvm -M virt -cpu host --nodefaults -vnc :1

# netstat -tuln | grep 5901
tcp        0      0 0.0.0.0:5901            0.0.0.0:*               LISTEN
tcp6       0      0 :::5901                 :::*                    LISTEN
>>> from socket import *
>>> s_v4 = socket(AF_INET, SOCK_STREAM)
>>> s_v6 = socket(AF_INET6, SOCK_STREAM)
>>> # IPv6 will refuse to bind
>>> s_v6.bind(('localhost', 5901))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 98] Address already in use
>>> # IPv4 can successfully bind the port, but should not
>>> s_v4.bind(('localhost', 5901))
>>>

PaulYuuu avatar Mar 22 '24 01:03 PaulYuuu

avocado-vt will find free ports to VM, like vnc port

self.vnc_port = utils_misc.find_free_port(5900, 6900, sequent=True)

When defining the second VM, 5900 is already used but find_free_port still returns 5900, a workaround is to use family=socket.AF_INET6, but some systems may have no INET6 enablement.

PaulYuuu avatar Mar 22 '24 01:03 PaulYuuu

@clebergnu, can you take a look?

PaulYuuu avatar Apr 16 '24 08:04 PaulYuuu