Skip to content

Fix HapMonitor sockets never reconnecting after same-port HAP instance restart#43

Merged
NorthernMan54 merged 6 commits into
beta-4.0.4from
copilot/fix-hapmonitor-sockets-reconnect
Apr 26, 2026
Merged

Fix HapMonitor sockets never reconnecting after same-port HAP instance restart#43
NorthernMan54 merged 6 commits into
beta-4.0.4from
copilot/fix-hapmonitor-sockets-reconnect

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 26, 2026

When a paired HAP instance restarts on the same port with identical mDNS TXT records (md, port, c# unchanged), the monitor socket dies permanently. The bonjour up handler gates refreshMonitorConnection on a port/name/config diff — none fire on an identical restart — and HapMonitor has no self-healing logic on socket close.

Changes

src/monitor.ts — self-healing reconnect loop

  • On socket.close, if !_stopped, schedules _scheduleReconnect(instance) with exponential backoff: 2 s initial, doubles per failure, capped at 30 s, +0–1 s jitter
  • finish() sets _stopped = true and clears all pending timers before destroying sockets — intentional teardown does not trigger reconnects
  • refreshMonitorConnection() cancels any pending reconnect timer before creating a new connection (prevents duplicate reconnects when mDNS and the backoff timer race)
  • Null-guards the socket in refreshMonitorConnection for the case where the initial connectInstance failed before assignment
  • Adds isInstanceConnected(username): boolean — queries live socket state without exposing internals

src/index.ts — fast reconnect path on mDNS re-announcement

  • In the bonjour up handler, adds an else if branch: when an existing instance is re-announced unchanged but isInstanceConnected() is false, calls refreshMonitorConnection immediately rather than waiting for the backoff timer
} else if (this.hapMonitor && !this.hapMonitor.isInstanceConnected(instance.username)) {
  // Same port/name/config but socket is dead (e.g. same-port restart) — reconnect immediately
  this.hapMonitor.refreshMonitorConnection(this.instances[existingInstanceIndex])
}

The two fixes are complementary: the backoff loop handles the case where mDNS re-announcement is missed or delayed; the index.ts fast path fires an immediate reconnect as soon as mDNS confirms the bridge is back.

Copilot AI and others added 2 commits April 26, 2026 21:37
Agent-Logs-Url: https://github.com/homebridge/hap-client/sessions/79bcc638-c35e-4093-9c3a-e1fe526846b1

Co-authored-by: NorthernMan54 <19808920+NorthernMan54@users.noreply.github.com>
…restart

Agent-Logs-Url: https://github.com/homebridge/hap-client/sessions/79bcc638-c35e-4093-9c3a-e1fe526846b1

Co-authored-by: NorthernMan54 <19808920+NorthernMan54@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix HapMonitor sockets not reconnecting after HAP instance restart Fix HapMonitor sockets never reconnecting after same-port HAP instance restart Apr 26, 2026
Copilot AI requested a review from NorthernMan54 April 26, 2026 21:42
@github-actions github-actions Bot added the fix label Apr 26, 2026
@NorthernMan54 NorthernMan54 marked this pull request as ready for review April 26, 2026 22:41
@NorthernMan54 NorthernMan54 merged commit 88b0388 into beta-4.0.4 Apr 26, 2026
3 of 6 checks passed
@NorthernMan54 NorthernMan54 deleted the copilot/fix-hapmonitor-sockets-reconnect branch April 26, 2026 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HapMonitor sockets are never reconnected after a same-port HAP instance restart

2 participants