Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

afpacket sniffer: fix deadlock on stop #842

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions workers/sniffer_afpacket_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,24 @@ func (w *AfpacketSniffer) StartCollect() {

// defrag ipv4
go netutils.IPDefragger(fragIP4Chan, udpChan, tcpChan, w.GetConfig().Collectors.AfpacketLiveCapture.Port)

// defrag ipv6
go netutils.IPDefragger(fragIP6Chan, udpChan, tcpChan, w.GetConfig().Collectors.AfpacketLiveCapture.Port)

// tcp assembly
go netutils.TCPAssembler(tcpChan, dnsChan, 0)

// udp processor
go netutils.UDPProcessor(udpChan, dnsChan, 0)

ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func(ctx context.Context) {
defer func() {
dnsProcessor.Stop()
netutils.RemoveBpfFilter(w.fd)
syscall.Close(w.fd)
w.LogInfo("read data terminated")
defer close(done)
close(done)
}()

buf := make([]byte, 65536)
Expand All @@ -133,7 +135,7 @@ func (w *AfpacketSniffer) StartCollect() {
select {
case <-ctx.Done():
w.LogInfo("stopping sniffer...")
syscall.Close(w.fd)
// syscall.Close(w.fd)
return
default:
var fdSet syscall.FdSet
Expand All @@ -144,7 +146,7 @@ func (w *AfpacketSniffer) StartCollect() {
if errors.Is(err, syscall.EINTR) {
continue
}
panic(err)
w.LogFatal(pkgconfig.PrefixLogWorker+"["+w.GetName()+"select", err)
}
if nReady == 0 {
continue
Expand Down Expand Up @@ -227,11 +229,19 @@ func (w *AfpacketSniffer) StartCollect() {

// tcp or udp packets ?
if packet.TransportLayer().LayerType() == layers.LayerTypeUDP {
udpChan <- packet
select {
case <-ctx.Done():
return
case udpChan <- packet:
}
}

if packet.TransportLayer().LayerType() == layers.LayerTypeTCP {
tcpChan <- packet
select {
case <-ctx.Done():
return
case tcpChan <- packet:
}
}
}
}
Expand Down
Loading