-
-
Notifications
You must be signed in to change notification settings - Fork 362
refactor(NetworkMonitor): add try/finally keep release simaphore slim #6496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Reviewer's GuideEnsure the semaphore is always released by wrapping the network module initialization in a try/finally and removing the redundant release outside the block. Sequence diagram for semaphore handling in RegisterStateChangedCallbacksequenceDiagram
participant Component
participant DefaultNetworkMonitorService
participant SemaphoreSlim
participant NetworkModule
Component->>DefaultNetworkMonitorService: RegisterStateChangedCallback()
DefaultNetworkMonitorService->>SemaphoreSlim: WaitAsync(3000)
alt !_init
DefaultNetworkMonitorService->>DefaultNetworkMonitorService: try
DefaultNetworkMonitorService->>NetworkModule: LoadModuleByName("net")
NetworkModule-->>DefaultNetworkMonitorService: Module instance
DefaultNetworkMonitorService->>NetworkModule: InvokeVoidAsync("init", ...)
DefaultNetworkMonitorService->>SemaphoreSlim: finally Release()
else _init
DefaultNetworkMonitorService->>SemaphoreSlim: Release()
end
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @ArgoZhang - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `src/BootstrapBlazor/Services/DefaultNetworkMonitorService.cs:61` </location>
<code_context>
if (!_init)
{
- _init = true;
+ try
+ {
+ _init = true;
- _networkModule ??= await _runtime.LoadModuleByName("net");
- await _networkModule.InvokeVoidAsync("init", new
+ _networkModule ??= await _runtime.LoadModuleByName("net");
+ await _networkModule.InvokeVoidAsync("init", new
+ {
+ Invoke = _interop,
+ OnNetworkStateChangedCallback = nameof(TriggerNetworkStateChanged)
+ });
+ }
+ finally
{
- Invoke = _interop,
</code_context>
<issue_to_address>
The try/finally block now only releases the semaphore if _init was false.
If _init is true, the semaphore is not released, which may cause deadlocks on subsequent RegisterStateChangedCallback calls. Ensure _semaphoreSlim.Release() is always called to prevent this.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| try | ||
| { | ||
| _init = true; | ||
|
|
||
| _networkModule ??= await _runtime.LoadModuleByName("net"); | ||
| await _networkModule.InvokeVoidAsync("init", new | ||
| _networkModule ??= await _runtime.LoadModuleByName("net"); | ||
| await _networkModule.InvokeVoidAsync("init", new | ||
| { | ||
| Invoke = _interop, | ||
| OnNetworkStateChangedCallback = nameof(TriggerNetworkStateChanged) | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): The try/finally block now only releases the semaphore if _init was false.
If _init is true, the semaphore is not released, which may cause deadlocks on subsequent RegisterStateChangedCallback calls. Ensure _semaphoreSlim.Release() is always called to prevent this.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6496 +/- ##
========================================
Coverage ? 100.00%
========================================
Files ? 714
Lines ? 31387
Branches ? 4431
========================================
Hits ? 31387
Misses ? 0
Partials ? 0
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Link issues
fixes #6495
Summary By Copilot
Regression?
Risk
Verification
Packaging changes reviewed?
☑️ Self Check before Merge
Summary by Sourcery
Ensure DefaultNetworkMonitorService always releases its semaphore by wrapping the initialization logic in a try/finally and removing redundant Release calls.
Bug Fixes:
Enhancements: