Skip to content

[management] fix ephemeral peers hadn't been added to ephemeral list on login #5165

Merged
crn4 merged 2 commits intomainfrom
fix/ephemeral-peers
Jan 23, 2026
Merged

[management] fix ephemeral peers hadn't been added to ephemeral list on login #5165
crn4 merged 2 commits intomainfrom
fix/ephemeral-peers

Conversation

@crn4
Copy link
Copy Markdown
Contributor

@crn4 crn4 commented Jan 23, 2026

Describe your changes

Issue ticket number and link

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)

bug fix

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

  • New Features
    • Enhanced ephemeral peer management with automatic tracking and cleanup when temporary peers fail to synchronize or remain unexpectedly connected.

✏️ Tip: You can customize this high-level summary in your review settings.

Copilot AI review requested due to automatic review settings January 23, 2026 16:00
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 23, 2026

📝 Walkthrough

Walkthrough

Introduces ephemeral peer tracking by adding a new TrackEphemeralPeer method to the network map controller interface and its implementation. The AddPeer method signature is extended with a temporary flag parameter, enabling conditional tracking of temporary peers during addition.

Changes

Cohort / File(s) Change Summary
Network Map Controller Interface
management/internals/controllers/network_map/interface.go
Adds new TrackEphemeralPeer(ctx context.Context, peer *nbpeer.Peer) method to the Controller interface, expanding the public API surface for ephemeral peer tracking.
Network Map Controller Implementation
management/internals/controllers/network_map/controller/controller.go
Implements new TrackEphemeralPeer method that forwards calls to EphemeralPeersManager.OnPeerDisconnected.
Network Map Mock
management/internals/controllers/network_map/interface_mock.go
Updates MockController with new TrackEphemeralPeer mock method and corresponding recorder for gomock integration.
Peer Management
management/server/peer.go
Modifies AddPeer signature to include temporary bool parameter. Adds conditional tracking call for temporary peers via networkMapController.TrackEphemeralPeer.

Sequence Diagram

sequenceDiagram
    participant Client as Server/Client
    participant PM as PeerManager
    participant Ctrl as Network Map Controller
    participant EPM as EphemeralPeersManager
    
    Client->>PM: AddPeer(ctx, accountID, setupKey, userID, peer, temporary=true)
    PM->>PM: Add peer to All group
    alt temporary flag is true
        PM->>Ctrl: TrackEphemeralPeer(ctx, peer)
        Ctrl->>EPM: OnPeerDisconnected(ctx, peer)
        EPM->>EPM: Register peer for cleanup tracking
    end
    PM-->>Client: Return peer, networkMap, posture checks
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • pascal-fischer

Poem

🐰 A hop through the code, four files aligned,
New tracking for peers of ephemeral kind!
Controllers and servers in harmony dance,
Temporary guests get their cleanup chance! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description check ⚠️ Warning The description follows the template structure with checklist completed, but lacks critical details: no description of changes, no issue ticket link, and no explanation of why documentation is not needed. Add a 'Describe your changes' section explaining the fix, provide the issue ticket number and link, and briefly explain why documentation isn't needed for this bug fix.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main bug fix: enabling tracking of ephemeral peers during login, which aligns with the code changes adding TrackEphemeralPeer integration.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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.

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes a bug where ephemeral peers weren't being added to the ephemeral tracking list when they were first created during login/registration. Without this fix, ephemeral peers that were created but never connected wouldn't be scheduled for automatic cleanup.

Changes:

  • Added tracking of ephemeral peers immediately upon creation in AddPeer method
  • Introduced new TrackEphemeralPeer method to the network map controller interface to handle ephemeral peer tracking
  • Updated mock interface to include the new method

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
management/server/peer.go Added call to track ephemeral peers when they are created, ensuring they are scheduled for cleanup if never connected
management/internals/controllers/network_map/interface.go Added new TrackEphemeralPeer method to the Controller interface
management/internals/controllers/network_map/controller/controller.go Implemented TrackEphemeralPeer method that delegates to the ephemeral manager
management/internals/controllers/network_map/interface_mock.go Updated generated mock to include the new interface method

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

func (c *Controller) DisconnectPeers(ctx context.Context, accountId string, peerIDs []string) {
c.peersUpdateManager.CloseChannels(ctx, peerIDs)
}

Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

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

The method TrackEphemeralPeer is calling OnPeerDisconnected, which is semantically misleading. The intent here is to track a newly created ephemeral peer that hasn't connected yet, but calling OnPeerDisconnected suggests the peer was previously connected and then disconnected. While the functionality works correctly (adding the peer to the tracking list for cleanup), the naming creates confusion about the actual behavior.

Consider either:

  1. Renaming TrackEphemeralPeer to something more descriptive like TrackNewEphemeralPeer or MarkEphemeralPeerForTracking
  2. Adding the ephemeral manager's method directly to the interface (e.g., exposing a method like TrackUnconnectedEphemeralPeer)
  3. Adding a clarifying comment explaining that this method is used for newly created peers that haven't connected yet
Suggested change
// TrackEphemeralPeer registers a newly created ephemeral peer for tracking/cleanup.
// Note: the peer may not have connected yet; we intentionally reuse OnPeerDisconnected
// to add it to the ephemeral peers manager's tracking list.

Copilot uses AI. Check for mistakes.
Comment thread management/server/peer.go
}

if temporary {
// we should track ephemeral peers to be able to clean them if the peer don't sync and be marked as connected
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

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

The comment states "if the peer don't sync and be marked as connected" which has a grammatical issue. Consider rephrasing to "if the peer doesn't sync and get marked as connected" or "if the peer doesn't connect and sync".

Suggested change
// we should track ephemeral peers to be able to clean them if the peer don't sync and be marked as connected
// we should track ephemeral peers to be able to clean them if the peer doesn't sync and get marked as connected

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
management/server/peer.go (1)

547-547: All AddPeer call sites correctly updated with the temporary argument; however, move TrackEphemeralPeer outside the transaction.

The TrackEphemeralPeer call at line 731 is invoked inside the database transaction block. If the transaction fails or rolls back after this call, the tracking state will be inconsistent with the database. Move this call outside the transaction to execute only after successful commit.

Current code structure (lines 710–740)
err = am.Store.ExecuteInTransaction(ctx, func(transaction store.Store) error {
    // ... transaction operations ...
    if temporary {
        am.networkMapController.TrackEphemeralPeer(ctx, newPeer)  // ← side-effect inside transaction
    }
    // ... more operations ...
})
🤖 Fix all issues with AI agents
In `@management/server/peer.go`:
- Around line 731-734: The TrackEphemeralPeer call is being invoked inside the
DB transaction, causing side effects if the transaction rolls back and extending
lock time; move the am.networkMapController.TrackEphemeralPeer(ctx, newPeer)
invocation out of the transaction commit path so it runs only after the
transaction successfully commits—locate the block that checks the temporary
boolean and the transaction scope in peer.go around where newPeer is created and
call TrackEphemeralPeer(ctx, newPeer) after the transaction completes (using the
same temporary/newPeer variables and ctx).

Comment thread management/server/peer.go
@crn4 crn4 merged commit 737d606 into main Jan 23, 2026
45 of 47 checks passed
@crn4 crn4 deleted the fix/ephemeral-peers branch January 23, 2026 17:05
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.

3 participants