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

Exception thrown if FocusAsync() and @onfocusin are used in custom input element at the same time - blazor Wasm #30070

Closed
DCIW opened this issue Feb 10, 2021 · 7 comments · Fixed by #32017
Assignees
Labels
affected-few This issue impacts only small number of customers area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed feature-blazor-component-model Any feature that affects the component model for Blazor (Parameters, Rendering, Lifecycle, etc) investigate severity-major This label is used by an internal tool
Milestone

Comments

@DCIW
Copy link

DCIW commented Feb 10, 2021

In a Blazor WebAssembly project

Simultaneously setting of focus and using of the razor component event onfocusin throws an exception:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Event 3 is already tracked
Error: Event 3 is already tracked

I want to create an input element that is focused after the first render. At the same time, I want to do further work once the element receives focus (from the user or programmatically) using @onfocusin.

For these reasons, my input element now looks like this:

@inherits InputText

<input type="text"
       @bind-value="CurrentValue"
       @ref="_inputReference"
       @onfocusin="FocusInHandler" />

@code {
        private ElementReference _inputReference;

        protected async override Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
            {
                await SetFocus();
            }

            await base.OnAfterRenderAsync(firstRender);
        }

        public async Task SetFocus()
        {
            await _inputReference.FocusAsync();
        }

        async Task FocusInHandler(FocusEventArgs e)
        {
            ...
        }

The exception is thrown as soon as the focus is set (for the demo project its when the page containing the input is loaded).

To Reproduce

Demo project

