Skip to content

Fix stdout resource leak in find_ssh_servers#45

Open
InterstellarIntruder wants to merge 1 commit into
Project-CETI:mainfrom
InterstellarIntruder:fix/40-stdout-resource-leak
Open

Fix stdout resource leak in find_ssh_servers#45
InterstellarIntruder wants to merge 1 commit into
Project-CETI:mainfrom
InterstellarIntruder:fix/40-stdout-resource-leak

Conversation

@InterstellarIntruder
Copy link
Copy Markdown

@InterstellarIntruder InterstellarIntruder commented Apr 17, 2026

Fixes #40

find_ssh_servers() was manually redirecting sys.stdout to /dev/null for both LAN and USB scans, but only restoring it on the happy path. If asyncio.run() raised, stdout could stay redirected and hide all later output.

This replaces manual swapping with nested context managers:

  • open(os.devnull, "w") to manage the devnull handle
  • contextlib.redirect_stdout(...) to guarantee stdout restoration on both success and exception

Tests

  • Added tests/test_whaletag.py
  • Added 3 tests covering:
    • success path restores stdout
    • exception on first asyncio.run restores stdout
    • exception on second asyncio.run restores stdout

find_ssh_servers redirected sys.stdout manually and only restored it on
the happy path. If asyncio.run() raised during LAN or USB scans, stdout
could remain redirected to /dev/null and hide all subsequent output.

Use nested context managers (open + redirect_stdout) to guarantee stdout
restoration and close the devnull handle on both success and exception.
Add three tests covering success and failures at both asyncio.run call
sites to prove stdout is always restored.

Fixes Project-CETI#40
@InterstellarIntruder
Copy link
Copy Markdown
Author

Added focused tests for stdout restoration in all three relevant paths (success, first scan failure, second scan failure). If anyone sees a preferred pattern for suppressing findssh output here, happy to adjust.

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.

stdout Resource Leak

1 participant