Skip to content

Commit 32175f7

Browse files
sarahmonodGus Monod
authored andcommitted
Use pytest_sessionfinish instead of atexit
With 3.12, it is no longer possible to create a new thread in an `atexit` handler. Stopping the monitor process requires writing a message in the queue, which itself may require the creation of a thread. This commit will remove the need to have an `atexit` handler, by using the `pytest_sessionfinish` hook instead, which should work in our case. Signed-off-by: Gus Monod <gmonod1@bloomberg.net>
1 parent ce3f99d commit 32175f7

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/pytest_pystack/_monitor_process.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import atexit
21
import multiprocessing
32
import os
43
import shlex
@@ -9,22 +8,31 @@
98
from ._config import PystackConfig
109
from ._debug_detect import debug_detected
1110

11+
_queue = multiprocessing.Queue()
12+
_process = None
1213

13-
def start(config):
14-
queue = multiprocessing.Queue()
1514

16-
process = multiprocessing.Process(
15+
def start(config):
16+
global _process
17+
_process = multiprocessing.Process(
1718
target=_run_monitor,
1819
args=(
1920
config,
2021
os.getpid(),
21-
queue,
22+
_queue,
2223
),
2324
name="pystack_monitor",
2425
)
25-
process.start()
26-
atexit.register(_stop, process, queue)
27-
return queue
26+
_process.start()
27+
return _queue
28+
29+
30+
def stop():
31+
if _process:
32+
_queue.put_nowait(None)
33+
_process.join(timeout=5)
34+
if _process.is_alive():
35+
_process.kill()
2836

2937

3038
def _run_monitor(config: PystackConfig, pid, queue):
@@ -67,8 +75,3 @@ def _run_monitor(config: PystackConfig, pid, queue):
6775
if config.output_file:
6876
with open(config.output_file, "a") as f:
6977
print(output, file=f)
70-
71-
72-
def _stop(process, queue):
73-
queue.put(None)
74-
process.join(timeout=5)

src/pytest_pystack/plugin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,8 @@ def pytest_configure(config) -> None:
9393
print_stderr=True,
9494
)
9595
config._pystack_queue = _monitor_process.start(pystack_config)
96+
97+
98+
@pytest.hookimpl
99+
def pytest_sessionfinish(session, exitstatus):
100+
_monitor_process.stop()

0 commit comments

Comments
 (0)