SteveSandersonMS / WebWindow

.NET Core library to open native OS windows containing web UI on Windows, Mac, and Linux. Experimental.
Apache License 2.0
1.99k stars 215 forks source link

Two bound events shortly firing after eachother trigger concurrency bug. #88

Open jspuij opened 4 years ago

jspuij commented 4 years ago

Minimal Repro here:

https://github.com/jspuij/WebWindow/tree/eventbug

The keydown and oninput events fire shortly after eachother and trigger some sort of concurrency issue. The error message to the console is:

Uncaught (in promise) Error: System.ArgumentException: There is no event handler associated with this event. EventId: '7'. (Parameter 'eventHandlerId')
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync(/UInt64 eventHandlerId, EventFieldInfo fieldInfo, EventArgs eventArgs)
   at WebWindows.Blazor.JSInteropMethods.DispatchEvent(/WebEventDescriptor eventDescriptor, String eventArgsJson)
    at Object.endInvokeDotNetFromJS (Microsoft.JSInterop.js:162)
    at Boot.Desktop.ts:33
    at IPC.ts:44
    at Array.forEach (<anonymous>)
    at IPC.ts:44
    at EventTarget.<anonymous> (<anonymous>:1:202)
    at <anonymous>:1:650
    at <anonymous>:1:676

What seems to happen is that the renderer does not like asynchronous dispatch of multiple events in such short order. I've written the IPC messages to the debugger, and the order seems to be:

ipc:BeginInvokeDotNetFromJS ["1","WebWindow","DispatchEvent",0,"[{\"browserRendererId\":0,\"eventHandlerId\":3,\"eventArgsType\":\"keyboard\",\"eventFieldInfo\":{\"componentId\":6,\"fieldValue\":\"\"}},\"{\\\"type\\\":\\\"keydown\\\",\\\"key\\\":\\\"Unidentified\\\",\\\"code\\\":\\\"\\\",\\\"location\\\":0,\\\"repeat\\\":false,\\\"ctrlKey\\\":false,\\\"shiftKey\\\":false,\\\"altKey\\\":false,\\\"metaKey\\\":false}\"]"]
ipc:BeginInvokeDotNetFromJS ["2","WebWindow","DispatchEvent",0,"[{\"browserRendererId\":0,\"eventHandlerId\":4,\"eventArgsType\":\"change\",\"eventFieldInfo\":{\"componentId\":6,\"fieldValue\":\"a\"}},\"{\\\"type\\\":\\\"input\\\",\\\"value\\\":\\\"a\\\"}\"]"]
JS.RenderBatch:[0,"BgAAAAEAAAADAAAAAQAAAAAAAAD/////AQAAAAAAAAABAAAAAwAAAAAAAAD/////BQAAAAAAAAAAAAAAAQAAAAQAAAAAAAAAB29uaW5wdXRIAAAAGAAAACAAAAA4AAAAPAAAAFAAAAA="]
JS.EndInvokeDotNet:["1",true]
JS.EndInvokeDotNet:["2",false,"System.ArgumentException: There is no event handler associated with this event. EventId: \u00274\u0027.

What I think happens is that either the render or first EndInvokeDotNet clears up all the remaining eventHandlerIds, and subsequently the second EndInvokeDotNet fails. This halts the updating of the binding. (keydown is processed as it's the first event).

I'm not familiar enough with the inner workings of the renderer and dispatcher to tackle this easily myself.