Skip to content

[blazor] fix multiple calls to OnConnectionDownAsync #62518

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix multiple calls to OnConnectionDownAsync
  • Loading branch information
pavelsavara committed Jul 1, 2025
commit ebf208ff8a2e0f09842bb580601a709f393a614f
5 changes: 4 additions & 1 deletion src/Components/Server/src/Circuits/CircuitHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ await Renderer.Dispatcher.InvokeAsync(async () =>

try
{
await OnConnectionDownAsync(CancellationToken.None);
if (this.Client.Connected)
{
await OnConnectionDownAsync(CancellationToken.None);
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is the right change to fix the metric issue. It's changing an existing behavior of the system that users might be relying on.

Copy link
Member Author

Choose a reason for hiding this comment

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

this event is called twice. That's a bug not a feature

Copy link
Member

Choose a reason for hiding this comment

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

What is causing the 2nd call?

Copy link
Member Author

Choose a reason for hiding this comment

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

The proper event is Hub.OnDisconnect()

CircuitHost.OnConnectionDownAsync() is called from CircuitHost.DisposeAsync() regardless if it's connected or not, creating dis-balance.

Image

Image

Copy link
Member Author

Choose a reason for hiding this comment

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

@javiercn does this make sense or you have another proposal for the fix ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I just tested the "close tab" and "navigate to non-interactive page". Both behave as expected, that is, they call the event once.

Copy link
Member

Choose a reason for hiding this comment

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

Terminate it's not being fired in those cases.

  • Close tab will likely don't cause unload to fire.
  • navigate to non-interactive page
    • It takes a bit of time for the client to start the circuit disposal.
    • If for some reason it's not happening, it's also a bug.

https://github.com/dotnet/aspnetcore/blob/main/src/Components/Server/src/CircuitDisconnectMiddleware.cs#L81-L87

https://github.com/dotnet/aspnetcore/blob/main/src/Components/Server/src/Circuits/CircuitRegistry.cs#L391

If we check for the circuit to be connected it won't fire when we want to eagerly terminate the circuit.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we need to call that event from places that call circuitHost.Client.SetDisconnected() rather than relying that circuitHost.DisposeAsync() would always call it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also I'm not sure that the new PauseAndDisposeCircuitHost is calling SetDisconnected()

Copy link
Member Author

Choose a reason for hiding this comment

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

I made the calls to OnConnectionDownAsync more explicit now, instead of relying on the DisposeAsync.

@javiercn please review again.

}
catch
{
Expand Down
5 changes: 0 additions & 5 deletions src/Components/Server/src/Circuits/CircuitMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ public void OnCircuitDown(long startTimestamp, long currentTimestamp)
_circuitActiveCounter.Add(-1);
}

if (_circuitConnectedCounter.Enabled)
{
_circuitConnectedCounter.Add(-1);
}

if (_circuitDuration.Enabled)
{
var duration = Stopwatch.GetElapsedTime(startTimestamp, currentTimestamp);
Expand Down
9 changes: 4 additions & 5 deletions src/Components/Server/test/Circuits/CircuitMetricsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,11 @@ public async Task OnCircuitDown_UpdatesCountersAndRecordsDuration()

// Assert
var activeMeasurements = activeCircuitCounter.GetMeasurementSnapshot();
var connectedMeasurements = connectedCircuitCounter.GetMeasurementSnapshot();
var durationMeasurements = circuitDurationCollector.GetMeasurementSnapshot();

Assert.Single(activeMeasurements);
Assert.Equal(-1, activeMeasurements[0].Value);

Assert.Single(connectedMeasurements);
Assert.Equal(-1, connectedMeasurements[0].Value);

Assert.Single(durationMeasurements);
Assert.True(durationMeasurements[0].Value > 0);
}
Expand Down Expand Up @@ -162,7 +158,10 @@ public void FullCircuitLifecycle_RecordsAllMetricsCorrectly()
// 4. Connection re-established
circuitMetrics.OnConnectionUp();

// 5. Circuit closes
// 5. Connection drops
circuitMetrics.OnConnectionDown();

// 6. Circuit closes
Thread.Sleep(10); // Add a small delay to ensure a measurable duration
var endTime = Stopwatch.GetTimestamp();
circuitMetrics.OnCircuitDown(startTime, endTime);
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy