Skip to content

Commit e63fb6d

Browse files
author
rodrigo.nogueira
committed
test: use baseline thread detection for Windows cleanup
Instead of matching thread names (which was unreliable), capture the baseline set of threads before starting proxy.py and wait until all extra threads have finished.
1 parent 968d9bd commit e63fb6d

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

tests/test_proxy_functional.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ def secure_proxy_url(tls_certificate_pem_path: str) -> Iterator[URL]:
6666
6767
This fixture also spawns that instance and tears it down after the test.
6868
"""
69+
if os.name == "nt":
70+
import gc
71+
import threading
72+
import time
73+
74+
baseline_threads = set(threading.enumerate())
75+
6976
proxypy_args = [
7077
# --threadless does not work on windows, see
7178
# https://github.com/abhinavsingh/proxy.py/issues/492
@@ -83,26 +90,21 @@ def secure_proxy_url(tls_certificate_pem_path: str) -> Iterator[URL]:
8390
]
8491

8592
with proxy.Proxy(input_args=proxypy_args) as proxy_instance:
93+
if os.name == "nt":
94+
spawned_threads = set(threading.enumerate()) - baseline_threads
95+
8696
yield URL.build(
8797
scheme="https",
8898
host=str(proxy_instance.flags.hostname),
8999
port=proxy_instance.flags.port,
90100
)
91101

92102
if os.name == "nt":
93-
import gc
94-
import threading
95-
import time
96-
97103
deadline = time.monotonic() + 5.0
98104
while time.monotonic() < deadline:
99105
gc.collect()
100-
proxy_threads = [
101-
t
102-
for t in threading.enumerate()
103-
if "proxy" in t.name.lower() or "acceptor" in t.name.lower()
104-
]
105-
if not proxy_threads:
106+
remaining = set(threading.enumerate()).intersection(spawned_threads)
107+
if not remaining:
106108
break
107109
time.sleep(0.05)
108110

0 commit comments

Comments
 (0)