Skip to content

Conversation

@vbragaru
Copy link

@vbragaru vbragaru commented Feb 6, 2026

Problem

The TunnelManager.UpdateTunnels() function was creating socket connections to /var/run/usbmuxd for every device on every call, including
network-connected devices that can never establish tunnels. This caused thousands of orphaned Unix sockets to accumulate over time.

Root cause:

  1. ListDevices() returns all devices including 50+ network-connected devices
  2. For each device not in m.tunnels, it calls startTunnel() which creates socket connections via ios.GetProductVersion()
  3. Network devices always fail to connect, but are never tracked as failed
  4. Next UpdateTunnels() call retries the same failing devices
  5. With default 1-second interval × 50+ network devices = hundreds of leaked sockets per hour

Evidence: After running overnight, lsof showed 3000+ orphaned Unix sockets:
Busymate 94207 v 5u unix 0x5bcac11ae229dddf ->(none)
Busymate 94207 v 6u unix 0x1c4c7d88a25e315b ->(none)
... (3000+ more)

Solution

  1. Skip network-connected devices - They cannot establish tunnels anyway (ConnectionType == "Network")

  2. Track failed devices with exponential backoff - Instead of retrying every cycle:

    • 30s after 1st failure
    • 60s after 2nd failure
    • 120s after 3rd failure
    • Max 5 minutes between retries
  3. Clean up on disconnect - Remove device from failed list when it disconnects, allowing fresh retry on reconnect

  4. Clean up on success - Remove device from failed list when tunnel succeeds

Testing

  • OS: macOS 15 Sequoia (Darwin 24.6.0)
  • Ran app overnight with 50+ network devices discovered
  • Socket count remained stable instead of growing unboundedly
  • USB-connected devices still establish tunnels correctly

… and adding exponential backoff

The TunnelManager was creating socket connections for every device on every
UpdateTunnels call, including network-connected devices that can never establish
tunnels. This caused thousands of orphaned Unix sockets over time.

Changes:
- Skip network-connected devices (ConnectionType == "Network") since they
  cannot establish tunnels
- Track devices that fail to connect and apply exponential backoff (30s, 60s,
  120s, up to 5 min max) before retrying
- Clean up failed device tracking when devices disconnect to allow fresh
  retry on reconnect
- Remove devices from failed list when tunnel connection succeeds

This fixes a memory/socket leak that could accumulate thousands of orphaned
Unix sockets to /var/run/usbmuxd when many network devices are discovered.
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.

1 participant