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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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);
}
Comment on lines +212 to +215
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

Choose a reason for hiding this comment

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

I think it makes sense, but I think it might not be potentially right to just check Client.Connected.

I might be wrong, but I suspect there are two paths that we care about.

So, I think that if we check on this, we run the risk of not running the handlers during termination. We might need to mark the host as terminating or something similar to differentiate between the two cases and choose when to run the handlers.

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()

}
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