fix: prevent socket leak in TunnelManager #682
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The
TunnelManager.UpdateTunnels()function was creating socket connections to/var/run/usbmuxdfor every device on every call, includingnetwork-connected devices that can never establish tunnels. This caused thousands of orphaned Unix sockets to accumulate over time.
Root cause:
ListDevices()returns all devices including 50+ network-connected devicesm.tunnels, it callsstartTunnel()which creates socket connections viaios.GetProductVersion()UpdateTunnels()call retries the same failing devicesEvidence: After running overnight,
lsofshowed 3000+ orphaned Unix sockets:Busymate 94207 v 5u unix 0x5bcac11ae229dddf ->(none)
Busymate 94207 v 6u unix 0x1c4c7d88a25e315b ->(none)
... (3000+ more)
Solution
Skip network-connected devices - They cannot establish tunnels anyway (
ConnectionType == "Network")Track failed devices with exponential backoff - Instead of retrying every cycle:
Clean up on disconnect - Remove device from failed list when it disconnects, allowing fresh retry on reconnect
Clean up on success - Remove device from failed list when tunnel succeeds
Testing