Skip to content

Conversation

@doromaraujo
Copy link
Collaborator

@doromaraujo doromaraujo commented Dec 1, 2025

This PR addresses the racing condition issue where a thread may set NetworkChangeDetector's listener to null while another is trying to call its methods.

Summary by CodeRabbit

  • Bug Fixes
    • Improved thread safety in network change detection to prevent potential race conditions during network state updates.

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

@coderabbitai
Copy link

coderabbitai bot commented Dec 1, 2025

Walkthrough

The NetworkChangeDetector listener field is made volatile and accessed via local copies within callbacks to prevent race conditions and ensure thread-safe visibility across concurrent threads during network availability and loss events.

Changes

Cohort / File(s) Summary
Thread Safety Enhancement
tool/src/main/java/io/netbird/client/tool/networks/NetworkChangeDetector.java
Made listener field volatile and refactored onAvailable and onLost callbacks to use local references, preventing unsynchronized reads and potential race conditions during network state callbacks.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify volatile modifier properly enforces memory visibility semantics
  • Confirm local copy pattern is correctly applied in both callback methods to eliminate TOCTOU race conditions
  • Validate that null-check logic remains functionally equivalent after refactoring

Poem

🐰 A listener volatile, now truly free,
No race conditions haunting thee,
With local copies, the threads align,
Thread safety sealed, by design divine!
The networks flow without a care,
Safe and sound, with threads to spare!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: fixing a race condition in null-checks for NetworkAvailabilityListener within ConnectivityManager.NetworkCallback callbacks.
✨ 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 bug/on-network-available-called-on-null-listener

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bf4ece6 and e4fa7e5.

📒 Files selected for processing (1)
  • tool/src/main/java/io/netbird/client/tool/networks/NetworkChangeDetector.java (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: build-debug
  • GitHub Check: Analyze (java-kotlin)
🔇 Additional comments (3)
tool/src/main/java/io/netbird/client/tool/networks/NetworkChangeDetector.java (3)

16-16: Correct use of volatile for thread-safe visibility.

Marking the field volatile ensures that updates from subscribe()/unsubscribe() are immediately visible to the callback threads. This is essential for the local copy pattern used in the callbacks.


40-42: Correct local copy pattern to eliminate TOCTOU race.

Copying the volatile field to a local variable before the null check ensures that the same non-null reference is used throughout the callback, preventing the race where listener could be set to null between the check and the method invocation.


47-49: Consistent application of the fix in onLost.

Same correct pattern applied here, ensuring both callbacks are protected from the race condition.


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.

@doromaraujo doromaraujo requested a review from pappz December 2, 2025 12:17
@doromaraujo doromaraujo merged commit 8ba078f into main Dec 3, 2025
7 checks passed
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