Skip to content

Commit 682b4f4

Browse files
fix: avoid panic due to sending on closed channel (#455)
the channel is not closed anymore and before sending results on the channel, we check if the context hasn't terminated yet Co-authored-by: dmissmann <37073203+dmissmann@users.noreply.github.com>
1 parent 4ba4207 commit 682b4f4

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

ios/discover.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ func FindDeviceInterfaceAddress(ctx context.Context, device DeviceEntry) (string
2020
}
2121

2222
result := make(chan string)
23-
defer close(result)
23+
24+
var cancel context.CancelFunc
25+
ctx, cancel = context.WithCancel(ctx)
26+
defer cancel()
2427

2528
for _, iface := range ifaces {
2629
resolver, err := zeroconf.NewResolver(zeroconf.SelectIfaces([]net.Interface{iface}), zeroconf.SelectIPTraffic(zeroconf.IPv6))
@@ -57,13 +60,13 @@ func checkEntry(ctx context.Context, device DeviceEntry, interfaceName string, e
5760
}
5861
print(entry.ServiceInstanceName())
5962
for _, ip6 := range entry.AddrIPv6 {
60-
tryHandshake(ip6, entry.Port, interfaceName, device, result)
63+
tryHandshake(ctx, ip6, entry.Port, interfaceName, device, result)
6164
}
6265
}
6366
}
6467
}
6568

66-
func tryHandshake(ip6 net.IP, port int, interfaceName string, device DeviceEntry, result chan<- string) {
69+
func tryHandshake(ctx context.Context, ip6 net.IP, port int, interfaceName string, device DeviceEntry, result chan<- string) {
6770
addr := fmt.Sprintf("%s%%%s", ip6.String(), interfaceName)
6871
s, err := NewWithAddrPortDevice(addr, port, device)
6972
udid := device.Properties.SerialNumber
@@ -77,6 +80,10 @@ func tryHandshake(ip6 net.IP, port int, interfaceName string, device DeviceEntry
7780
return
7881
}
7982
if udid == h.Udid {
80-
result <- addr
83+
select {
84+
case <-ctx.Done():
85+
slog.Error("failed sending handshake result", "error", ctx.Err())
86+
case result <- addr:
87+
}
8188
}
8289
}

0 commit comments

Comments
 (0)