Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
15 changes: 14 additions & 1 deletion src/Components/Server/src/Circuits/CircuitHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ public Task InitializeAsync(ProtectedPrerenderComponentApplicationStore store, A
}));
}

public bool SetDisconnected()
{
if (Client.Connected)
{
Client.SetDisconnected();
return true;
}
return false;
}

// We handle errors in DisposeAsync because there's no real value in letting it propagate.
// We run user code here (CircuitHandlers) and it's reasonable to expect some might throw, however,
// there isn't anything better to do than log when one of these exceptions happens - because the
Expand All @@ -209,7 +219,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
25 changes: 18 additions & 7 deletions src/Components/Server/src/Circuits/CircuitRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ protected virtual bool DisconnectCore(CircuitHost circuitHost, string connection
var result = ConnectedCircuits.TryRemove(circuitId, out circuitHost);
Debug.Assert(result, "This operation operates inside of a lock. We expect the previously inspected value to be still here.");

circuitHost.Client.SetDisconnected();
var previouslyConnected = circuitHost.SetDisconnected();
RegisterDisconnectedCircuit(circuitHost);

Log.CircuitMarkedDisconnected(_logger, circuitId);

return true;
return previouslyConnected;
}

public void RegisterDisconnectedCircuit(CircuitHost circuitHost)
Expand Down Expand Up @@ -309,6 +309,11 @@ private Task PauseAndDisposeCircuitEntry(DisconnectedCircuitEntry entry)

private async Task PauseAndDisposeCircuitHost(CircuitHost circuitHost, bool saveStateToClient)
{
if (circuitHost.SetDisconnected())
{
await circuitHost.Renderer.Dispatcher.InvokeAsync(() => circuitHost.OnConnectionDownAsync(default));
}

await _circuitPersistenceManager.PauseCircuitAsync(circuitHost, saveStateToClient);
circuitHost.UnhandledException -= CircuitHost_UnhandledException;
await circuitHost.DisposeAsync();
Expand Down Expand Up @@ -376,10 +381,12 @@ private void DisposeTokenSource(DisconnectedCircuitEntry entry)
}

// We don't expect this to throw. User code only runs inside DisposeAsync and that does its own error handling.
public ValueTask TerminateAsync(CircuitId circuitId)
public async ValueTask TerminateAsync(CircuitId circuitId)
{
CircuitHost circuitHost;
DisconnectedCircuitEntry entry = default;
var circuitHandlerTask = Task.CompletedTask;

lock (CircuitRegistryLock)
{
if (ConnectedCircuits.TryGetValue(circuitId, out circuitHost) || DisconnectedCircuits.TryGetValue(circuitId.Secret, out entry))
Expand All @@ -388,17 +395,21 @@ public ValueTask TerminateAsync(CircuitId circuitId)
DisconnectedCircuits.Remove(circuitId.Secret);
ConnectedCircuits.TryRemove(circuitId, out _);
Log.CircuitDisconnectedPermanently(_logger, circuitHost.CircuitId);
circuitHost.Client.SetDisconnected();

if (circuitHost.SetDisconnected())
{
circuitHandlerTask = circuitHost.Renderer.Dispatcher.InvokeAsync(() => circuitHost.OnConnectionDownAsync(default));
}
}
}

await circuitHandlerTask;

if (circuitHost != null)
{
circuitHost.UnhandledException -= CircuitHost_UnhandledException;
return circuitHost.DisposeAsync();
await circuitHost.DisposeAsync();
}

return default;
}

// We don't need to do anything with the exception here, logging and sending exceptions to the client
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