Skip to content

HubExtensions.PushAndLockScope can destroy the DisableHub instance #4211

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
jamescrosswell opened this issue May 22, 2025 · 0 comments
Open

Comments

@jamescrosswell
Copy link
Collaborator

Bit of an edge case but I just bumped into this:

public static IDisposable PushAndLockScope(this IHub hub) => new LockedScope(hub);

LockedScope is defined as:

private sealed class LockedScope : IDisposable
{
private readonly IDisposable _scope;
public LockedScope(IHub hub)
{
_scope = hub.PushScope();
hub.LockScope();
}
public void Dispose() => _scope.Dispose();
}

In GlobalMode, hub.PushScope() returns DisabledHub.Instance:

public IDisposable PushScope<TState>(TState? state)
{
if (IsGlobalMode)
{
_options.LogWarning("Push scope called in global mode, returning.");
return DisabledHub.Instance;
}

Ao if someone did something like:

private void OnButtonClick(EventArgs args)
{
    using lockedHub = hub.PushAndLoclScope();
    // Do some work
}

...in GlobalMode, the instance of DisabledHub would be disposed of when this event handler completed.

I'm assuming we would never want that to happen. We could prevent this by returning some kind of FakeDisposable from HubExtensions.PushAndLockScope if GlobalMode was enabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

1 participant