Skip to content

Conversation

@X9X0
Copy link
Owner

@X9X0 X9X0 commented Nov 20, 2025

This commit fixes a critical resource leak that causes the server to run out of file descriptors after repeated discovery scans.

Problem:

  • VISAScanner created a ResourceManager once and kept it forever
  • Every 30-second discovery scan opened VISA resources
  • ResourceManager accumulated file descriptors without release
  • After ~1000 scans, system hits ulimit (1024 open files)
  • Results in "[Errno 24] Too many open files" errors

Root Cause:

  • ResourceManager was created in init but never closed
  • DiscoveryManager had no shutdown method to release resources
  • Each scan opened new sockets/file handles without cleanup

Fix:

  • Added finally block to VISAScanner.scan() to close ResourceManager
  • Added shutdown() method to DiscoveryManager to properly release resources
  • ResourceManager now created/destroyed on each scan cycle
  • Prevents file descriptor accumulation over time

Impact:

  • Eliminates file descriptor leak in VISA scanning
  • Eliminates file descriptor leak in mDNS scanning
  • Server can now run indefinitely without resource exhaustion
  • Discovery continues working after hours/days of operation

Files Changed:

  • server/discovery/visa_scanner.py: Close ResourceManager after each scan
  • server/discovery/manager.py: Add shutdown() method for cleanup

Testing:

  • Monitor /proc/[pid]/fd/ to verify file descriptors are released
  • Run discovery for extended periods (hours) to confirm no leaks
  • Check ulimit -n and compare to actual open file count

This commit fixes a critical resource leak that causes the server to
run out of file descriptors after repeated discovery scans.

Problem:
- VISAScanner created a ResourceManager once and kept it forever
- Every 30-second discovery scan opened VISA resources
- ResourceManager accumulated file descriptors without release
- After ~1000 scans, system hits ulimit (1024 open files)
- Results in "[Errno 24] Too many open files" errors

Root Cause:
- ResourceManager was created in __init__ but never closed
- DiscoveryManager had no shutdown method to release resources
- Each scan opened new sockets/file handles without cleanup

Fix:
- Added finally block to VISAScanner.scan() to close ResourceManager
- Added shutdown() method to DiscoveryManager to properly release resources
- ResourceManager now created/destroyed on each scan cycle
- Prevents file descriptor accumulation over time

Impact:
- Eliminates file descriptor leak in VISA scanning
- Eliminates file descriptor leak in mDNS scanning
- Server can now run indefinitely without resource exhaustion
- Discovery continues working after hours/days of operation

Files Changed:
- server/discovery/visa_scanner.py: Close ResourceManager after each scan
- server/discovery/manager.py: Add shutdown() method for cleanup

Testing:
- Monitor /proc/[pid]/fd/ to verify file descriptors are released
- Run discovery for extended periods (hours) to confirm no leaks
- Check ulimit -n and compare to actual open file count
@X9X0 X9X0 merged commit bc87a3a into main Nov 20, 2025
25 checks passed
@X9X0 X9X0 deleted the claude/lablink-diagnostics-011qQGdeoayWWDdfHQhtaYxZ branch November 20, 2025 00:11
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.

3 participants