{"id":10153,"library":"pytest-threadleak","title":"pytest-threadleak","description":"pytest-threadleak is a pytest plugin that automatically detects non-daemon thread leaks after tests complete. It helps identify when tests leave background threads running, which can cause resource exhaustion or unexpected behavior. The current version is 0.5.0, and it is maintained with releases as needed for bug fixes or new features.","status":"active","version":"0.5.0","language":"en","source_language":"en","source_url":"https://github.com/nirs/pytest-threadleak","tags":["pytest","testing","threading","plugins","leak detection","concurrency"],"install":[{"cmd":"pip install pytest-threadleak pytest","lang":"bash","label":"Install pytest-threadleak and pytest"}],"dependencies":[{"reason":"pytest-threadleak is a plugin for the pytest testing framework and requires pytest to function.","package":"pytest"}],"imports":[{"note":"pytest-threadleak provides fixtures, not direct module imports for its core functionality. The 'threadleak' fixture is automatically discovered and injected by pytest.","wrong":"from pytest_threadleak import threadleak","symbol":"threadleak","correct":"def test_something(threadleak): ..."}],"quickstart":{"code":"import threading\nimport time\n\n# test_my_app.py\n\ndef test_my_function_no_leak(threadleak):\n    \"\"\"\n    This test uses the 'threadleak' fixture to ensure no new\n    non-daemon threads are left running after the test completes.\n    This specific test should pass cleanly.\n    \"\"\"\n    def do_something_brief():\n        time.sleep(0.01)\n    \n    t = threading.Thread(target=do_something_brief)\n    t.start()\n    t.join() # Ensure the thread finishes before the test does\n\n# To run this test:\n# pip install pytest-threadleak pytest\n# pytest test_my_app.py\n\n# To observe a leak (uncomment and run):\n# def test_my_function_with_leak(threadleak):\n#     \"\"\"\n#     This test will intentionally leave a non-daemon thread running,\n#     causing pytest-threadleak to report a ThreadLeakError.\n#     \"\"\"\n#     def infinite_loop():\n#         while True:\n#             time.sleep(0.1)\n#     \n#     threading.Thread(target=infinite_loop).start()","lang":"python","description":"To get started, simply install `pytest-threadleak` and include the `threadleak` fixture in your test functions. The plugin automatically checks for new non-daemon threads spawned during the test that are still active when the test finishes. The example demonstrates a clean test and how to intentionally create a leak to see the plugin in action."},"warnings":[{"fix":"Upgrade to Python 3 or restrict pytest-threadleak version to 0.2.0 or earlier if Python 2 is strictly required (not recommended for new projects).","message":"Python 2 support was dropped in version 0.3.0. Users on Python 2 must use an older version.","severity":"breaking","affected_versions":"<=0.2.0"},{"fix":"Ensure all non-daemon threads are explicitly joined or terminated within the test's scope. For long-running background threads that are expected, configure `threadleak_thread_name_patterns_to_ignore` in your `pytest.ini`.","message":"pytest-threadleak only detects threads that are *still running* and are *not daemon threads* after a test. Threads that complete and join successfully before the test ends will not be flagged. Daemon threads are also ignored by default.","severity":"gotcha","affected_versions":"All"},{"fix":"Use the `threadleak_thread_name_patterns_to_ignore` option in your `pytest.ini` to specify regular expressions matching the names of threads that should be ignored, for example: `threadleak_thread_name_patterns_to_ignore = 'ThreadPoolExecutor.*'`.","message":"If tests fail with `ThreadLeakError` for threads that are part of standard libraries or external packages (e.g., from `requests` or `concurrent.futures`), these threads might need to be explicitly ignored.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure `pytest-threadleak` is installed in the same Python environment where you are running pytest: `pip install pytest-threadleak`.","cause":"The pytest-threadleak plugin is not installed, or pytest cannot find it in the current environment.","error":"pytest.FixtureLookupError: fixture 'threadleak' not found"},{"fix":"Identify the code spawning the listed thread(s) and ensure they are either daemonized (if background processing) or properly joined/terminated before the test function finishes. If it's an expected background thread, consider adding its name pattern to `threadleak_thread_name_patterns_to_ignore` in `pytest.ini`.","cause":"A non-daemon thread was spawned during the test and was still active when the test completed. This is the core functionality of the plugin detecting a leak.","error":"Failed: ThreadLeakError: The following threads were still running after the test: ['<Thread(Thread-1, started 123456789)>']"}]}