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

[Bug] HttpClient.PostAsync() doesn't work on iOS #13920

Closed
avorobjovs opened this issue Jan 27, 2022 · 4 comments
Closed

[Bug] HttpClient.PostAsync() doesn't work on iOS #13920

avorobjovs opened this issue Jan 27, 2022 · 4 comments

Comments

@avorobjovs
Copy link

avorobjovs commented Jan 27, 2022

I create a Xamarin.Forms app and run it on Windows (UWP), Android, and iOS.
I debug my app on devices from Visual Studio on Windows (using the Hot Restart for iOS).

Now I found the issue with a calling of the HttpClient.PostAsync() method on iOS. But the HttpClient.GetAsync() method works fine as expected.
And HttpClient.PostAsync() method also works as expected on UWP and Android.

Steps to Reproduce

Create any Xamarin.Forms or Xamarin.iOS app. Use the following code:

var client = new System.Net.Http.HttpClient();
var response = await client.PostAsync(<some_url>, <some-content>);

Expected Behavior

The PostAsync() method should return a response.

Actual Behavior

The PostAsync() method doesn't return a response and doesn't return control to the app at all. The app is hanged.

At the same time, I see the exception in the Visual Studio output log:

=================================================================

	Native Crash Reporting

=================================================================
Got a segv while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
	Native stacktrace:

=================================================================
	0x102ac81e0 - /private/var/containers/Bundle/Application/2377422B-A9B1-478C-8B23-1F01D811DFBE/DebessmannInsightsTestAppiOS.app/Xamarin.PreBuilt.iOS : mono_dump_native_crash_info
	0x102abec5c - /private/var/containers/Bundle/Application/2377422B-A9B1-478C-8B23-1F01D811DFBE/DebessmannInsightsTestAppiOS.app/Xamarin.PreBuilt.iOS : mono_handle_native_crash

	0x102acbbb8 - /private/var/containers/Bundle/Application/2377422B-A9B1-478C-8B23-1F01D811DFBE/DebessmannInsightsTestAppiOS.app/Xamarin.PreBuilt.iOS : mono_sigsegv_signal_handler_debug

	0x1dbb97d48 - /usr/lib/system/libsystem_platform.dylib : <redacted>

	0x182ce3050 - /System/Library/Frameworks/Foundation.framework/Foundation : <redacted>

	0x102c6dc54 - /private/var/containers/Bundle/Application/2377422B-A9B1-478C-8B23-1F01D811DFBE/DebessmannInsightsTestAppiOS.app/Xamarin.PreBuilt.iOS : do_icall
	0x102c6c2e4 - /private/var/containers/Bundle/Application/2377422B-A9B1-478C-8B23-1F01D811DFBE/DebessmannInsightsTestAppiOS.app/Xamarin.PreBuilt.iOS : do_icall_wrapper
	0x102c631d8 - /private/var/containers/Bundle/Application/2377422B-A9B1-478C-8B23-1F01D811DFBE/DebessmannInsightsTestAppiOS.app/Xamarin.PreBuilt.iOS : interp_exec_method_full
	0x102c61768 - /private/var/containers/Bundle/Application/2377422B-A9B1-478C-8B23-1F01D811DFBE/DebessmannInsightsTestAppiOS.app/Xamarin.PreBuilt.iOS : interp_runtime_invoke
	0x102acee70 - /private/var/containers/Bundle/Application/2377422B-A9B1-478C-8B23-1F01D811DFBE/DebessmannInsightsTestAppiOS.app/Xamarin.PreBuilt.iOS : mono_jit_runtime_invoke
	0x102b7d188 - /private/var/containers/Bundle/Application/2377422B-A9B1-478C-8B23-1F01D811DFBE/DebessmannInsightsTestAppiOS.app/Xamarin.PreBuilt.iOS : mono_runtime_try_invok
e
	0x102bb7948 - /private/var/containers/Bundle/Application/2377422B-A9B1-478C-8B23-1F01D811DFBE/DebessmannInsightsTestAppiOS.app/Xamarin.PreBuilt.iOS : worker_callback
	0x102bb51bc - /private/var/containers/Bundle/Application/2377422B-A9B1-478C-8B23-1F01D811DFBE/DebessmannInsightsTestAppiOS.app/Xamarin.PreBuilt.iOS : worker_thread
	0x102bc0144 - /private/var/containers/Bundle/Application/2377422B-A9B1-478C-8B23-1F01D811DFBE/DebessmannInsightsTestAppiOS.app/Xamarin.PreBuilt.iOS : start_wrapper_internal
	0x102bbffc8 - /private/var/containers/Bundle/Application/2377422B-A9B1-478C-8B23-1F01D811DFBE/DebessmannInsightsTestAppiOS.app/Xamarin.PreBuilt.iOS : start_wrapper
	0x1dbba83a4 - /usr/lib/system/libsystem_pthread.dylib : _pthread_start
	0x1dbba69fc - /usr/lib/system/libsystem_pthread.dylib : thread_start

