From e7b2f042cff822b7f6ed3f48fe964d16ecf5ac4d Mon Sep 17 00:00:00 2001 From: dmachard <5562930+dmachard@users.noreply.github.com> Date: Thu, 16 May 2024 08:20:51 +0200 Subject: [PATCH] fix: to avoid deadlock on XDP stopping --- workers/sniffer_xdp.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/workers/sniffer_xdp.go b/workers/sniffer_xdp.go index 3bc64a42..8a1c87ab 100644 --- a/workers/sniffer_xdp.go +++ b/workers/sniffer_xdp.go @@ -75,6 +75,7 @@ func (w *XDPSniffer) StartCollect() { ctx, cancel := context.WithCancel(context.Background()) done := make(chan struct{}) + stopChan := make(chan struct{}) go func(ctx context.Context) { defer func() { @@ -165,7 +166,11 @@ func (w *XDPSniffer) StartCollect() { dm.DNS.Length = len(dm.DNS.Payload) } - dnsChan <- dm + select { + case <-stopChan: + return + case dnsChan <- dm: + } } } }(ctx) @@ -175,6 +180,7 @@ func (w *XDPSniffer) StartCollect() { case <-w.OnStop(): w.LogInfo("stop to listen...") cancel() + close(stopChan) <-done return