From 53f40ab71259805a81aca8a598c3182feef4a1bd Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Thu, 31 Aug 2023 09:55:48 +0200 Subject: [PATCH] Backport PR #26542: TST: Ensure test_webagg subprocess is terminated --- .../tests/test_backends_interactive.py | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/tests/test_backends_interactive.py b/lib/matplotlib/tests/test_backends_interactive.py index 7341ebd3277a..dbc0f0928b74 100644 --- a/lib/matplotlib/tests/test_backends_interactive.py +++ b/lib/matplotlib/tests/test_backends_interactive.py @@ -477,24 +477,27 @@ def test_webagg(): inspect.getsource(_test_interactive_impl) + "\n_test_interactive_impl()", "{}"], env={**os.environ, "MPLBACKEND": "webagg", "SOURCE_DATE_EPOCH": "0"}) - url = "http://{}:{}".format( - mpl.rcParams["webagg.address"], mpl.rcParams["webagg.port"]) + url = f'http://{mpl.rcParams["webagg.address"]}:{mpl.rcParams["webagg.port"]}' timeout = time.perf_counter() + _test_timeout - while True: - try: - retcode = proc.poll() - # check that the subprocess for the server is not dead - assert retcode is None - conn = urllib.request.urlopen(url) - break - except urllib.error.URLError: - if time.perf_counter() > timeout: - pytest.fail("Failed to connect to the webagg server.") - else: - continue - conn.close() - proc.send_signal(signal.SIGINT) - assert proc.wait(timeout=_test_timeout) == 0 + try: + while True: + try: + retcode = proc.poll() + # check that the subprocess for the server is not dead + assert retcode is None + conn = urllib.request.urlopen(url) + break + except urllib.error.URLError: + if time.perf_counter() > timeout: + pytest.fail("Failed to connect to the webagg server.") + else: + continue + conn.close() + proc.send_signal(signal.SIGINT) + assert proc.wait(timeout=_test_timeout) == 0 + finally: + if proc.poll() is None: + proc.kill() def _lazy_headless():