avocado-framework / avocado

Avocado is a set of tools and libraries to help with automated testing. One can call it a test framework with benefits. Native tests are written in Python and they follow the unittest pattern, but any executable can serve as a test.
https://avocado-framework.github.io/
Other
336 stars 335 forks source link

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

Closed PaulYuuu closed 2 months ago

PaulYuuu commented 3 months ago

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 yihyu@redhat.com

PaulYuuu commented 3 months ago

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 commented 3 months ago

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 commented 2 months ago

@clebergnu, can you take a look?