Skip to content

[client] Fix race condition and ensure correct message ordering in Relay#5265

Merged
pappz merged 2 commits intomainfrom
fix/relay-msg-orders
Feb 9, 2026
Merged

[client] Fix race condition and ensure correct message ordering in Relay#5265
pappz merged 2 commits intomainfrom
fix/relay-msg-orders

Conversation

@pappz
Copy link
Copy Markdown
Collaborator

@pappz pappz commented Feb 5, 2026

Fix race condition and ensure correct message ordering in connection establishment

Reorder operations in OpenConn to register the connection before waiting for peer availability. This ensures:

  • Connection is ready to receive messages before peer subscription completes
  • Transport messages and onconnected events maintain proper ordering
  • No messages are lost during the connection establishment window
  • Concurrent OpenConn calls cannot create duplicate connections

If peer availability check fails, the pre-registered connection is properly cleaned up.

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)

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 connection initialization to track connection containers earlier and ensure partial resources are cleaned up if a remote peer is unavailable.
    • Enhanced error handling and logging around connection setup to make failures and status transitions clearer and reduce resource leaks.

connection establishment

Reorder operations in OpenConn to register the connection before
waiting for peer availability. This ensures:

- Connection is ready to receive messages before peer subscription
completes
- Transport messages and onconnected events maintain proper ordering
- No messages are lost during the connection establishment window
- Concurrent OpenConn calls cannot create duplicate connections

If peer availability check fails, the pre-registered connection is
properly cleaned up.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 5, 2026

📝 Walkthrough

Walkthrough

OpenConn now creates and registers the connection container before checking the remote peer's online status, and removes/closes the container if the peer check fails. Logging was updated to reflect the new initialization and cleanup order.

Changes

Cohort / File(s) Summary
Connection Initialization Reordering
shared/relay/client/client.go
Construct and register the connection container earlier (before remote peer online check); on peer-unavailable errors remove and close the container. Logging messages updated to reflect new sequence.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • lixmal
  • mlsmaycon

Poem

🐇 In burrows I first plant the door,

then peep outside to see who knocks—
if none arrive, I close once more,
neat tunnels tucked from rolling rocks.

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Description check ❓ Inconclusive The description covers the main changes and rationale, but is missing critical information: no issue ticket link, incomplete stack section, and missing explanation for why documentation is not needed. Add the issue ticket number and link, complete the stack section details, and provide a brief explanation for why documentation changes are not required.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: fixing a race condition and ensuring correct message ordering in the Relay client connection establishment.
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
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/relay-msg-orders

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.

@pappz pappz changed the title [client] Fix race condition and ensure correct message ordering in [client] Fix race condition and ensure correct message ordering in Relay Feb 5, 2026
Comment thread shared/relay/client/client.go
Ensure relay connections are properly cleaned up when the service is not running by verifying `serviceIsRunning` and removing stale entries from `c.conns` to prevent unintended behaviors.
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Feb 6, 2026

@pappz pappz requested a review from lixmal February 6, 2026 16:45
@pappz pappz merged commit 6981fdc into main Feb 9, 2026
38 of 39 checks passed
@pappz pappz deleted the fix/relay-msg-orders branch February 9, 2026 10:34
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