The Exception

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Event 3 is already tracked
      Error: Event 3 is already tracked
          at e.add (https://localhost:44390/_framework/blazor.webassembly.js:1:42456)
          at e.setListener (https://localhost:44390/_framework/blazor.webassembly.js:1:40690)
          at e.applyAttribute (https://localhost:44390/_framework/blazor.webassembly.js:1:36857)
          at e.insertElement (https://localhost:44390/_framework/blazor.webassembly.js:1:35691)
          at e.insertFrame (https://localhost:44390/_framework/blazor.webassembly.js:1:34562)
          at e.applyEdits (https://localhost:44390/_framework/blazor.webassembly.js:1:32948)
          at e.updateComponent (https://localhost:44390/_framework/blazor.webassembly.js:1:32264)
          at Object.t.renderBatch (https://localhost:44390/_framework/blazor.webassembly.js:1:12129)
          at Object.window.Blazor._internal.renderBatch (https://localhost:44390/_framework/blazor.webassembly.js:1:61899)
          at Object.w [as invokeJSFromDotNet] (https://localhost:44390/_framework/blazor.webassembly.js:1:64421)
Microsoft.JSInterop.JSException: Event 3 is already tracked
Error: Event 3 is already tracked
    at e.add (https://localhost:44390/_framework/blazor.webassembly.js:1:42456)
    at e.setListener (https://localhost:44390/_framework/blazor.webassembly.js:1:40690)
    at e.applyAttribute (https://localhost:44390/_framework/blazor.webassembly.js:1:36857)
    at e.insertElement (https://localhost:44390/_framework/blazor.webassembly.js:1:35691)
    at e.insertFrame (https://localhost:44390/_framework/blazor.webassembly.js:1:34562)
    at e.applyEdits (https://localhost:44390/_framework/blazor.webassembly.js:1:32948)
    at e.updateComponent (https://localhost:44390/_framework/blazor.webassembly.js:1:32264)
    at Object.t.renderBatch (https://localhost:44390/_framework/blazor.webassembly.js:1:12129)
    at Object.window.Blazor._internal.renderBatch (https://localhost:44390/_framework/blazor.webassembly.js:1:61899)
    at Object.w [as invokeJSFromDotNet] (https://localhost:44390/_framework/blazor.webassembly.js:1:64421)
   at Microsoft.JSInterop.WebAssembly.WebAssemblyJSRuntime.InvokeUnmarshalled[Int32,RenderBatch,Object,Object](String identifier, Int32 arg0, RenderBatch arg1, Object arg2, Int64 targetInstanceId)
   at Microsoft.JSInterop.WebAssembly.WebAssemblyJSRuntime.InvokeUnmarshalled[Int32,RenderBatch,Object](String identifier, Int32 arg0, RenderBatch arg1)
   at Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer.UpdateDisplayAsync(RenderBatch& batch)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()

Further technical details

  • version: .NET 5.0.3
  • dotnet --info:

.NET SDK (gemäß "global.json"):
Version: 5.0.103
Commit: 72dec52dbd

Laufzeitumgebung:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.103\

Host (useful for support):
Version: 5.0.3
Commit: c636bbdc8a

.NET SDKs installed:
3.1.406 [C:\Program Files\dotnet\sdk]
5.0.100-rc.2.20479.15 [C:\Program Files\dotnet\sdk]
5.0.100 [C:\Program Files\dotnet\sdk]
5.0.103 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.0-rc.2.20475.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.0-rc.2.20475.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.0-rc.2.20475.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

  • Visual Studio Professional 2019 v. 16.8.5
@DCIW DCIW changed the title Exception thrown if FocusAsync() and @onfocusin are used in custom input element at the same time Exception thrown if FocusAsync() and @onfocusin are used in custom input element at the same time - blazor Wasm Feb 10, 2021
@mkArtakMSFT mkArtakMSFT added area-blazor Includes: Blazor, Razor Components investigate labels Feb 10, 2021
@mkArtakMSFT mkArtakMSFT added this to the Next sprint planning milestone Feb 10, 2021
@ghost
Copy link

ghost commented Feb 10, 2021

Thanks for contacting us.
We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@stsrki
Copy link
Contributor

stsrki commented Feb 12, 2021

The same thing is reported for Blazorise Megabit/Blazorise#1823

But by the look of it, it seems this is the Blazor issue.

@SteveSandersonMS SteveSandersonMS added affected-few This issue impacts only small number of customers bug This issue describes a behavior which is not expected - a bug. severity-major This label is used by an internal tool labels Feb 19, 2021 — with ASP.NET Core Issue Ranking
@jan-johansson-mr
Copy link

jan-johansson-mr commented Mar 8, 2021

I second that bug, I've included another sample based on little more than the starter Blazor WebAssembly template. You'll find the sample at Blazor issue with focus trap.
Thanks

@stsrki
Copy link
Contributor

stsrki commented Mar 25, 2021

Happening also with our Blazorise components. Megabit/Blazorise#1823

ABP framework is also affected. https://github.com/volosoft/volo/issues/5807

It's a pretty serious bug and I don't think affected-few label is valid anymore.

@anddrzejb
Copy link

I hit the same bug while working on Ant Design Blazor. @SteveSandersonMS looking at this it seems to be affecting a lot of component libraries. Blazorise, AntDesign & MudBlazor have in total almost 480K nuget downloads (ABP has 4500K but probably only a fraction of that is for blazor related projects). It is not a huge number but I would not consider that "a few" either. This bug is also present in core6.0 preview 3. In my case the bug does not surface when I run my app in Firefox. It does show in Chrome & Edge.

@javiercn javiercn added the feature-blazor-component-model Any feature that affects the component model for Blazor (Parameters, Rendering, Lifecycle, etc) label Apr 20, 2021
@SteveSandersonMS
Copy link
Member

SteveSandersonMS commented Apr 21, 2021

I'm working on a fix for this in 6.0-preview5, but a quick workaround for people currently affected is to add an await Task.Yield() before you call FocusAsync, e.g., updating the code posted above:

        protected async override Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
            {
                await Task.Yield();
                await _inputReference.FocusAsync();
            }
        }

@stsrki
Copy link
Contributor

stsrki commented Apr 21, 2021

I can confirm Task.Yield(); helps as a workaround, for now.

Thank you @SteveSandersonMS

@ghost ghost added Done This issue has been fixed and removed Working labels Apr 22, 2021
@ghost ghost locked as resolved and limited conversation to collaborators May 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affected-few This issue impacts only small number of customers area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed feature-blazor-component-model Any feature that affects the component model for Blazor (Parameters, Rendering, Lifecycle, etc) investigate severity-major This label is used by an internal tool
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants