Skip to content

Fix NetworkHelper re-entry guard and add Reset()#397

Merged
josesimoes merged 2 commits into
nanoframework:mainfrom
josesimoes:improvements-network-helper
May 29, 2026
Merged

Fix NetworkHelper re-entry guard and add Reset()#397
josesimoes merged 2 commits into
nanoframework:mainfrom
josesimoes:improvements-network-helper

Conversation

@josesimoes
Copy link
Copy Markdown
Member

Description

  • Scope re-entry guard to event-based setup path only, so token-based callers can retry after timeout/failure.
  • Add public Reset() to clear all state and allow reconfiguration from scratch.
  • Improve AddressChangedCallback to track IP loss and re-acquisition, resetting NetworkReady accordingly.
  • Add Reconnecting status enum value.
  • New tests covering retry-after-timeout and reset-then-restart scenarios.
  • Migrate all test assertions to TestFramework v3 API.
  • Update README with NetworkHelper usage guidance.

Motivation and Context

How Has This Been Tested?

Screenshots

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dependencies (update dependencies and changes associated, has no impact on code or features)
  • Unit Tests (add new Unit Test(s) or improved existing one(s), has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist:

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).
  • I have added new tests to cover my changes.

- Scope re-entry guard to event-based setup path only, so token-based callers can retry after timeout/failure.
- Add public `Reset()` to clear all state and allow reconfiguration from scratch.
- Improve `AddressChangedCallback` to track IP loss and re-acquisition, resetting `NetworkReady` accordingly.
- Add `Reconnecting` status enum value.
- New tests covering retry-after-timeout and reset-then-restart scenarios.
- Migrate all test assertions to TestFramework v3 API.
- Update README with `NetworkHelper` usage guidance.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 29, 2026

Review Change Stack

Warning

Review limit reached

@josesimoes, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 27 minutes and 14 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: nanoframework/coderabbit/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 02811fe1-a82d-4beb-ae09-609565e372e3

📥 Commits

Reviewing files that changed from the base of the PR and between 055d4fe and 338aab3.

📒 Files selected for processing (4)
  • README.md
  • Tests/NetworkHelperTests/ConnectToEthernetTests.cs
  • nanoFramework.System.Net/NetworkHelper/NetworkHelper.cs
  • nanoFramework.System.Net/NetworkHelper/NetworkHelperStatus.cs
📝 Walkthrough

Walkthrough

This PR adds a public Reset() method to NetworkHelper for full reconfiguration, changes reconnection signaling to detect IP loss and enter a Reconnecting state, updates integration tests, and migrates test assertion APIs across the suite from xUnit to MSTest style.

Changes

NetworkHelper reset and reconnection feature

Layer / File(s) Summary
Status enum and public API documentation
nanoFramework.System.Net/NetworkHelper/NetworkHelperStatus.cs, nanoFramework.System.Net/NetworkHelper/NetworkHelper.cs
Adds Reconnecting enum member and updates XML documentation for Reset(), SetupNetworkHelper, and SetupAndConnectNetwork to clarify that event-based setup is single-use without reset and token-based setup is retryable.
Reset() method implementation
nanoFramework.System.Net/NetworkHelper/NetworkHelper.cs
Replaces internal ResetInstance() with a new public Reset() method that deregisters the NetworkAddressChanged event handler and reinitializes flags, signals, status, and configuration storage.
Reconnection signaling and SetupHelper wiring
nanoFramework.System.Net/NetworkHelper/NetworkHelper.cs
Changes AddressChangedCallback to set Reconnecting status and block NetworkReady signal when IP is lost; re-signals availability only when DateTime requirement is met. Reorganizes SetupHelper conditional logic while preserving handler registration flow.
ConnectToEthernetTests updated for Reset() API
Tests/NetworkHelperTests/ConnectToEthernetTests.cs
Updates integration tests to call the new public Reset() method, adds explicit Assert.IsTrue() checks around NetworkReady.WaitOne(), and adjusts test comments to reflect the new signal-state expectations.
NetworkHelper usage documentation
README.md
Documents token-based (retryable) and event-based (single-use) connection patterns, NetworkReady transient behavior during disconnection/reconnection, and full reconfiguration via Reset() including the InvalidOperationException on re-call without reset.
Test assertion API migration across suite
Tests/IPAddressTests/IPAddressTests.cs, Tests/SocketTests/SocketExceptionsTests.cs, Tests/SocketTests/SocketOptionsTests.cs
Converts assertion calls from xUnit style (Assert.NotNull, Assert.Equal, Assert.True, Assert.Throws) to MSTest style (Assert.IsNotNull, Assert.AreEqual, Assert.IsTrue, Assert.ThrowsException) across multiple test classes with no change to test logic.

🎯 3 (Moderate) | ⏱️ ~20 minutes

Type: documentation

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main changes: fixing a re-entry guard in NetworkHelper and adding a Reset() method. It is 50 characters, under the 50-character guideline, and directly related to the core changeset without a trailing period.
Description check ✅ Passed The pull request description is directly related to the changeset, providing clear context, motivation, and detailed breakdown of changes including scope clarification, new Reset() method, callback improvements, enum additions, tests, and documentation updates.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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


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.

Copy link
Copy Markdown

@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: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@nanoFramework.System.Net/NetworkHelper/NetworkHelper.cs`:
- Around line 141-153: Reset() currently clears shared static state while
background worker (WorkingThread) and event handler (AddressChangedCallback) may
still be running; add a generation token or CancellationTokenSource that both
WorkingThread and AddressChangedCallback check and update atomically,
set/trigger cancellation and wait for the worker to exit (synchronize/Join/Wait)
before clearing fields, and only deregister AddressChangedCallback after
confirming callbacks are quiescent; update Reset(), WorkingThread(), and
AddressChangedCallback() to honor the token/cancellation and to avoid touching
shared fields when the generation/token has changed.

In `@nanoFramework.System.Net/NetworkHelper/NetworkHelperStatus.cs`:
- Around line 45-48: The new enum member Reconnecting was inserted before
ExceptionOccurred which changes ExceptionOccurred's numeric value; update
NetworkHelperStatus so existing numeric values are preserved by either moving
Reconnecting to the end of the enum declaration or by assigning explicit integer
values to all members (e.g., keep the original numeric value for
ExceptionOccurred) — adjust the enum definition (NetworkHelperStatus,
Reconnecting, ExceptionOccurred) accordingly to maintain wire compatibility for
consumers.

In `@README.md`:
- Line 66: Remove the stray bare Markdown heading "##" from the README (the
empty level-2 heading) by either deleting that line or replacing it with an
appropriate section title so the README renders correctly; search for the lone
"##" string in the file and update that location accordingly.

In `@Tests/NetworkHelperTests/ConnectToEthernetTests.cs`:
- Around line 104-110: The test currently allows a stale NetworkHelper.Status to
satisfy the assertion, so capture the pre-call state, call
NetworkHelper.SetupAndConnectNetwork(token: cs2.Token) into secondResult, and
then assert a concrete terminal outcome (e.g., secondResult indicates success or
timeout) instead of just Status != NetworkHelperStatus.None; alternatively
measure elapsed time with a Stopwatch around the second call to assert it waited
at least the expected timeout window (approx 10000ms minus small slack) and then
verify NetworkHelper.Status reflects the post-call terminal state. Use the
symbols NetworkHelper.SetupAndConnectNetwork, secondResult, cs2,
NetworkHelper.Status and NetworkHelperStatus.None to locate and update the
assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: nanoframework/coderabbit/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: c2ed67c5-000a-4329-88b1-2282b3c501a9

📥 Commits

Reviewing files that changed from the base of the PR and between 8d26f1d and 055d4fe.

📒 Files selected for processing (7)
  • README.md
  • Tests/IPAddressTests/IPAddressTests.cs
  • Tests/NetworkHelperTests/ConnectToEthernetTests.cs
  • Tests/SocketTests/SocketExceptionsTests.cs
  • Tests/SocketTests/SocketOptionsTests.cs
  • nanoFramework.System.Net/NetworkHelper/NetworkHelper.cs
  • nanoFramework.System.Net/NetworkHelper/NetworkHelperStatus.cs

Comment thread nanoFramework.System.Net/NetworkHelper/NetworkHelper.cs
Comment thread nanoFramework.System.Net/NetworkHelper/NetworkHelperStatus.cs Outdated
Comment thread README.md Outdated
Comment thread Tests/NetworkHelperTests/ConnectToEthernetTests.cs Outdated
@josesimoes josesimoes force-pushed the improvements-network-helper branch from af70432 to 338aab3 Compare May 29, 2026 10:28
@josesimoes
Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 29, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@josesimoes josesimoes merged commit 32e2b7e into nanoframework:main May 29, 2026
8 checks passed
@josesimoes josesimoes deleted the improvements-network-helper branch May 29, 2026 10:38
josesimoes added a commit that referenced this pull request May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants