fix: high cpu when iface remove#1670
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Neo - PR Security ReviewNo security issues found Comment |
Mzack9999
left a comment
There was a problem hiding this comment.
Thanks for the fix! The PacketSource approach is cleaner and does resolve the immediate CPU spinning issue by leveraging gopacket v1.2.0's internal 5ms sleep on errors.
However, there are two concerns with the current approach:
-
Goroutine leak on interface removal:
pcap.NextErrorReadErrordoesn't match any of gopacket's known unrecoverable errors, sopacketsToChannelwill never break — it will sleep-and-retry indefinitely. The CPU spike is fixed, but the goroutine silently lingers for the lifetime of the process. -
No cancellation support:
Packets()usescontext.Background()internally, so there's no way to stop the read loop on shutdown. gopacket v1.2.0 providesPacketsCtx(ctx)— wiring that to a cancellable context (e.g., from the scanner lifecycle) would allow clean termination.
Suggested change: use PacketsCtx(ctx) instead of Packets(), where ctx is derived from the scanner's shutdown signal. This would fix both the goroutine leak and enable graceful cleanup:
packetSource := gopacket.NewPacketSource(handler, handler.LinkType())
for packet := range packetSource.PacketsCtx(ctx) {
data := packet.Data()
// ...
}This way, when the scanner shuts down (or the interface is known to be gone), cancelling the context will cleanly terminate the read loop.
|
Thank you for reviewing, I will make corrections. |
|
@Mzack9999 I have carefully reviewed the two issues you mentioned, and I have found that they cannot be resolved:
|
related #1669