diff --git a/hscontrol/poll.go b/hscontrol/poll.go index 937caee8..5197e731 100644 --- a/hscontrol/poll.go +++ b/hscontrol/poll.go @@ -222,22 +222,22 @@ func (h *Headscale) pollNetMapStream( keepAliveTicker := time.NewTicker(keepAliveInterval) - const chanSize = 8 - updateChan := make(chan types.StateUpdate, chanSize) - h.pollNetMapStreamWG.Add(1) defer h.pollNetMapStreamWG.Done() + const chanSize = 8 + updateChan := make(chan types.StateUpdate, chanSize) + defer closeChanWithLog(updateChan, machine.Hostname, "updateChan") + + // Register the node's update channel + h.nodeNotifier.AddNode(machine.MachineKey, updateChan) + defer h.nodeNotifier.RemoveNode(machine.MachineKey) + ctx := context.WithValue(ctxReq, machineNameContextKey, machine.Hostname) ctx, cancel := context.WithCancel(ctx) defer cancel() - // Register the node's update channel - h.nodeNotifier.AddNode(machine.MachineKey, updateChan) - defer h.nodeNotifier.RemoveNode(machine.MachineKey) - defer closeChanWithLog(updateChan, machine.Hostname, "updateChan") - for { select { case <-keepAliveTicker.C: