Skip to content

fix: high cpu when iface remove#1670

Open
akkuman wants to merge 1 commit intoprojectdiscovery:mainfrom
akkuman:fix/high-cpu
Open

fix: high cpu when iface remove#1670
akkuman wants to merge 1 commit intoprojectdiscovery:mainfrom
akkuman:fix/high-cpu

Conversation

@akkuman
Copy link
Copy Markdown
Contributor

@akkuman akkuman commented Mar 31, 2026

related #1669

@auto-assign auto-assign bot requested a review from dogancanbakir March 31, 2026 03:21
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 31, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d9f9c5a2-1093-4c0a-b0e5-8635e648777a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@neo-by-projectdiscovery-dev
Copy link
Copy Markdown

neo-by-projectdiscovery-dev bot commented Mar 31, 2026

Neo - PR Security Review

No security issues found

Comment @pdneo help for available commands. · Open in Neo

Copy link
Copy Markdown
Member

@Mzack9999 Mzack9999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Goroutine leak on interface removal: pcap.NextErrorReadError doesn't match any of gopacket's known unrecoverable errors, so packetsToChannel will never break — it will sleep-and-retry indefinitely. The CPU spike is fixed, but the goroutine silently lingers for the lifetime of the process.

  2. No cancellation support: Packets() uses context.Background() internally, so there's no way to stop the read loop on shutdown. gopacket v1.2.0 provides PacketsCtx(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.

@akkuman
Copy link
Copy Markdown
Contributor Author

akkuman commented Apr 1, 2026

Thank you for reviewing, I will make corrections.

@akkuman
Copy link
Copy Markdown
Contributor Author

akkuman commented Apr 1, 2026

@Mzack9999 I have carefully reviewed the two issues you mentioned, and I have found that they cannot be resolved:

  1. When pcap.NextErrorReadError is thrown, it's not actually possible to determine whether the problem is caused by the disappearance of the virtual network interface or by some other reason for the read error. Therefore, the official implementation of gopacket reflects the expected behavior and is not a goroutine leak.
  2. This function was originally designed to execute throughout the entire program lifecycle, so no context was passed in to the outer function, making it impossible to call PacketsCtx(ctx).

@akkuman akkuman requested a review from Mzack9999 April 3, 2026 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants