brutella / hap

The HomeKit Accessory Protocol (hap) implemented in Go
Apache License 2.0
355 stars 43 forks source link

Usage example from readme cannot gracefully exit the app #41

Closed mcsakoff closed 11 months ago

mcsakoff commented 1 year ago

OS: MacOS Sonoma, MacOS Ventrura hap v0.0.27

Calling cancel() doesn't make it gracefully exit from server.ListenAndServe(ctx).

I added some logging

    ctx, cancel := context.WithCancel(context.Background())
    go func() {
        <-c
        // Stop delivering signals.
        signal.Stop(c)
        println("stopping")
        // Cancel the context to stop the server.
        cancel()
    }()

    // Run the server.
    println("starting")
    if err = server.ListenAndServe(ctx); err != nil {
        println(err.Error())
    }
    println("stopped")

Result:

starting
^Cstopping
^C

Application exits with return value 130 after second Crtl+C.

PS: Linux is not affected

mcsakoff commented 1 year ago

Traced down to this line:

https://github.com/brutella/hap/blob/98079d5315240e20da559d80c3bc8c69473fc46e/server.go#L246

It never returns from the function call even when context is canceled. Looks like the issue must be addressed to brutella/dnssd.

mcsakoff commented 1 year ago

Workaround

Provide one specific interface:

server.Ifaces = []string{"en0"}
brutella commented 12 months ago

This may be fixed by https://github.com/brutella/dnssd/pull/43

Please check out hap v0.0.28 which includes this change.

mcsakoff commented 11 months ago

hap v0.0.28 dnssd v1.2.10

Fixed. Discovery works fine. I suppose that #33 and #34 can be closed as well.