On Mon, Feb 28, 2022 at 12:43:25PM +0100, Thomas Huth wrote:
> The BootLinux tests are currently failing with an ugly python
> stack trace on my RHEL8 system since they cannot get a free port
> (likely due to the firewall settings on my system). Let's properly
> check the return value of find_free_port() instead and cancel the
> test gracefully if it cannot get a free port.
> 
> Signed-off-by: Thomas Huth <th...@redhat.com>
> ---
>  Unfortunately, it still takes > 70 seconds for each and every
>  tests from tests/avocado/boot_linux.py to get canceled, so
>  tests/avocado/boot_linux.py still renders "make check-avocado"
>  for me pretty unusable... looking at the implementation of
>  find_free_port() in Avocado, I wonder whether there isn't a
>  better way to get a free port number in Python? Brute-forcing
>  all ports between 1024 and 65536 seems just quite cumbersome
>  to me...

Even in the worst case of testing every single port,
for INET and INET6 and for STREAM and DGRAM sockets,
that find_free_port port completes in a couple of
seconds. 

The impl though is totally overkill, in checking
for both INET and INET6 and STREAM and DGRAM.

The code using the phone_home_port hardcodes
0.0.0.0 and TCP. So it would be sufficient to
let the kernel tell you what port is free:

   import socket
   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   s.bind(("0.0.0.0", 0))

   freeport = s.getsockname()[1]
   s.close()
   return freeport


This code is all inherantly racy though, because as
designed it is checking for a port that's available
and then later calls wait_for_phone_home which spins
up a HTTP server listening on the port. The port can
be used in between the check and use. This can be
the case if running many things in parallel on the
host.

It would be better to spin up that server using
kernel port auto-selection at the start eliminating
the race entirely.  Then just record the port that
was allocated and use that when building thue
cloudinit config for the guest.

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


Reply via email to