Skip to content

Commit 7315a1b

Browse files
author
rodrigo.nogueira
committed
test: use thread polling instead of fixed sleep for Windows cleanup
Instead of waiting a fixed 5+ seconds, poll for proxy threads to finish with gc.collect() calls. This is faster on typical runs (exits as soon as threads are gone) while still having a 5s timeout for robustness.
1 parent f6a3a00 commit 7315a1b

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

tests/test_proxy_functional.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,21 @@ def secure_proxy_url(tls_certificate_pem_path: str) -> Iterator[URL]:
8989
port=proxy_instance.flags.port,
9090
)
9191

92-
# On Windows, proxy.py uses threaded mode which can leave sockets in
93-
# a state where they haven't been fully released when GC runs during
94-
# pytest cleanup. A longer delay + multiple gc.collect() passes ensures
95-
# the proxy threads finish cleanup before pytest's unraisableexception
96-
# plugin collects warnings.
97-
# TEMPORARY: Testing with delays before AND after gc.collect()
9892
if os.name == "nt":
9993
import gc
94+
import threading
10095
import time
10196

102-
time.sleep(5)
103-
gc.collect()
104-
gc.collect()
105-
gc.collect()
106-
time.sleep(1)
97+
deadline = time.monotonic() + 5.0
98+
while time.monotonic() < deadline:
99+
gc.collect()
100+
proxy_threads = [
101+
t for t in threading.enumerate()
102+
if "proxy" in t.name.lower() or "acceptor" in t.name.lower()
103+
]
104+
if not proxy_threads:
105+
break
106+
time.sleep(0.05)
107107

108108

109109
@pytest.fixture

0 commit comments

Comments
 (0)