Fix HapMonitor sockets never reconnecting after same-port HAP instance restart#43
Merged
NorthernMan54 merged 6 commits intoApr 26, 2026
Merged
Conversation
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
NorthernMan54
approved these changes
Apr 26, 2026
NorthernMan54
added a commit
that referenced
this pull request
May 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 bonjouruphandler gatesrefreshMonitorConnectionon a port/name/config diff — none fire on an identical restart — andHapMonitorhas no self-healing logic on socket close.Changes
src/monitor.ts— self-healing reconnect loopsocket.close, if!_stopped, schedules_scheduleReconnect(instance)with exponential backoff: 2 s initial, doubles per failure, capped at 30 s, +0–1 s jitterfinish()sets_stopped = trueand clears all pending timers before destroying sockets — intentional teardown does not trigger reconnectsrefreshMonitorConnection()cancels any pending reconnect timer before creating a new connection (prevents duplicate reconnects when mDNS and the backoff timer race)refreshMonitorConnectionfor the case where the initialconnectInstancefailed before assignmentisInstanceConnected(username): boolean— queries live socket state without exposing internalssrc/index.ts— fast reconnect path on mDNS re-announcementuphandler, adds anelse ifbranch: when an existing instance is re-announced unchanged butisInstanceConnected()is false, callsrefreshMonitorConnectionimmediately rather than waiting for the backoff timerThe two fixes are complementary: the backoff loop handles the case where mDNS re-announcement is missed or delayed; the
index.tsfast path fires an immediate reconnect as soon as mDNS confirms the bridge is back.