From db1a2cfe2d9b6dd88fe34195badaffd89e24610b Mon Sep 17 00:00:00 2001
From: Fabian Vogt <fvogt@suse.de>
Date: Thu, 27 Aug 2026 15:35:34 +0200
Subject: [PATCH] fix: wait for git daemon to listen before connecting

Properly wait instead of hardcoding a short sleep.

Fixes #1676
---
 test/lib/helper.py | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/test/lib/helper.py b/test/lib/helper.py
index 58923eaef..4135fe5dd 100644
--- a/test/lib/helper.py
+++ b/test/lib/helper.py
@@ -32,6 +32,7 @@
 import os
 import os.path as osp
 from stat import S_ISLNK, ST_MODE
+import socket
 import subprocess
 import sys
 import tempfile
@@ -218,8 +219,15 @@ def git_daemon_launched(base_path, ip, port):
                 base_path=base_path,
                 as_process=True,
             )
-        # Yes, I know... fortunately, this is always going to work if sleep time is just large enough.
-        time.sleep(1.0 if sys.platform == "win32" else 0.5)
+
+        # Wait until git daemon listens for connections.
+        for _attempt in range(1, 30):
+            try:
+                socket.create_connection((ip, port), timeout=30).close()
+                break
+            except ConnectionRefusedError:
+                time.sleep(0.5)
+
     except Exception as ex:
         msg = textwrap.dedent(
             """