=================================================================
	Basic Fault Address Reporting
=================================================================
Memory around native instruction pointer (0x1
h
 
a8 
dbb96874):0x1dbb96864  28 24 40 a8 2a 2c 41 a8 21 80 00 91 42 00 05 cb  ($@.*,A.!...B...
0x1dbb96874  0c 34 00 a8 0e 3c 01 a8 42 00 01 f1 29 01 00 54  .4...<..B...)..T
0x1dbb96884  68 24 00 a8 6a 2c 01 a8 63 80 00 91 28 24 40 
$
.
..c...($@.
0x1dbb96894  2a 2c 41 a8 21 80 00 91 42 80 00 f1 28 ff ff 
.j,
54  *,A.!...B
...(..T

=================================================================
	Managed Stacktrace:
=================================================================
	  at <unknown> <0xffffffff>
	  at ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_UIntPtr <0x00022>
	  at Foundation.NSMutableData:AppendBytes <0x00054>
	  at Foundation.NSData:FromStream <0x0010c>
	  at <CreateRequest>d__61:MoveNext <0x003c2>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:Start <0x00052>
	  at System.Net.Http.NSUrlSessionHandler:CreateRequest <0x0004a>
	  at <SendAsync>d__62:MoveNext <0x00068>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:Start <0x00052>
	  at System.Net.Http.NSUrlSessionHandler:SendAsync <0x0005c>
	  at System.Net.Http.HttpMessageInvoker:SendAsync <0x0006a>
	  at System.Net.Http.HttpClient:SendAsync <0x00174>
	  at System.Net.Http.HttpClient:SendAsync <0x00016>
	  at System.Net.Http.HttpClient:PostAsync <0x00048>
	  at System.Net.Http.HttpClient:PostAsync <0x00024>
	  at System.Net.
dule>:invoke_void <0x00078>
	  at System.Threading.Tasks.AwaitTaskContinuation:RunOrScheduleAction <0x00064>
	  at System.Threading.Tasks.Task:FinishContinuations <0x00092>
	  at System.Threading.Tasks.Task:FinishStageThree <0x00074>
	  at System.Threading.Tasks.Task`1:TrySetResult <0x000a0>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult <0x0008c>
	  at <IsServerAvailableAsync>d__8:MoveNext <0x005c6>
	  at MoveNextRunner:InvokeMoveNext <0x00012>
	  at <Module>:invoke_void_object <0x0009e>
	  at System.Threading.ExecutionContext:RunInternal <0x00134>
	  at System.Threading.ExecutionContext:Run <0x00016>
	  at MoveNextRunner:Run <0x00058>
	  at <Module>:invoke_void <0x00078>
	  at System.Threading.Tasks.AwaitTaskContinuation:RunOrScheduleAction <0x00064>
	  at System.Threading.Tasks.Task:FinishContinuations <0x00092>
	  at System.Threading.Tasks.Task:FinishStageThree <0x00074>
	  at System.Threading.Tasks.Task`1:TrySetResult <0x000a0>
	  at System.Runtime.CompilerServices.AsyncTaskMeth
Http.HttpClient:PostAsync <0x0001a>
	  at <SendEventInternalAsync>d__11:MoveNext <0x0019e>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:Start <0x0004a>
	  at Debessmann.Insights.Core.EventSender.Debessmann.DebessmannEventSender:SendEventInternalAsync <0x0005a>
	  at <SendEventAsync>d__10:MoveNext <0x00070>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:Start <0x0004a>
	  at Debessmann.Insights.Core.EventSender.Debessmann.DebessmannEventSender:SendEventAsync <0x0005a>
	  at <ProcessEventQueueAsync>d__14:MoveNext <0x00268>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start <0x0004a>
	  at Debessmann.Insights.Core.EventManager:ProcessEventQueueAsync <0x0004e>
	  at <SendUndeliveredEventsAsync>d__13:MoveNext <0x001ec>
	  at MoveNextRunner:InvokeMoveNext <0x00012>
	  at <Module>:invoke_void_object <0x0009e>
	  at System.Threading.ExecutionContext:RunInternal <0x00134>
	  at System.Threading.ExecutionContext:Run <0x00016>
	  at MoveNextRunner:Run <0x00058>
	  at <Mo
odBuilder`1:SetResult <0x0008c>
	  at <FinishSendAsyncBuffered>d__62:MoveNext <0x00342>
	  at MoveNextRunner:InvokeMoveNext <0x00012>
	  at <Module>:invoke_void_object <0x0009e>
	  at System.Threading.ExecutionContext:RunInternal <0x00134>
	  at System.Threading.ExecutionContext:Run <0x00016>
	  at MoveNextRunner:Run <0x00058>
	  at <Module>:invoke_void <0x00078>
	  at System.Threading.Tasks.AwaitTaskContinuation:RunOrScheduleAction <0x00064>
	  at System.Threading.Tasks.Task:FinishContinuations <0x00092>
	  at System.Threading.Tasks.Task:FinishStageThree <0x00074>
	  at System.Threading.Tasks.Task`1:TrySetResult <0x000a6>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult <0x0009c>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult <0x0003a>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder:SetResult <0x00012>
	  at <LoadIntoBufferAsyncCore>d__52:MoveNext <0x001e8>
	  at MoveNextRunner:InvokeMoveNext <0x00012>
	  at <Module>:invoke_void_object <0x0009e>

	  at System.Threading.ExecutionContext:RunInternal <0x00134>
	  at System.Threading.ExecutionContext:Run <0x00016>
	  at MoveNextRunner:Run <0x00058>
	  at <Module>:invoke_void <0x00078>
	  at System.Threading.Tasks.AwaitTaskContinuation:RunOrScheduleAction <0x00064>
	  at System.Threading.Tasks.Task:FinishContinuations <0x00092>
	  at System.Threading.Tasks.Task:FinishStageThree <0x00074>
	  at System.Threading.Tasks.Task`1:TrySetResult <0x000a6>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult <0x0009c>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult <0x0003a>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder:SetResult <0x00012>
	  at <CopyToAsyncInternal>d__28:MoveNext <0x002fa>
	  at MoveNextRunner:InvokeMoveNext <0x00012>
	  at <Module>:invoke_void_object <0x0009e>
	  at System.Threading.ExecutionContext:RunInternal <0x00134>
	  at System.Threading.ExecutionContext:Run <0x00016>
	  at MoveNextRunner:Run <0x00058>
	  at <Module>:invoke_void <0x0
0078>
	  at System.Threading.Tasks.AwaitTaskContinuation:RunOrScheduleAction <0x00064>
	  at System.Threading.Tasks.Task:FinishContinuations <0x00092>
	  at System.Threading.Tasks.Task:FinishStageThree <0x00074>
	  at System.Threading.Tasks.Task`1:TrySetResult <0x000a0>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult <0x0008c>
	  at <ReadAsync>d__14:MoveNext <0x00502>
	  at MoveNextRunner:InvokeMoveNext <0x00012>
	  at <Module>:invoke_void_object <0x0009e>
	  at System.Threading.ExecutionContext:RunInternal <0x00134>
	  at System.Threading.ExecutionContext:Run <0x00016>
	  at MoveNextRunner:Run <0x00058>
	  at <Module>:invoke_void <0x00078>
	  at System.Threading.Tasks.AwaitTaskContinuation:RunOrScheduleAction <0x00064>
	  at System.Threading.Tasks.Task:FinishContinuations <0x00092>
	  at System.Threading.Tasks.Task:FinishStageThree <0x00074>
	  at System.Threading.Tasks.Task`1:TrySetResult <0x000a6>
	  at DelayPromise:Complete <0x0006e>
	  at <>c:<Delay>b__247_1 <0x00012>
	  at <Modu
le>:invoke_void_object <0x0007c>
	  at Scheduler:TimerCB <0x0002a>
	  at <Module>:invoke_void_object <0x0009e>
	  at System.Threading.QueueUserWorkItemCallback:System.Threading.IThreadPoolWorkItem.ExecuteWorkItem <0x00032>
	  at System.Threading.ThreadPoolWorkQueue:Dispatch <0x0014c>
	  at System.Func`1:invoke_TResult <0x00096>
	  at ObjCRuntime.Runtime:ThreadPoolDispatcher <0x0001e>
	  at System.Func`2:invoke_TResult_T <0x0009e>
	  at System.Threading._ThreadPoolWaitCallback:PerformWaitCallback <0x00038>
	  at <Module>:runtime_invoke_direct_bool <0x00036>
=================================================================

Environment

Windows Version 21H2 (OS Build 19044.1415)

Xamarin.Forms 5.0.0.2083

iPhone 7 Plus device with iOS version 15.2.1

Microsoft Visual Studio Enterprise 2022
Version 17.0.5
VisualStudio.17.Release/17.0.5+32112.339
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Enterprise

Visual C++ 2022   00476-80000-00000-AA288
Microsoft Visual C++ 2022

.NET Core Debugging with WSL   1.0
.NET Core Debugging with WSL

ASP.NET and Web Tools 2019   17.0.795.42246
ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019   17.0.795.42246
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0   17.0.795.42246
Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools   17.0.795.42246
Azure Functions and Web Jobs Tools

C# Tools   4.0.1-1.21568.1+6ab6601178d9fba8c680b56934cd1742e0816bff
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus   1.2.6 (master@34d6af2)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

Fabric.DiagnosticEvents   1.0
Fabric Diagnostic Events

Microsoft Azure Service Fabric Tools for Visual Studio   17.0
Microsoft Azure Service Fabric Tools for Visual Studio

Microsoft Azure Tools for Visual Studio   2.9
Support for Azure Cloud Services projects

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager   2.1.134+45632ee938.RR
Install client-side libraries easily to any web project

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards   1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers   1.2
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   17.0.11 (54f19d2)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager   6.0.1
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

Razor (ASP.NET Core)   17.0.0.2156603+e9cd763349a080127b6519ffbec0123949a4c385
Provides languages services for ASP.NET Core Razor.

Snapshot Debugging Extension   1.0
Snapshot Debugging Visual Studio Extension Detailed Info

SQL Server Data Tools   17.0.62110.20190
Microsoft SQL Server Data Tools

TypeScript Tools   17.0.1001.2002
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   4.0.1-1.21568.1+6ab6601178d9fba8c680b56934cd1742e0816bff
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools   17.0.0-beta.21522.2+6d626ff0752a77d339f609b4d361787dc9ca93a5
Microsoft Visual F# Tools

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions   1.0
View, manage, and diagnose containers within Visual Studio.

Visual Studio IntelliCode   2.2
AI-assisted development for Visual Studio.

Visual Studio Tools for Containers   1.0
Visual Studio Tools for Containers

VisualStudio.DeviceLog   1.0
Information about my package

VisualStudio.Foo   1.0
Information about my package

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

Xamarin   17.0.0.343 (d17-0@fb07a17)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   17.0.0.183 (remotes/origin/d17-0@a351f0f1f)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   17.0.17 (9e779b0)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   12.1.0.5 (d17-0/6b0e6b2)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: c633fe9
    Java.Interop: xamarin/java.interop/d17-0@febb1367
    ProGuard: Guardsquare/proguard/v7.0.1@912d149
    SQLite: xamarin/sqlite/3.36.0@a575761
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-0@a5194e9


Xamarin.iOS and Xamarin.Mac SDK   15.4.0.0 (8fc41ae82)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

.

@rolfbjarne
Copy link
Member

This looks like a variation of this bug: dotnet/runtime#61153, which was recently fixed and will be included in the next service release.

It should work if you disable hot restart in your project.

@avorobjovs
Copy link
Author

avorobjovs commented Jan 27, 2022

As I found on the Internet, this is an old problem. It has already appeared earlier from time to time.
For some people it works, but for some people doesn't. On some devices it works, but on some devices it doesn't.

Maybe it depends on iOS device, maybe it depends on iOS version, maybe on Mono version, maybe on Xamarin.iOS version, maybe on Xamarin.Forms version, I don't know.

@avorobjovs
Copy link
Author

I also found a workaround.
https://stackoverflow.com/questions/70064427/xamarin-forms-httpclient-postasync-always-throw-nsmallocexception

All you have to do is use the HttpClientHandler.

var handler = new HttpClientHandler();
var client = new HttpClient(handler);
var response = await client.PostAsync(<some_url>, <some-content>);

And it helps! All works fine, as expected!

But the question is: why it doesn't work without the HttpClientHandler?!

@rolfbjarne
Copy link
Member

why it doesn't work without the HttpClientHandler?!

The default handler is a very different implementation, and likely doesn't hit the same problem.

In any case, I'm glad you were able to find a workaround until the fix is released, so I'll close this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants