fix bug when goroutine crashed if lux channel closed before

This commit is contained in:
mykola2312 2025-01-18 10:37:57 +02:00
parent 9e661baad2
commit 80a546b124
2 changed files with 12 additions and 4 deletions

View file

@ -25,6 +25,7 @@ type LuxChannel struct {
Address *net.UDPAddr Address *net.UDPAddr
conn *net.UDPConn conn *net.UDPConn
preconnected bool preconnected bool
control chan bool
} }
// bind udp socket to receive packets // bind udp socket to receive packets
@ -44,6 +45,7 @@ func NewLuxInboundChannel(udpStr string, chType LuxChannelType) (LuxChannel, err
Address: addr, Address: addr,
conn: conn, conn: conn,
preconnected: false, preconnected: false,
control: make(chan bool),
}, nil }, nil
} }

View file

@ -190,6 +190,7 @@ func (r *LuxRouter) CloseChannel(channel *LuxChannel, closeInbound bool) {
if closeInbound { if closeInbound {
for i, ch := range r.inbound { for i, ch := range r.inbound {
if &ch == channel { if &ch == channel {
ch.control <- true
r.inbound = append(r.inbound[:i], r.inbound[:i+1]...) r.inbound = append(r.inbound[:i], r.inbound[:i+1]...)
} }
} }
@ -208,12 +209,17 @@ func channelReceiver(r *LuxRouter, channel *LuxChannel) {
var dgram LuxDatagram var dgram LuxDatagram
var err error var err error
for err == nil { select {
case <-channel.control:
return
default:
dgram, err = channel.Recv() dgram, err = channel.Recv()
dgramChan <- dgram if err == nil {
dgramChan <- dgram
} else {
return
}
} }
r.CloseChannel(channel, true)
} }
func (r *LuxRouter) Start() { func (r *LuxRouter) Start() {