Skip to content

Stop leaking StreamHandlers across test runs (#450)#457

Merged
nitrobass24 merged 1 commit into
developfrom
fix/test-logger-handler-leak
May 3, 2026
Merged

Stop leaking StreamHandlers across test runs (#450)#457
nitrobass24 merged 1 commit into
developfrom
fix/test-logger-handler-leak

Conversation

@nitrobass24
Copy link
Copy Markdown
Owner

@nitrobass24 nitrobass24 commented May 2, 2026

Summary

  • Add self.addCleanup(logger.removeHandler, handler) after every addHandler(handler) call in test setUp methods (10 sites across 8 files)

Why

Per #450: every test setUp adds a StreamHandler to a logger (mostly the root logger) and never removes it. Loggers are singletons, so by test N the logger has N handlers and every log line prints N times. Test correctness isn't affected — no assertion depends on handler count — but the suite output gets progressively noisier.

Approach

Used unittest's addCleanup rather than wiring up tearDown. Reasons:

  • Drops in as a single line right after addHandler, so the registration sits next to the operation it cleans up
  • Runs even if the rest of setUp raises after the registration — robust to partial initialization failures
  • Doesn't conflict with files that already have a tearDown (tests/integration/test_lftp/test_lftp.py, tests/unittests/test_lftp/test_lftp.py)

Files touched

File setUp methods fixed
tests/integration/test_web/test_web_app.py 1
tests/integration/test_lftp/test_lftp.py 1
tests/integration/test_controller/test_controller.py 1
tests/unittests/test_common/test_app_process.py 2 (TestAppProcess + TestAppOneShotProcess)
tests/unittests/test_ssh/test_sshcp.py 1
tests/unittests/test_lftp/test_lftp.py 1
tests/unittests/test_controller/test_delete/test_delete_process.py 2 (TestDeleteLocalProcess + TestDeleteRemoteProcess)
tests/unittests/test_controller/test_scan/test_active_scanner.py 1

Test plan

  • Python Unit Tests CI green
  • Python Integration Tests CI green
  • Run the suite locally — log lines no longer appear N times for the Nth test

Closes #450

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Improved test isolation and reliability by ensuring logging handlers are automatically cleaned up after each test run across multiple test suites, preventing handler accumulation and test interference.

Test setUp methods across the suite added a fresh StreamHandler to a
shared logger (mostly the root logger) and never removed it. Because
loggers are singletons, by test N the logger held N handlers and each
log line printed N times. Test correctness wasn't affected — no
assertion depends on handler count — only output noise.

Use unittest's addCleanup hook to remove the handler after each test.
addCleanup runs even if setUp partially fails after the registration,
so the cleanup is robust against initialization errors.

Touches 10 setUp methods across 8 files (TestAppProcess /
TestAppOneShotProcess and TestDeleteLocalProcess /
TestDeleteRemoteProcess each have their own setUp).

Closes #450

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 2, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 781539ce-ecf4-4abf-af81-1260c037912f

📥 Commits

Reviewing files that changed from the base of the PR and between 142dc62 and e292560.

📒 Files selected for processing (8)
  • src/python/tests/integration/test_controller/test_controller.py
  • src/python/tests/integration/test_lftp/test_lftp.py
  • src/python/tests/integration/test_web/test_web_app.py
  • src/python/tests/unittests/test_common/test_app_process.py
  • src/python/tests/unittests/test_controller/test_delete/test_delete_process.py
  • src/python/tests/unittests/test_controller/test_scan/test_active_scanner.py
  • src/python/tests/unittests/test_lftp/test_lftp.py
  • src/python/tests/unittests/test_ssh/test_sshcp.py

📝 Walkthrough

Walkthrough

This PR adds cleanup callbacks to 8 test setUp methods across integration and unit tests to remove logger StreamHandler instances after each test completes, preventing handler accumulation across test runs.

Changes

Logger Handler Cleanup in Test Setup

Layer / File(s) Summary
Test Cleanup Registration
src/python/tests/integration/test_controller/test_controller.py, src/python/tests/integration/test_lftp/test_lftp.py, src/python/tests/integration/test_web/test_web_app.py, src/python/tests/unittests/test_common/test_app_process.py, src/python/tests/unittests/test_controller/test_delete/test_delete_process.py, src/python/tests/unittests/test_controller/test_scan/test_active_scanner.py, src/python/tests/unittests/test_lftp/test_lftp.py, src/python/tests/unittests/test_ssh/test_sshcp.py
Each test's setUp method now registers self.addCleanup(logger.removeHandler, handler) to ensure the StreamHandler added to the root logger is removed during test teardown, preventing handler persistence across test runs.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Stop leaking StreamHandlers across test runs (#450)' clearly and concisely summarizes the main change: fixing StreamHandler leaks in tests by adding cleanup handlers.
Linked Issues check ✅ Passed The PR successfully addresses all coding requirements from issue #450 by adding self.addCleanup(logger.removeHandler, handler) to all 8 affected test files across 10 setUp locations.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the StreamHandler leak issue identified in #450; no unrelated modifications or scope creep detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/test-logger-handler-leak

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 0/1 reviews remaining, refill in 60 minutes.

Comment @coderabbitai help to get the list of available commands and usage tips.

@nitrobass24
Copy link
Copy Markdown
Owner Author

@CodeRabbit review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 2, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@nitrobass24 nitrobass24 merged commit 3b66451 into develop May 3, 2026
18 checks passed
@nitrobass24 nitrobass24 deleted the fix/test-logger-handler-leak branch May 3, 2026 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test logger handlers leak across test runs

1 participant