You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using TradingView inside HybridWebView. I noticed that sometimes when I go back (from page that have this view) my app crash.
I started to look for a problem in MAUI code, and I found it XD
So HybridWebViewHandler for Android is using MauiHybridWebViewClient. Most interesting method for us is ShouldInterceptRequest which use GetResponseStream.
Inside this method we have a case for InvokeDotNetPath
// 1. Try special InvokeDotNet pathif(relativePath==HybridWebViewHandler.InvokeDotNetPath){varfullUri=newUri(fullUrl!);varinvokeQueryString=HttpUtility.ParseQueryString(fullUri.Query);varcontentBytesTask=Handler.InvokeDotNetAsync(invokeQueryString);varresponseStream=newDotNetInvokeAsyncStream(contentBytesTask,Handler);returnnewWebResourceResponse("application/json","UTF-8",200,"OK",GetHeaders("application/json"),responseStream);}
Okey, now lets look inside constructor of DotNetInvokeAsyncStream which is private class inside this class and is implementation of Stream
Looks good ? Then lets look inside InvokeMethodAndWriteBytes()
privateasyncvoidInvokeMethodAndWriteBytes(){try{vardata=await_task;// the stream or handler may be disposed after the method completesObjectDisposedException.ThrowIf(_isDisposed,nameof(DotNetInvokeAsyncStream));ArgumentNullException.ThrowIfNull(Handler,nameof(Handler));// copy the data into the pipeif(datais not null&&data.Length>0){varmemory=_pipe.Writer.GetMemory(data.Length);data.CopyTo(memory);_pipe.Writer.Advance(data.Length);}_pipe.Writer.Complete();}catch(Exceptionex){Handler?.MauiContext?.CreateLogger<HybridWebViewHandler>()?.LogError(ex,"Error invoking .NET method from JavaScript: {ErrorMessage}",ex.Message);_pipe.Writer.Complete(ex);}}
Really, async void? Here we have first mistake. Lets go to another - what if we are on page with HWV and go to another when the _task is running ? In other words, what if we disconnect hander during doing _task?
As we can see in the code ArgumentNullException.ThrowIfNull(Handler, nameof(Handler)) will throw exception then we write exception to pipe _pipe.Writer.Complete(ex). This actually seems fine, but lets go back to InvokeDotNetPath
Description
I am using TradingView inside HybridWebView. I noticed that sometimes when I go back (from page that have this view) my app crash.
I started to look for a problem in MAUI code, and I found it XD
So
HybridWebViewHandler
for Android is usingMauiHybridWebViewClient
. Most interesting method for us isShouldInterceptRequest
which useGetResponseStream
.Inside this method we have a case for
InvokeDotNetPath
Okey, now lets look inside constructor of
DotNetInvokeAsyncStream
which is private class inside this class and is implementation ofStream
Looks good ? Then lets look inside
InvokeMethodAndWriteBytes()
Really,
async void
? Here we have first mistake. Lets go to another - what if we are on page with HWV and go to another when the_task
is running ? In other words, what if we disconnect hander during doing_task
?As we can see in the code
ArgumentNullException.ThrowIfNull(Handler, nameof(Handler))
will throw exception then we write exception to pipe_pipe.Writer.Complete(ex)
. This actually seems fine, but lets go back toInvokeDotNetPath
So responseStream return stream with Exception inside and then pass it to
WebResourceResponse
. But whatWebResourceResponse
actually do?Ohh, so we pass stream with exception to Android API.... And what this cause ? A native exception that we can't handle so app crash.
Please, re write this class, because this code is some kind of mistake.
Steps to Reproduce
I didn't do any minimal version, but i guess if you delay that task, and perform go back this will happened
Link to public reproduction project repository
No response
Version with bug
9.0.50 SR5
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
No response
Did you find any workaround?
No, but i guess that returning
_pipe.Writer.Complete();
instead of_pipe.Writer.Complete(ex);
will do.Relevant log output
The text was updated successfully, but these errors were encountered: