Skip to contentSkip to conversation

tests/functional: Use more fine-grained locking when looking for free ports

PATCH In review Thomas Huth 3 participants 7 messages Aug 21, 2025 – Aug 25, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/functional: Use more fine-grained locking when looking for free ports

From: Thomas Huth <[email protected]>

Currently, we have one lock that is held while a test is looking for free ports. However, we are also using different ranges for looking for free ports nowadays (PORTS_START is based on the PID of the process), so instead of using only one lock, we should rather use a lock per range instead. This should help to allow running more tests in parallel.

While we're at it, also create the lock files without executable bit (mode is 0o777 by default).

---
 tests/functional/qemu_test/ports.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tests/functional/qemu_test/ports.py +3 -2
index 631b77abf6b..81174a61532 100644
@@ -23,8 +23,9 @@ class Ports():
     PORTS_END = PORTS_START + PORTS_RANGE_SIZE
 
     def __enter__(self):
-        lock_file = os.path.join(BUILD_DIR, "tests", "functional", "port_lock")
-        self.lock_fh = os.open(lock_file, os.O_CREAT)
+        lock_file = os.path.join(BUILD_DIR, "tests", "functional",
+                                 f".port_lock.{self.PORTS_START}")
+        self.lock_fh = os.open(lock_file, os.O_CREAT, mode=0o666)
         fcntl.flock(self.lock_fh, fcntl.LOCK_EX)
         return self
 
Signature · 1 line
2.50.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Thomas Huth

From: Thomas Huth <[email protected]>

Currently, we have one lock that is held while a test is looking for free ports. However, we are also using different ranges for looking for free ports nowadays (PORTS_START is based on the PID of the process), so instead of using only one lock, we should rather use a lock per range instead. This should help to allow running more tests in parallel.

Quoted 7 lines

While we're at it, also create the lock files without executable bit (mode is 0o777 by default).

---
 tests/functional/qemu_test/ports.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#2 lore raw
In reply to Thomas Huth
Quoted 12 lines · Thomas Huth

From: Thomas Huth <[email protected]>

Currently, we have one lock that is held while a test is looking for free ports. However, we are also using different ranges for looking for free ports nowadays (PORTS_START is based on the PID of the process), so instead of using only one lock, we should rather use a lock per range instead. This should help to allow running more tests in parallel.

While we're at it, also create the lock files without executable bit (mode is 0o777 by default).

(Unrelated to this patch but the file itself)

Hm. AF_INET supports binding to port 0 to connect to any available port (see man 7 ip). Is this not portable?

Quoted 25 lines
---
 tests/functional/qemu_test/ports.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tests/functional/qemu_test/ports.py +3 -3
index 631b77abf6b..81174a61532 100644
@@ -23,8 +23,9 @@ class Ports():
     PORTS_END = PORTS_START + PORTS_RANGE_SIZE
 
     def __enter__(self):
-        lock_file = os.path.join(BUILD_DIR, "tests", "functional", "port_lock")
-        self.lock_fh = os.open(lock_file, os.O_CREAT)
+        lock_file = os.path.join(BUILD_DIR, "tests", "functional",
+                                 f".port_lock.{self.PORTS_START}")
+        self.lock_fh = os.open(lock_file, os.O_CREAT, mode=0o666)
         fcntl.flock(self.lock_fh, fcntl.LOCK_EX)
         return self
 
--

2.50.1

#3 lore raw
Quoted 10 lines · Manos Pitsidianakis
Quoted 9 lines · Thomas Huth

From: Thomas Huth <[email protected]>

Currently, we have one lock that is held while a test is looking for free ports. However, we are also using different ranges for looking for free ports nowadays (PORTS_START is based on the PID of the process), so instead of using only one lock, we should rather use a lock per range instead. This should help to allow running more tests in parallel.

Quoted 8 lines
Quoted 3 lines

While we're at it, also create the lock files without executable bit (mode is 0o777 by default).

(Unrelated to this patch but the file itself)

Hm. AF_INET supports binding to port 0 to connect to any available port (see man 7 ip). Is this not portable?

No clue ... but in that case, we'd need to go back to only use one lock for all tests that are running in parallel, so it might cause some more contention?

Thomas

#4 lore raw
Quoted 16 lines · Thomas Huth
Quoted 14 lines · Manos Pitsidianakis
Quoted 12 lines · Thomas Huth

From: Thomas Huth <[email protected]>

Currently, we have one lock that is held while a test is looking for free ports. However, we are also using different ranges for looking for free ports nowadays (PORTS_START is based on the PID of the process), so instead of using only one lock, we should rather use a lock per range instead. This should help to allow running more tests in parallel.

While we're at it, also create the lock files without executable bit (mode is 0o777 by default).

Quoted 7 lines
Quoted 4 lines

(Unrelated to this patch but the file itself)

Hm. AF_INET supports binding to port 0 to connect to any available port (see man 7 ip). Is this not portable?

No clue ... but in that case, we'd need to go back to only use one lock for all tests that are running in parallel, so it might cause some more contention?

IIUC there would be no need for locking, since the kernel would return a free port for each process.

I can submit a patch btw, but thought I could ask first.

Signature · 2 lines
Manos Pitsidianakis
Emulation and Virtualization Engineer at Linaro Ltd
#5 lore raw
Quoted 19 lines · Manos Pitsidianakis
Quoted 18 lines · Thomas Huth
Quoted 16 lines · Manos Pitsidianakis
Quoted 12 lines · Thomas Huth

From: Thomas Huth <[email protected]>

Currently, we have one lock that is held while a test is looking for free ports. However, we are also using different ranges for looking for free ports nowadays (PORTS_START is based on the PID of the process), so instead of using only one lock, we should rather use a lock per range instead. This should help to allow running more tests in parallel.

While we're at it, also create the lock files without executable bit (mode is 0o777 by default).

(Unrelated to this patch but the file itself)

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Hm. AF_INET supports binding to port 0 to connect to any available port (see man 7 ip). Is this not portable?

No clue ... but in that case, we'd need to go back to only use one lock for all tests that are running in parallel, so it might cause some more contention?

IIUC there would be no need for locking, since the kernel would return a free port for each process.

That only works within a process, doesn't it? The problem here is that the test itself does not open the port, it just first needs to figure out a free port that will be used later. The test then launches QEMU with that port number, which will then open this port. Then the test connects to that port number (which it must know) to interact with QEMU. If QEMU is using port 0 for that, the test might not have a way to retrieve the final port number from QEMU in all cases.

Thomas

#6 lore raw
Quoted 39 lines · Thomas Huth
Quoted 19 lines · Manos Pitsidianakis
Quoted 18 lines · Thomas Huth
Quoted 16 lines · Manos Pitsidianakis
Quoted 12 lines · Thomas Huth

From: Thomas Huth <[email protected]>

Currently, we have one lock that is held while a test is looking for free ports. However, we are also using different ranges for looking for free ports nowadays (PORTS_START is based on the PID of the process), so instead of using only one lock, we should rather use a lock per range instead. This should help to allow running more tests in parallel.

While we're at it, also create the lock files without executable bit (mode is 0o777 by default).

(Unrelated to this patch but the file itself)

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Hm. AF_INET supports binding to port 0 to connect to any available port (see man 7 ip). Is this not portable?

No clue ... but in that case, we'd need to go back to only use one lock for all tests that are running in parallel, so it might cause some more contention?

IIUC there would be no need for locking, since the kernel would return a free port for each process.

That only works within a process, doesn't it? The problem here is that the test itself does not open the port, it just first needs to figure out a free port that will be used later.
The test then launches QEMU with that port number, which will then open this port. Then the test connects to that port number (which it must know) to interact with QEMU.
If QEMU is using port 0 for that, the test might not have a way to retrieve the final port number from QEMU in all cases.

Quoted 2 lines

Thomas

Ah yeah, correct. It depends on what the test wants to do with that port.

Thanks

Signature · 2 lines
Manos Pitsidianakis
Emulation and Virtualization Engineer at Linaro Ltd