Skip to content

Fix HomeKit server closing the controller connection when a stream ends - #2776

Closed
RyoKusnadi wants to merge 1 commit into
AlexxIT:masterfrom
RyoKusnadi:fix/homekit-stop-keeps-hap-conn
Closed

RyoKusnadi wants to merge 1 commit into
AlexxIT:masterfrom
RyoKusnadi:fix/homekit-stop-keeps-hap-conn

Conversation

@RyoKusnadi

Copy link
Copy Markdown
Contributor

Problem: Every time you close a camera view in the Home app, go2rtc logs write tcp ...: use of closed network connection from internal/homekit/server.go:125. The error isn't just noise: go2rtc has closed the iPhone's HAP connection, so the iPhone has to connect and pair-verify again for its next request.

How it happens: Closing the view sends PUT /characteristics with a SelectedStreamConfiguration whose command is End. server.SetCharacteristic finds the consumer and calls consumer.Stop(). homekit.Consumer.Stop calls core.Connection.Stop(), which closes Transport, and for this consumer Transport is the controller's HAP conn (NewConsumer(conn, ...)). handleRequest then writes the 204 for that same PUT to the closed conn, and the handler returns the error. The same close happens when the consumer's deadline fires, through stream.RemoveConsumer.

Steps to reproduce: expose a stream as a HomeKit camera, open it in the Home app, close it.
Expected: no error, and the HAP connection stays open.
Actual: the error above, and the HAP connection is closed.

Fix: Consumer.Stop closes its senders and stops the deadline, but no longer closes the conn. The HAP session belongs to the controller and outlives one video session. It's still closed when the controller goes away (defer conn.Close() in internal/homekit/server.go).

Scope:

  • The producer (homekit.Client.Stop) is left as is: there go2rtc is the controller, so closing its own HAP connection is right.
  • The consumer never calls srtp.DelSession for its sessions, while the producer does. I left that out: removing the last session closes the shared UDP listener, so it's a separate change.
  • HomeKit/HKSV: consolidate open fixes, add G.711 talkback #2641 also edits Consumer.Stop (talkback) but keeps c.Connection.Stop(), so one of the two will need a small rebase.

Testing: new TestConsumerStopKeepsControllerConn (pkg/homekit) stops a consumer on a net.Pipe and writes to the conn. Before the fix: Received unexpected error: io: read/write on closed pipe. After: it passes. go test ./pkg/homekit/ ./pkg/hap/... ./internal/homekit/ passes, go vet and gofmt -l are clean.
Environment: master @ 9e1d6ab, go1.27.4, darwin/arm64.

Fixes #2772

Consumer.Stop called core.Connection.Stop, which closes the Transport. For the HomeKit consumer that is the controller's HAP session, so ending a video session dropped the whole connection and the response to the end request failed with "use of closed network connection".

Fixes AlexxIT#2772

Signed-off-by: Ryo Kusnadi <ryokusnadi@gmail.com>
@AlexxIT AlexxIT added kind/bug Reports a defect or fixes one area/homekit HomeKit camera pairing, streaming, and accessory support resolution/declined Maintainer declined the proposal or change in its current form ai Submission contains text or code generated by AI labels Nov 29, 2026
@AlexxIT

AlexxIT commented Nov 29, 2026

Copy link
Copy Markdown
Owner

Thanks! Your AI isn't smart enough. This is wrong fix. You shouldn't remove default c.Connection.Stop().

@AlexxIT AlexxIT closed this Nov 29, 2026
@RyoKusnadi
RyoKusnadi deleted the fix/homekit-stop-keeps-hap-conn branch November 30, 2026 13:05
@RyoKusnadi

Copy link
Copy Markdown
Contributor Author

Ah, got it. I missed that the default c.Connection.Stop() has to stay. Thanks for looking at it and for explaining.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai Submission contains text or code generated by AI area/homekit HomeKit camera pairing, streaming, and accessory support kind/bug Reports a defect or fixes one resolution/declined Maintainer declined the proposal or change in its current form

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix error after homekit viewer stops viewing a stream

2 participants