[PR #5424] [MERGED] [client] fix busy-loop in network monitor routing socket on macOS/BSD #25853

Closed
opened 2026-08-05 07:06:33 -04:00 by saavagebueno · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbirdio/netbird/pull/5424
Author: @pappz
Created: 2/23/2026
Status: Merged
Merged: 2/23/2026
Merged by: @pappz

Base: mainHead: fix/network-monitor-routing-socket-busy-loop


📝 Commits (2)

  • 4da7a60 [client] fix busy-loop in network monitor routing socket on macOS/BSD
  • 8417e6f [client] add fd range validation in waitReadable to prevent out-of-bound errors

📊 Changes

1 file changed (+72 additions, -37 deletions)

View changed files

📝 client/internal/networkmonitor/check_change_common.go (+72 -37)

📄 Description

After system wakeup, the AF_ROUTE socket created by Go's unix.Socket() is non-blocking, causing unix.Read to return EAGAIN immediately and spin at 100% CPU filling the log with thousands of warnings per second.

Replace the tight read loop with a unix.Select call that blocks until the fd is readable, checking ctx cancellation on each 1-second timeout. Fatal errors (EBADF, EINVAL) now return an error instead of looping.

Describe your changes

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__

Summary by CodeRabbit

  • Bug Fixes
    • Improved network route-change monitoring by replacing busy-wait polling with a blocking wait to reduce CPU usage and latency.
    • Added robust error handling and message validation to avoid spurious failures when reading route updates.
    • Better handling of route add/delete events, including matching deletions against configured nexthops and periodic context re-checks for reliability.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbirdio/netbird/pull/5424 **Author:** [@pappz](https://github.com/pappz) **Created:** 2/23/2026 **Status:** ✅ Merged **Merged:** 2/23/2026 **Merged by:** [@pappz](https://github.com/pappz) **Base:** `main` ← **Head:** `fix/network-monitor-routing-socket-busy-loop` --- ### 📝 Commits (2) - [`4da7a60`](https://github.com/netbirdio/netbird/commit/4da7a60e83583823c5643eb227d03d030fbb7b03) [client] fix busy-loop in network monitor routing socket on macOS/BSD - [`8417e6f`](https://github.com/netbirdio/netbird/commit/8417e6f461abc0b426c5f9014558c1c88edd31a5) [client] add fd range validation in waitReadable to prevent out-of-bound errors ### 📊 Changes **1 file changed** (+72 additions, -37 deletions) <details> <summary>View changed files</summary> 📝 `client/internal/networkmonitor/check_change_common.go` (+72 -37) </details> ### 📄 Description After system wakeup, the AF_ROUTE socket created by Go's unix.Socket() is non-blocking, causing unix.Read to return EAGAIN immediately and spin at 100% CPU filling the log with thousands of warnings per second. Replace the tight read loop with a unix.Select call that blocks until the fd is readable, checking ctx cancellation on each 1-second timeout. Fatal errors (EBADF, EINVAL) now return an error instead of looping. ## Describe your changes ## Issue ticket number and link ## Stack <!-- branch-stack --> ### Checklist - [x] Is it a bug fix - [ ] Is a typo/documentation fix - [ ] Is a feature enhancement - [ ] It is a refactor - [ ] Created tests that fail without the change (if possible) > By submitting this pull request, you confirm that you have read and agree to the terms of the [Contributor License Agreement](https://github.com/netbirdio/netbird/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT.md). ## Documentation Select exactly one: - [ ] I added/updated documentation for this change - [x] Documentation is **not needed** for this change (explain why) ### Docs PR URL (required if "docs added" is checked) Paste the PR link from https://github.com/netbirdio/docs here: https://github.com/netbirdio/docs/pull/__ <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved network route-change monitoring by replacing busy-wait polling with a blocking wait to reduce CPU usage and latency. * Added robust error handling and message validation to avoid spurious failures when reading route updates. * Better handling of route add/delete events, including matching deletions against configured nexthops and periodic context re-checks for reliability. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
saavagebueno added the pull-request label 2026-08-05 07:06:33 -04:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: DYNR/netbird#25853