[Fixed] memory leak

This commit is contained in:
Skyxim 2022-03-13 18:35:55 +08:00
parent 9c5b184db6
commit 002163f07b

View file

@ -92,7 +92,8 @@ func NewAdapter(device dev.TunDevice, conf config.Tun, tcpIn chan<- C.ConnContex
r.Complete(true) r.Complete(true)
return return
} }
r.Complete(false)
defer r.Complete(false)
conn := gonet.NewTCPConn(&wq, ep) conn := gonet.NewTCPConn(&wq, ep)
@ -107,6 +108,7 @@ func NewAdapter(device dev.TunDevice, conf config.Tun, tcpIn chan<- C.ConnContex
target := getAddr(ep.Info().(*stack.TransportEndpointInfo).ID) target := getAddr(ep.Info().(*stack.TransportEndpointInfo).ID)
tcpIn <- inbound.NewSocket(target, conn, C.TUN) tcpIn <- inbound.NewSocket(target, conn, C.TUN)
}) })
ipstack.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpFwd.HandlePacket) ipstack.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpFwd.HandlePacket)
// UDP handler // UDP handler
@ -194,6 +196,7 @@ func (t *gvisorAdapter) AsLinkEndpoint() (result stack.LinkEndpoint, err error)
log.Errorln("can not read from tun: %v", err) log.Errorln("can not read from tun: %v", err)
continue continue
} }
var p tcpip.NetworkProtocolNumber var p tcpip.NetworkProtocolNumber
switch header.IPVersion(packet) { switch header.IPVersion(packet) {
case header.IPv4Version: case header.IPv4Version:
@ -204,14 +207,20 @@ func (t *gvisorAdapter) AsLinkEndpoint() (result stack.LinkEndpoint, err error)
log.Warnln("invalid IP version:%d", header.IPVersion(packet)) log.Warnln("invalid IP version:%d", header.IPVersion(packet))
continue continue
} }
if linkEP.IsAttached() { if linkEP.IsAttached() {
linkEP.InjectInbound(p, stack.NewPacketBuffer(stack.PacketBufferOptions{ pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
Data: buffer.View(packet[:n]).ToVectorisedView(), Data: buffer.View(packet[:n]).ToVectorisedView(),
})) })
linkEP.InjectInbound(p, pkt)
// release memory
pkt.DecRef()
} else { } else {
log.Debugln("received packet from tun when %s is not attached to any dispatcher.", t.device.Name()) log.Debugln("received packet from tun when %s is not attached to any dispatcher.", t.device.Name())
} }
} }
t.wg.Done() t.wg.Done()
t.Close() t.Close()
log.Debugln("%v stop read loop", t.device.Name()) log.Debugln("%v stop read loop", t.device.Name())