Skip to content
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

fix: debounce using ImmediateDelay can throw an exception because of a race condition with PeriodicTimer. #2030

Closed
ApacheTech opened this issue May 9, 2024 · 1 comment
Assignees
Labels
bug A bug

Comments

@ApacheTech
Copy link
Contributor

ApacheTech commented May 9, 2024

🐛 Bug Report

Using the FluentSearch input box, but likely any debounce with ImmediateDelay, you can reliably force it to throw an exception, by firing the debounce while the PeriodicTimer is resetting itself. The exception shows that the CancellationToken used to reset the timer is set to cancelled, as the timer tries to tick.

💻 Repro or Code Sample

@code {
    private string _searchTerm;
    private string? _results;

    private async Task OnSearchTermChangedAsync(string searchTerm)
    {
        _results = null;
        _searchTerm = searchTerm;
        var delay = (Random.Shared.Next(2, 5) * 100);
        await Task.Delay(delay);
        _results = $"{_searchTerm}: {delay}";
        StateHasChanged();
    }
}

<FluentSearch Immediate
              ImmediateDelay="300"
              Value="_searchTerm"
              ValueChanged="OnSearchTermChangedAsync" />

@if (!string.IsNullOrEmpty(_results))
{
    @_results;
}

🤔 Expected Behavior

The debouncing should work as if using the internal debouncer.

😯 Current Behavior

Occasionally (but predicably, and repeatedly) throws an exception saying the PeriodicTimer used to debounce the ValueChanged handler cannot be fired because it is in a cancelled state. This seems to be when the debounce is triggered at the same time the previous operation is finished.

💁 Possible Solution

I have applied a workaround by using FluentUI's internal debouncer. I've copied this file into my application to expose it for use. Within your repo, it is internal. This works as expected, and I've had no issues with it, thus far. If debouncing is done via this, there should be no issues.

🔦 Context

I'm creating an API Docs Reference site, using Blazor8 InteractiveServer, OrchardCore CMS, FluentUI, Heroicons, and Tailwind. The FluentSearch is being used on the search page, streaming results to the page, as the user enters search terms. The search uses Lucene/Elasticsearch, using locally cached indexes.

🌍 Your Environment

OS: Windows 11
Browsers: Firefox, Chrome, Opera, Edge
Framework: .NET CoreCLR 8.0
FluentUI: v4.7.2
OrchardCore: v1.8.3

@microsoft-github-policy-service microsoft-github-policy-service bot added the triage New issue. Needs to be looked at label May 9, 2024
@ApacheTech ApacheTech changed the title fix: debounce using ImmediateDelay can throw an exeption because of a race condition with PeriodicTimer. fix: debounce using ImmediateDelay can throw an exception because of a race condition with PeriodicTimer. May 9, 2024
@vnbaaij vnbaaij added bug A bug and removed triage New issue. Needs to be looked at labels May 9, 2024
ApacheTech added a commit to ApacheTech-Forked-Repos/fluentui-blazor that referenced this issue May 14, 2024
…tion because of a race condition with PeriodicTimer.
@dvoituron
Copy link
Collaborator

Will be fixed using #2042. Many thanks @ApacheTech :-)

vnbaaij pushed a commit that referenced this issue May 15, 2024
…uncing `ValueChanged` handler with `ImmediateDelay`. (#2042)

* fix(#2030):  debounce using ImmediateDelay can throw an exception because of a race condition with PeriodicTimer.

* feat: add `FluentSearch` demo for immediate use with debounce

* chore: minor code clean for consistency
vnbaaij pushed a commit that referenced this issue May 15, 2024
…uncing `ValueChanged` handler with `ImmediateDelay`. (#2042)

* fix(#2030):  debounce using ImmediateDelay can throw an exception because of a race condition with PeriodicTimer.

* feat: add `FluentSearch` demo for immediate use with debounce

* chore: minor code clean for consistency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug
Projects
None yet
Development

No branches or pull requests

3 participants