maui icon indicating copy to clipboard operation
maui copied to clipboard

Preventing NullReferenceException on WebAuthenticatorIntermediateActi…

Open ederbond opened this issue 2 years ago • 11 comments

Description of Change

Preventing random NullReferenceException on WebAuthenticatorIntermediateActivity.android.cs

ederbond avatar Dec 29 '22 20:12 ederbond

Hey there @ederbond! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

ghost avatar Dec 29 '22 20:12 ghost

Hello, does this also apply to NetworkOnMainThreadException, wich appears randomly on Authentication for Rest APIs? Greetings

LAXStudios avatar Dec 29 '22 20:12 LAXStudios

Hi @LAXStudios, I'm not sure. I just got a NullReferenceException that was happening randomly when I try do authenticate with Identity Server.

ederbond avatar Dec 29 '22 20:12 ederbond

/azp run

jfversluis avatar Dec 30 '22 12:12 jfversluis

Azure Pipelines successfully started running 2 pipeline(s).

azure-pipelines[bot] avatar Dec 30 '22 12:12 azure-pipelines[bot]

Any news about this @jfversluis and @mattleibow ?

ederbond avatar Jan 23 '23 22:01 ederbond

@ederbond did you actually verify this works? I can imagine that maybe this line might now break because this value is going to be null?

https://github.com/dotnet/maui/pull/12347/files#diff-9dd7f7fa057499c8357247f9f40fe5abc4890c315fc8451563c48b840c9af101R46

Still, this wouldn't be worse than what is happening now. Just wondering if we have a full solution here.

jfversluis avatar Feb 01 '23 11:02 jfversluis

Hi @jfversluis I didn't tested it cause I don't know how to compile a modifield version of .NET MAUI locally and use it on my own Maui Project. But this is a random error that happen time to time when I try to authenticate using OAuth. The stacktrace that Visual Studio 2022 shows me, make it cristal clear that there was a NullReference exception on that line.

BTW I couldn't navigate the line you asked me a question on your previous reply: This link doesn't righlight the line you're refering to on your previous reply: https://github.com/dotnet/maui/pull/12347/files#diff-9dd7f7fa057499c8357247f9f40fe5abc4890c315fc8451563c48b840c9af101R46

ederbond avatar Feb 01 '23 16:02 ederbond

I don't know how to compile a modifield version of .NET MAUI locally and use it on my own Maui Project.

This might be helpful https://github.com/dotnet/maui/blob/main/.github/DEVELOPMENT.md#compile-using-a-local-bindotnet, see:

dotnet cake --sln="<download_directory>\MauiApp2\MauiApp2.sln" --target=VS

It does not affect your system if you follow the "Compile using a local bin\dotnet" section. If you are on Windows, then https://github.com/dotnet/maui/issues/12762#issuecomment-1406656020 is important too.

MartyIX avatar Feb 01 '23 16:02 MartyIX

Azure Pipelines successfully started running 2 pipeline(s).

azure-pipelines[bot] avatar Feb 17 '23 12:02 azure-pipelines[bot]

Thank you for your pull request. We are auto-formating your source code to follow our code guidelines.

github-actions[bot] avatar Mar 01 '23 15:03 github-actions[bot]

/azp run

mattleibow avatar Mar 07 '23 17:03 mattleibow

Azure Pipelines successfully started running 2 pipeline(s).

azure-pipelines[bot] avatar Mar 07 '23 17:03 azure-pipelines[bot]

Hi, @mattleibow is it going to be merged? Time to time I bump into an app crash because of this. But since it's a random error it's hard to reproduce, I'm quite confident this PR will fix the issue.

ederbond avatar Mar 08 '23 01:03 ederbond

We're currently not planning on backporting this because we don't have a repro and we haven't tested this fix. We feel there is most likely still an issue inside the OnResume call that will occur. Once we can test this fix we will reconsider backporting.

PureWeen avatar Mar 09 '23 15:03 PureWeen

While that my MAUI app will still be randomly crashing most of the time when I ask it to logoff. I'm using <PackageReference Include="IdentityModel.OidcClient" Version="5.2.1" /> to authenticate against my Identity Server.

Sometimes when I call IdentityModel.OidcClient.LogoutAsync() it crashes with a NullReferenceException on that line that I have changed on this PR. But since it doesn't happen very oftem (random) it's hard to create a repro test.

ederbond avatar Mar 09 '23 17:03 ederbond

Unfortunately, this pull request does not solve the whole problem. I was able to debug the behavior a bit closer. When I set the Opera browser as default, no custom tabs are used. The default browser is started.

https://github.com/dotnet/maui/blob/main/src/Essentials/src/WebAuthenticator/WebAuthenticator.android.cs#L78

This probably causes that the activity is not started and so the intent extras are null.

https://github.com/dotnet/maui/blob/main/src/Essentials/src/WebAuthenticator/WebAuthenticatorIntermediateActivity.android.cs

The null check is not sufficient as suspected. if(extras == null) { return; } https://github.com/dotnet/maui/pull/12347/commits/104b87b5bda520ca315f437fef7a2d0640f0193c

I was able to fix it with a null check.

https://github.com/dotnet/maui/blob/main/src/Essentials/src/WebAuthenticator/WebAuthenticatorIntermediateActivity.android.cs#L46

StartActivity(actualIntent ?? Intent);

Maybe someone could test this with the Mi Browser.

Px7-941 avatar Mar 17 '23 20:03 Px7-941

I would like to test the workaround @Px7-941 shared. However, I added the Activity class: WebAuthenticatorIntermediateActivity.cs in the /Platforms/Android folder using same Maui namespace to see if get overwritten without luck.

do you have any idea how to overwrite it in any .NEt MAUI app, so I can test using the Mi Browser?

Thanks in advance

vhugogarcia avatar Apr 11 '23 17:04 vhugogarcia

Does anyone have a sample test case to illustrate the original problem or the problem that @PureWeen alludes to potentially existing in issue #13978 ? And is it the same issue that @Px7-941 is hitting?

jstedfast avatar May 09 '23 21:05 jstedfast