FIX deadlock related to blocking channel sending

This commit is contained in:
mykola2312 2025-01-29 04:46:03 +02:00
parent 3051a1b5ea
commit f575ab3cb4
2 changed files with 39 additions and 7 deletions

View file

@ -24,6 +24,9 @@ type LuxNode struct {
state LuxNodeState
stateLock deadlock.RWMutex
subscribers []LuxNodeSubscriber
stopChan chan bool
genlist net.LuxNonceList
dns *LuxDnsServer
@ -36,6 +39,8 @@ func NewLuxNode(nodeKey crypto.LuxKey, ks crypto.LuxKeyStore) LuxNode {
running: false,
neighbors: make(map[proto.LuxID]*ipnet.UDPAddr),
state: NewLuxNodeState(),
subscribers: make([]LuxNodeSubscriber, 0),
stopChan: make(chan bool),
genlist: net.NewLuxNonceList(),
dns: nil,
}
@ -85,6 +90,10 @@ func (node *LuxNode) AddNeighbor(id proto.LuxID, udpAddr string) error {
return nil
}
func (node *LuxNode) AddSubscriber(subscriber LuxNodeSubscriber) {
node.subscribers = append(node.subscribers, subscriber)
}
func (node *LuxNode) AddDnsFrontend(udpListen string) error {
if node.dns == nil {
node.dns = NewLuxDnsServer(node)
@ -232,11 +241,27 @@ func nodeLoop(node *LuxNode) {
}
}
// we need to read state from stateChan, or otherwise sending to it
// will block everything and cause deadlock
func subscriberLoop(node *LuxNode) {
for node.running {
select {
case <-node.stopChan:
return
case state := <-node.state.stateChan:
for _, subscriber := range node.subscribers {
subscriber.HandleStateUpdate(state)
}
}
}
}
func (node *LuxNode) Start() {
node.router.Start()
node.running = true
go nodeLoop(node)
go subscriberLoop(node)
}
func (node *LuxNode) Stop() {
@ -245,6 +270,8 @@ func (node *LuxNode) Stop() {
}
node.running = false
node.stopChan <- true
node.router.Stop()
}

View file

@ -0,0 +1,5 @@
package node
type LuxNodeSubscriber interface {
HandleStateUpdate(state LuxHostState)
}