maui icon indicating copy to clipboard operation
maui copied to clipboard

HttpClient and AcceptEncoding gzip on Android stoped working after latest SR4 release.

Open JoacimWall opened this issue 10 months ago • 5 comments

Description

After I updated to latest Dotnet SDK and workloads on my dev machine my app start to return error on some API calls. The same thing happened in Azure Pipeline even though I didn't make any changes to the script.

The app works flawlessly under Ios. So the unchanged code that target MAUI 8.0.6( Also tested target 8.0.20) return the same error.

if I just check de content of the return it's still gzip I think. var test = result.Content.ReadAsStringAsync() on Ios = "{"authentication":{"logintoken":
on Android = "�\b\0\0\0\0\0\0��n�8�_�0��g�T����-ǒc[�,-)Qe�"ɲ�A�f�a_`^l)'��N��3h����!�(�>�����UHҊz��Y�y�s'�

//Joacim

Steps to Reproduce

If I check the logs of the Mac build host in Azure pipeline Pool: Azure Pipelines Image: macOS-13 Agent: Azure Pipelines 3 Started: Today at 20:01 Duration: 10m 39s

I see that Successfully installed .NET Core sdk version 8.0.201. Creating global tool path and pre-pending to PATH. Finishing: Use .Net 8 and the dotnet workload install maui Now installs newer versions of the workload and other parts than it did before, Maybe the error is there somewhere

Link to public reproduction project repository

https://github.com/JoacimWall/Joacim_Bug_Report/tree/main/GzipBug

Version with bug

8.0.20 SR4

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.6 SR1

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

Yes turn of the gzip for Android #if IOS this.client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); #endif

Relevant log output

Writing workload pack installation record for Microsoft.NETCore.App.Runtime.AOT.Cross.net7.iossimulator-x86 version 7.0.18...
Installing pack Microsoft.NETCore.App.Runtime.Mono.ios-arm version 7.0.18...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.Mono.net7.ios-arm version 7.0.18...
Installing pack Microsoft.NETCore.App.Runtime.Mono.ios-arm64 version 7.0.18...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.Mono.net7.ios-arm64 version 7.0.18...
Installing pack Microsoft.NETCore.App.Runtime.Mono.iossimulator-arm64 version 7.0.18...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.Mono.net7.iossimulator-arm64 version 7.0.18...
Installing pack Microsoft.NETCore.App.Runtime.Mono.iossimulator-x64 version 7.0.18...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.Mono.net7.iossimulator-x64 version 7.0.18...
Installing pack Microsoft.NETCore.App.Runtime.Mono.iossimulator-x86 version 7.0.18...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.Mono.net7.iossimulator-x86 version 7.0.18...
Installing pack Microsoft.MacCatalyst.Sdk version 17.2.8053...
Writing workload pack installation record for Microsoft.MacCatalyst.Sdk.net8 version 17.2.8053...
Installing pack Microsoft.MacCatalyst.Sdk version 16.4.7142...
Writing workload pack installation record for Microsoft.MacCatalyst.Sdk.net7 version 16.4.7142...
Installing pack Microsoft.MacCatalyst.Ref version 17.2.8053...
Writing workload pack installation record for Microsoft.MacCatalyst.Ref version 17.2.8053...
Installing pack Microsoft.MacCatalyst.Runtime.maccatalyst-x64 version 17.2.8053...
Writing workload pack installation record for Microsoft.MacCatalyst.Runtime.maccatalyst-x64 version 17.2.8053...
Installing pack Microsoft.MacCatalyst.Runtime.maccatalyst-arm64 version 17.2.8053...
Writing workload pack installation record for Microsoft.MacCatalyst.Runtime.maccatalyst-arm64 version 17.2.8053...
Installing pack Microsoft.MacCatalyst.Templates version 17.2.8053...
Writing workload pack installation record for Microsoft.MacCatalyst.Templates.net8 version 17.2.8053...
Installing pack Microsoft.NETCore.App.Runtime.AOT.osx-x64.Cross.maccatalyst-arm64 version 8.0.4...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.AOT.Cross.maccatalyst-arm64 version 8.0.4...
Installing pack Microsoft.NETCore.App.Runtime.AOT.osx-x64.Cross.maccatalyst-x64 version 8.0.4...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.AOT.Cross.maccatalyst-x64 version 8.0.4...
Installing pack Microsoft.NETCore.App.Runtime.Mono.maccatalyst-arm64 version 8.0.4...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.Mono.maccatalyst-arm64 version 8.0.4...
Installing pack Microsoft.NETCore.App.Runtime.Mono.maccatalyst-x64 version 8.0.4...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.Mono.maccatalyst-x64 version 8.0.4...
Installing pack Microsoft.NETCore.App.Runtime.AOT.osx-x64.Cross.maccatalyst-arm64 version 7.0.18...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.AOT.Cross.net7.maccatalyst-arm64 version 7.0.18...
Installing pack Microsoft.NETCore.App.Runtime.AOT.osx-x64.Cross.maccatalyst-x64 version 7.0.18...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.AOT.Cross.net7.maccatalyst-x64 version 7.0.18...
Installing pack Microsoft.NETCore.App.Runtime.Mono.maccatalyst-arm64 version 7.0.18...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.Mono.net7.maccatalyst-arm64 version 7.0.18...
Installing pack Microsoft.NETCore.App.Runtime.Mono.maccatalyst-x64 version 7.0.18...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.Mono.net7.maccatalyst-x64 version 7.0.18...
Garbage collecting for SDK feature band(s) 8.0.200...

Successfully installed workload(s) maui.


Finishing: Install Maui workload

JoacimWall avatar Apr 11 '24 18:04 JoacimWall

@jonathanpeppers thoughts?

PureWeen avatar Apr 11 '24 22:04 PureWeen

@JoacimWall can you share more complete sample code?

You might be able to test with: https://httpbin.org/

@simonrozsival @grendello could this be related to some of the changes here:

  • https://github.com/xamarin/xamarin-android/compare/34.0.85...34.0.95

jonathanpeppers avatar Apr 12 '24 02:04 jonathanpeppers

@JoacimWall can you share more complete sample code? You might be able to test with: https://httpbin.org/ @simonrozsival @grendello could this be related to some of the changes here:

Hi I am not responsible for the backend so I can't share the code for that. I have now shared how the front end logic works and a dummy backend but this is not the real backend.

https://github.com/JoacimWall/Joacim_Bug_Report/tree/main/GzipBug

I ma running on a Mac and i have not bin able to get the simulator to accept connect to local server.

Hi I have now published a dummy API on Azure (the code is in the repo also). if I debug on real Android device(samsung sm-g96OF (API29)) I get the error now. If you use iOS the api works. //Joacim

JoacimWall avatar Apr 12 '24 06:04 JoacimWall

@jonathanpeppers @grendello This change is probably relevant: https://github.com/xamarin/xamarin-android/pull/8753

@JoacimWall could you please try setting handler.AutomaticDecompression = DecompressionMethods.None; or handler.AutomaticDecompression = DecompressionMethods.GZip; here: https://github.com/JoacimWall/Joacim_Bug_Report/blob/main/GzipBug/GzipClient/HttpsClientHandlerService.cs#L9?

simonrozsival avatar Apr 12 '24 07:04 simonrozsival

It seems like the BCL doesn't have automatic decompression by default:

  • https://github.com/dotnet/runtime/blob/7b534da455fa12c7a19b518ddab059dd70af1fa7/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs#L64
  • https://github.com/dotnet/runtime/blob/7b534da455fa12c7a19b518ddab059dd70af1fa7/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionSettings.cs#L19

So, was AndroidMessageHandler auto decompressing in the past?

It does seem like we should match BCL behavior, just unfortunate it changed in a service release.

jonathanpeppers avatar Apr 12 '24 13:04 jonathanpeppers

Hello I do not understand what you need for more information. I think that I can't do more to solve this bug. I reported this bug when something that worked in the previous version of MAUI now stopped working, so that others that encounter the same error could know what the issue is. We have 3 MAUI customer apps in production that are still on version 8.0.6 because there are new bugs in the new SRs that mean we cannot update.

//Regards Joacim

JoacimWall avatar May 02 '24 05:05 JoacimWall

@JoacimWall I noticed that in the GzipBug repro project you set the default Accept-Encoding headers here: https://github.com/JoacimWall/Joacim_Bug_Report/blob/main/GzipBug/GzipClient/RestClient.cs#L68-L70. The better way to do this is by setting the AutomaticDecompression property:

var handler = new HttpClientHandler
{
    AutomaticDecompression = DecompressionMethods.Gzip | DecompressionMethods.Deflate,
};
var client = new HttpClient(handler);
// ...

When I change the default AcceptEncoding headers for AutomaticDecompression, the repro app works correctly on Android. Does this solve your issue?

I'm slightly confused that you report that this used to work on Android in MAUI 8.0.6. I thought I might have broken the default behavior in https://github.com/xamarin/xamarin-android/pull/8753 but after revisiting it, I can't find a way that this could have been affected.

simonrozsival avatar May 02 '24 08:05 simonrozsival

@JoacimWall I noticed that in the GzipBug repro project you set the default Accept-Encoding headers here: https://github.com/JoacimWall/Joacim_Bug_Report/blob/main/GzipBug/GzipClient/RestClient.cs#L68-L70. The better way to do this is by setting the AutomaticDecompression property:

var handler = new HttpClientHandler
{
    AutomaticDecompression = DecompressionMethods.Gzip | DecompressionMethods.Deflate,
};
var client = new HttpClient(handler);
// ...

When I change the default AcceptEncoding headers for AutomaticDecompression, the repro app works correctly on Android. Does this solve your issue?

I'm slightly confused that you report that this used to work on Android in MAUI 8.0.6. I thought I might have broken the default behavior in xamarin/xamarin-android#8753 but after revisiting it, I can't find a way that this could have been affected.

Hi As I write above, this is from a production app where everything works, then we update to the latest version of MAUI no code changes and it stops working, so some type of breaking change. It's good that you can get around this by defining a specific HttpHandler or remove the GZIP row. But as you can see, this is not done in the example code, instead we run the standard declaration new Httpclient(); I think that I am not the only one that has built an app where we just do var client = new HttpClient(); and try to add
this.client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));

I spent some hours to find what it was that generate the error create I sample and report this issue. So others could skip spending the hours to find the root issue.

//Joacim

JoacimWall avatar May 02 '24 09:05 JoacimWall

@JoacimWall thanks for the additional details.

@grendello do you have an idea if we could have broken this with https://github.com/xamarin/xamarin-android/pull/8753 or what else could have caused this regression?

simonrozsival avatar May 02 '24 12:05 simonrozsival

This issue was moved to xamarin/xamarin-android#8928

PureWeen avatar May 07 '24 08:05 PureWeen