Repository: dotnet/aspnetcore
File: src/Components/Server/src/Circuits/CircuitRegistry.cs (C#)

Code before:
            {
                // Only pause and persist the circuit state if it has been active at some point,
                // meaning that the client called UpdateRootComponents on it.
                var circuitHost = entry.CircuitHost;
                return PauseAndDisposeCircuitHost(circuitHost, saveStateToClient: false);
            }
            else
            {
                Log.PersistedCircuitStateDiscarded(_logger, entry.CircuitHost.CircuitId);
            }
        }
        catch (Exception ex)
        {
            Log.UnhandledExceptionDisposingCircuitHost(_logger, ex);
        }

        return Task.CompletedTask;
    }

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

    internal async Task PauseCircuitAsync(
    CircuitHost circuitHost,
    string connectionId)
    {
        try
        {
            Log.CircuitPauseStarted(_logger, circuitHost.CircuitId, connectionId);

            Task pauseTask;
            lock (CircuitRegistryLock)
            {
                pauseTask = PauseCore(circuitHost, connectionId);
            }
            await pauseTask;
        }
        catch (Exception)
        {
            Log.CircuitPauseFailed(_logger, circuitHost.CircuitId, connectionId);
        }
    }

Review comment (suggestion):
This is too early to be doing this.

`CircuitPersistenceManager` needs to send the state to the client and needs a connected circuit to do that.

Code after:
            {
                // Only pause and persist the circuit state if it has been active at some point,
                // meaning that the client called UpdateRootComponents on it.
                var circuitHost = entry.CircuitHost;
                return PauseAndDisposeCircuitHost(circuitHost, saveStateToClient: false);
            }
            else
            {
                Log.PersistedCircuitStateDiscarded(_logger, entry.CircuitHost.CircuitId);
            }
        }
        catch (Exception ex)
        {
            Log.UnhandledExceptionDisposingCircuitHost(_logger, ex);
        }

        return Task.CompletedTask;
    }

    private async Task PauseAndDisposeCircuitHost(CircuitHost circuitHost, bool saveStateToClient)
    {
        await _circuitPersistenceManager.PauseCircuitAsync(circuitHost, saveStateToClient);
        circuitHost.UnhandledException -= CircuitHost_UnhandledException;
        circuitHost.Client.SetDisconnected();
        await circuitHost.DisposeAsync();
    }

    internal async Task PauseCircuitAsync(
    CircuitHost circuitHost,
    string connectionId)
    {
        try
        {
            Log.CircuitPauseStarted(_logger, circuitHost.CircuitId, connectionId);

            Task pauseTask;
            lock (CircuitRegistryLock)
            {
                pauseTask = PauseCore(circuitHost, connectionId);
            }
            await pauseTask;
        }
        catch (Exception)
        {
            Log.CircuitPauseFailed(_logger, circuitHost.CircuitId, connectionId);
        }
    }

    internal virtual Task PauseCore(CircuitHost circuitHost, string connectionId)
    {
        var circuitId = circuitHost.CircuitId;