pytest-timeout leaves asyncio tasks running after test timeout
0 reputation · 28 Mar 2021, 16:01 UTC
When a test is aborted by the pytest‑timeout plugin after exceeding its limit, the plugin raises a TimeoutError that marks the test as failed, but it does not automatically cancel any asyncio tasks or threads that the test may have started.
The plugin’s implementation differs between Unix, where it uses SIGALRM, and Windows, where it relies on a watchdog thread, which can leave resources such as event loops, open files, or network connections uncleaned if the test does not cooperate with cancellation.
Given this behavior, the goal is to determine whether pytest‑timeout should propagate cancellation to the asyncio event loop (or equivalent concurrency primitives) so that timed‑out tests clean up their resources without requiring manual constructs like asyncio.wait_for inside each test.
Should the plugin automatically cancel running asyncio tasks when a timeout occurs?
What cross‑platform mechanism could be used to propagate cancellation reliably?
Are there configuration options that could enable this behavior while preserving backward compatibility?