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

await within method with returntype Task<T> makes app crash #4380

Closed
hansmbakker opened this Issue Jul 3, 2018 · 24 comments

Comments

Projects
None yet
@hansmbakker
Copy link

hansmbakker commented Jul 3, 2018

The symptom and workaround are similar to xamarin/xamarin-android#1472 but I'm not sure if the cause is the same.

Steps to Reproduce

Code in netstandard 2.0 project referenced from Xamarin.iOS project:

private async Task<T> GetFromBackendAsync<T>(string action) where T : class
        {
            HttpResponseMessage response = null;

            try
            {
                var uri = new Uri(_profileControllerUri, action);

                response = await _client.GetAsync(uri);
            }
            catch (Exception)
            {
                throw new CustomException("Connection issue", ServiceExceptionKind.ConnectionError);
            }

            if (response != null && response.IsSuccessStatusCode)
            {
                var data = await response.Content.ReadAsAsync<T>();
                return data;
            }

            if (response != null && response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                throw new CustomException("Not logged in", ServiceExceptionKind.NotAuthenticated);
            }
            else
            {
                throw new CustomException("Unknown Error", ServiceExceptionKind.UnknownError);
            }
        }
  1. Implement code above in netstandard2.0 project referenced from Xamarin.iOS project
  2. Step through it with debugger in VS for Mac

Expected Behavior

HttpClient correctly fetches data. Debugger doesn't crash.

Actual Behavior

Debugger crashes on the line await _client.GetAsync(uri);

Debug output:

2018-07-03 10:56:30.949 MyAppName.iOS[3722:1889946] error: * Assertion at ../../../../../mono/mini/debugger-agent.c:4901, condition `is_ok (error)' not met, function:get_this_async_id, Could not execute the method because the containing type is not fully instantiated. assembly:<unknown assembly> type:<unknown type> member:(null) signature:<none>
2018-07-03 10:56:30.949 MyAppName.iOS[3722:1889946] critical: Stacktrace:

2018-07-03 10:56:30.949 MyAppName.iOS[3722:1889946] critical:   at <unknown> <0xffffffff>
2018-07-03 10:56:30.950 MyAppName.iOS[3722:1889946] critical:   at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) [0x0005c] in <8099d51c44f0414697155302a4fa0973>:0
2018-07-03 10:56:30.950 MyAppName.iOS[3722:1889946] critical:   at UIKit.UIApplication.Main (string[],intptr,intptr) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.14.0.4/src/Xamarin.iOS/UIKit/UIApplication.cs:79
2018-07-03 10:56:30.950 MyAppName.iOS[3722:1889946] critical:   at UIKit.UIApplication.Main (string[],string,string) [0x0002c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.14.0.4/src/Xamarin.iOS/UIKit/UIApplication.cs:63
2018-07-03 10:56:30.950 MyAppName.iOS[3722:1889946] critical:   at MyAppName.iOS.Application.Main (string[]) [0x00001] in /Users/myusername/projects/projectname/MyAppName.iOS/Main.cs:17
2018-07-03 10:56:30.950 MyAppName.iOS[3722:1889946] critical:   at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) [0x00051] in <e37596883bd34159a118ef33d83fc1c7>:0
2018-07-03 10:56:30.950 MyAppName.iOS[3722:1889946] critical: 
Native stacktrace:

2018-07-03 10:56:30.972 MyAppName.iOS[3722:1889946] critical: 	0   MyAppName.iOS                      0x000000010632d2d4 mono_handle_native_crash + 244
2018-07-03 10:56:30.973 MyAppName.iOS[3722:1889946] critical: 	1   libsystem_platform.dylib            0x000000011743bf5a _sigtramp + 26
2018-07-03 10:56:30.973 MyAppName.iOS[3722:1889946] critical: 	2   ???                                 0x000000000000ffff 0x0 + 65535
2018-07-03 10:56:30.973 MyAppName.iOS[3722:1889946] critical: 	3   libsystem_c.dylib                   0x0000000117071c97 abort + 127
2018-07-03 10:56:30.973 MyAppName.iOS[3722:1889946] critical: 	4   MyAppName.iOS                      0x0000000106548040 _ZL12log_callbackPKcS0_S0_iPv + 64
2018-07-03 10:56:30.973 MyAppName.iOS[3722:1889946] critical: 	5   MyAppName.iOS                      0x0000000106516503 monoeg_g_logv + 83
2018-07-03 10:56:30.973 MyAppName.iOS[3722:1889946] critical: 	6   MyAppName.iOS                      0x00000001065167f7 monoeg_assertion_message + 199
2018-07-03 10:56:30.973 MyAppName.iOS[3722:1889946] critical: 	7   MyAppName.iOS                      0x000000010628adfe get_this_async_id + 478
2018-07-03 10:56:30.973 MyAppName.iOS[3722:1889946] critical: 	8   MyAppName.iOS                      0x0000000106289ec3 ss_start + 723
2018-07-03 10:56:30.974 MyAppName.iOS[3722:1889946] critical: 	9   MyAppName.iOS                      0x000000010627d6fa process_breakpoint + 1530
2018-07-03 10:56:30.974 MyAppName.iOS[3722:1889946] critical: 	10  MyAppName.iOS                      0x000000010627d08e debugger_agent_breakpoint_from_context + 158
2018-07-03 10:56:30.974 MyAppName.iOS[3722:1889946] critical: 	11  ???                                 0x000000013168107f 0x0 + 5123870847
2018-07-03 10:56:30.974 MyAppName.iOS[3722:1889946] critical: 	12  ???                                 0x0000000137f141c6 0x0 + 5233525190
2018-07-03 10:56:30.974 MyAppName.iOS[3722:1889946] critical: 	13  ???                                 0x0000000137f13d5f 0x0 + 5233524063
2018-07-03 10:56:30.974 MyAppName.iOS[3722:1889946] critical: 	14  ???                                 0x0000000137f12b63 0x0 + 5233519459
2018-07-03 10:56:30.974 MyAppName.iOS[3722:1889946] critical: 	15  ???                                 0x0000000137f11b33 0x0 + 5233515315
2018-07-03 10:56:30.974 MyAppName.iOS[3722:1889946] critical: 	16  ???                                 0x0000000137f0e073 0x0 + 5233500275
2018-07-03 10:56:30.975 MyAppName.iOS[3722:1889946] critical: 	17  ???                                 0x0000000137f078eb 0x0 + 5233473771
2018-07-03 10:56:30.975 MyAppName.iOS[3722:1889946] critical: 	18  MyAppName.iOS                      0x0000000106340023 mono_jit_runtime_invoke + 1443
2018-07-03 10:56:30.975 MyAppName.iOS[3722:1889946] critical: 	19  MyAppName.iOS                      0x000000010642fa9f mono_runtime_invoke_checked + 127
2018-07-03 10:56:30.975 MyAppName.iOS[3722:1889946] critical: 	20  MyAppName.iOS                      0x0000000106433513 mono_runtime_invoke + 83
2018-07-03 10:56:30.975 MyAppName.iOS[3722:1889946] critical: 	21  MyAppName.iOS                      0x000000010617481c _ZL31native_to_managed_trampoline_20P11objc_objectP13objc_selectorPP11_MonoMethodj + 300
2018-07-03 10:56:30.975 MyAppName.iOS[3722:1889946] critical: 	22  MyAppName.iOS                      0x00000001061fc024 -[DDEP_Shared_iOS_Controller_TouchIdPinEntryDelegate validatePin] + 52
2018-07-03 10:56:30.975 MyAppName.iOS[3722:1889946] critical: 	23  Foundation                          0x0000000113fea9eb __NSFireDelayedPerform + 418
2018-07-03 10:56:30.976 MyAppName.iOS[3722:1889946] critical: 	24  CoreFoundation                      0x0000000114f9ae64 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
2018-07-03 10:56:30.976 MyAppName.iOS[3722:1889946] critical: 	25  CoreFoundation                      0x0000000114f9aa52 __CFRunLoopDoTimer + 1026
2018-07-03 10:56:30.976 MyAppName.iOS[3722:1889946] critical: 	26  CoreFoundation                      0x0000000114f9a60a __CFRunLoopDoTimers + 266
2018-07-03 10:56:30.976 MyAppName.iOS[3722:1889946] critical: 	27  CoreFoundation                      0x0000000114f91e4c __CFRunLoopRun + 2252
2018-07-03 10:56:30.976 MyAppName.iOS[3722:1889946] critical: 	28  CoreFoundation                      0x0000000114f9130b CFRunLoopRunSpecific + 635
2018-07-03 10:56:30.976 MyAppName.iOS[3722:1889946] critical: 	29  GraphicsServices                    0x0000000118c25a73 GSEventRunModal + 62
2018-07-03 10:56:30.977 MyAppName.iOS[3722:1889946] critical: 	30  UIKit                               0x0000000111dda057 UIApplicationMain + 159
2018-07-03 10:56:30.977 MyAppName.iOS[3722:1889946] critical: 	31  ???                                 0x0000000134f2e31e 0x0 + 5183300382
2018-07-03 10:56:30.977 MyAppName.iOS[3722:1889946] critical: 	32  ???                                 0x0000000134f2e0b3 0x0 + 5183299763
2018-07-03 10:56:30.977 MyAppName.iOS[3722:1889946] critical: 
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

Environment

=== Visual Studio Community 2017 for Mac (Preview) ===

Version 7.6 Preview (7.6 build 1773)
Installation UUID: 0db94ca7-2477-4634-9017-40752d39bdaf
Runtime:
	Mono 5.12.0.273 (2018-02/f59eac4c0f1) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)
	Xamarin.Mac 4.4.1.178 (master / eeaeb7e6)

	Package version: 512000273

=== NuGet ===

Version: 4.3.1.4445

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Version: 2.1.0
SDK: /usr/local/share/dotnet/sdk/2.1.300/Sdks
SDK Version: 2.1.300
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.12.0/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

Version: 1.6.3
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin.Android ===

Not Installed

=== Xamarin Inspector ===

Version: 1.4.0
Hash: b3f92f9
Branch: master
Build date: Fri, 19 Jan 2018 22:00:34 GMT
Client compatibility: 1

=== Apple Developer Tools ===

Xcode 9.4.1 (14161)
Build 9F2000

=== Xamarin.Mac ===

Xamarin.Mac not installed. Can't find /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/Version.

=== Xamarin.iOS ===

Version: 11.14.0.4 (Visual Studio Community)
Hash: 29d671b5
Branch: d15-8
Build date: 2018-06-07 11:51:32-0400

=== Build Information ===

Release ID: 706001773
Git revision: e5958ff9015312c81fc3b1b6b6413585575ca869
Build date: 2018-06-19 12:19:27+00
Build branch: release-7.6
Xamarin extensions: 6910f36b43e7d559ff40ae268e73ae26c30a2a6b

=== Operating System ===

Mac OS X 10.13.5
Darwin 17.6.0 Darwin Kernel Version 17.6.0
    Tue May  8 15:22:16 PDT 2018
    root:xnu-4570.61.1~1/RELEASE_X86_64 x86_64

=== Enabled user installed extensions ===

Internet of Things (IoT) development (Preview) 7.5

Workaround

Using response = _client.GetAsync(uri).GetAwaiter().GetResult(); instead of await _client.GetAsync(uri); works. The symptom and workaround are similar to xamarin/xamarin-android#1472 but I'm not sure if the cause is the same.

@hansmbakker

This comment has been minimized.

Copy link
Author

hansmbakker commented Jul 4, 2018

I get the crash during debugging when stepping into the GetAsync call; if i don’t debut then the app just hangs and the http request doesn’t seem to reach the server.

@dalexsoto

This comment has been minimized.

Copy link
Member

dalexsoto commented Jul 5, 2018

Hello I've tried to reproduce the issue on my end but I have not been able to, here is the snippet I used. Could you please provide us with a full reproducible project test case?

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace AsyncSampler {
	public class Downloader {

		static HttpClient _client = new HttpClient ();

		public async Task<string> GetFromBackendAsync ()
		{
			HttpResponseMessage response = null;

			try {
				var uri = new Uri ("https://jsonplaceholder.typicode.com/posts/1");

				response = await _client.GetAsync (uri);
			} catch (Exception ex) {
				throw new Exception ($"Connection issue ServiceExceptionKind.ConnectionError {ex.Message}");
			}

			if (response != null && response.IsSuccessStatusCode) {
				var data = await response.Content.ReadAsStringAsync ();
				return data;
			}

			if (response != null && response.StatusCode == System.Net.HttpStatusCode.Unauthorized) {
				throw new Exception ("Not logged in ServiceExceptionKind.NotAuthenticated");
			} else {
				throw new Exception ("Unknown Error ServiceExceptionKind.UnknownError");
			}
		}
	}
}

@dalexsoto dalexsoto added this to the Future milestone Jul 5, 2018

@softlion

This comment has been minimized.

Copy link

softlion commented Jul 31, 2018

Nearly same issue here. While debugging and stepping out from an async method, called by another async method, both in a netstandard 2 project, called from ios, debugged from ios 8 simulator (not remoted) from Visual Studio 15.7.5. Xcode 10 is installed on mac along with xcode 9 but not selected as default.
Running app without debugging works fine though.

error: /Users/builder/data/lanes/5944/64fece5f/source/xamarin-macios/external/mono/mono/mini/debugger-agent.c:4923: Could not execute the method because the containing type is not fully instantiated. assembly:<unknown assembly> type:<unknown type> member:(null) signature:<none>

Note that the async method is generic:

Calling method:

        public Task<ApiResult<VfrResponse<List<NotificationBindings>>>> GetNotifications(CancellationToken cancel)
        {
            return rest.GetWrap<VfrResponse<List<NotificationBindings>>>(...);
        }
        public static Task<ApiResult<T>> GetWrap<T>(this IRest rest, ...)
        {
            return rest.RequestWrap<T>(...);
        }

Called method, whose return crashes the debugger:

        public static async Task<ApiResult<T>> RequestWrap<T>(this IRest rest, ...)
        {
            try
            {
                var result = await rest.Request(...).ConfigureAwait(false);
                ...
                return oResult;
            }
            catch (Exception e)
            { ...
            }
        }
@danielmeza

This comment has been minimized.

Copy link

danielmeza commented Aug 6, 2018

I have this issue to on MonoAndroid.

@IgorKravchenko10

This comment has been minimized.

Copy link

IgorKravchenko10 commented Aug 8, 2018

It's not only ios. I have same problem at Android.
Works:
var response = httpClient.SendAsync(request).GetAwaiter().GetResult();
Does not work:
var response = await httpClient.SendAsync(request);

@softlion

This comment has been minimized.

Copy link

softlion commented Aug 8, 2018

await httpClient.SendAsync(request); does work.

@IgorKravchenko10

This comment has been minimized.

Copy link

IgorKravchenko10 commented Aug 15, 2018

OK. I found it doesn't work if making request from App.OnStart(). In other cases it works as well.

@ChrisAllisonMalta

This comment has been minimized.

Copy link

ChrisAllisonMalta commented Sep 1, 2018

Yeah I have this issue with both Android and iOS, did anyone resolve it?

@rolfbjarne

This comment has been minimized.

Copy link
Member

rolfbjarne commented Sep 7, 2018

We need a complete test project we can use to reproduce this issue in order to track this down.

These types of debugger crashes tend to only happen in very specific scenarios, which means we if were to try to recreate the exact conditions from the descriptions only we could be trying forever.

@rolfbjarne rolfbjarne added the bug label Sep 7, 2018

@ChrisAllisonMalta

This comment has been minimized.

Copy link

ChrisAllisonMalta commented Sep 7, 2018

I'll try and create a simple one over the weekend, sods law means it will work on the new one!

@VincentDondain

This comment has been minimized.

Copy link
Contributor

VincentDondain commented Sep 26, 2018

Hi @ChrisAllisonMalta, did you manage to create a test project so we can resume work on this issue by any chance?

Thanks.

@eihctir

This comment has been minimized.

Copy link

eihctir commented Sep 27, 2018

I am experiencing the same issue but the workaround does not seem to work in my case. Also this stopped working properly when we updated to iOS 12.

@ChrisAllisonMalta

This comment has been minimized.

Copy link

ChrisAllisonMalta commented Oct 1, 2018

Hi @VincentDondain
Sorry I still struggled to get it working even with the new project however when I used the new project it gave me a clue about unsecured connections which eventually led me to white list the app url as discussed here and it works.

I think the problem I had was that I was developing the dotnet core webapi locally at the same time which works fine in Android but clearly not with the ATS security requirements.

I do think that there could be more documentation about co-developing Xamarin Forms and a dot net core web api as the back end instead of the standard Azure tables.

Thanks

@rolfbjarne

This comment has been minimized.

Copy link
Member

rolfbjarne commented Nov 5, 2018

I'm closing this issue, since according to the last comment you've managed to make it work.

I do think that there could be more documentation about co-developing Xamarin Forms and a dot net core web api as the back end instead of the standard Azure tables.

You can file your documentation suggestions/requests here: https://github.com/MicrosoftDocs/xamarin-docs/issues, that way the documentation team will see it.

@rolfbjarne rolfbjarne closed this Nov 5, 2018

@ChrisAllisonMalta

This comment has been minimized.

Copy link

ChrisAllisonMalta commented Nov 19, 2018

Hi @rolfbjarne I've come up against this issue again, this time I'm now hitting an API thats on Azure with SSL and ATS v1.2 for sure (Azure tells me so!)

The project is pretty basic so I can post on gitHub to be downloaded but I'd rather not share the links etc with the universe, is there a way I can share it directly with someone on the team?

@rolfbjarne

This comment has been minimized.

Copy link
Member

rolfbjarne commented Nov 20, 2018

@ChrisAllisonMalta my recommendation would be to:

  • Upload a zip of the project on Dropbox or a similar service (this way you can delete the project when you wish)
  • Email me (Rolf.Kvinge@microsoft.com) the Dropbox link. I'll store the link privately, so that other team members can download the project as well.
  • Create a new issue (continuing here will just be confusing) describing the problem, and mentioning who you sent the project to.
@dbwelch

This comment has been minimized.

Copy link

dbwelch commented Dec 17, 2018

Same issue, ios 12.1, on OSx Visual Studio 7.7.1. Crashes on:
var response = await client.PostAsync(uri, flds);

@ChrisAllisonMalta

This comment has been minimized.

Copy link

ChrisAllisonMalta commented Dec 17, 2018

Same issue, ios 12.1, on OSx Visual Studio 7.7.1. Crashes on:
var response = await client.PostAsync(uri, flds);

@dbwelch I found, in my case it was to do with how exceptions are handled in async code, basically they go somewhere else! Try having try/catch braces as deep as you can go, in my case it was not able to cast the response into the object it expected

@dbwelch

This comment has been minimized.

Copy link

dbwelch commented Dec 17, 2018

@ChrisAllisonMalta Thanks for responding, but, the call isn't even getting to my server, strange thing is this was working fine for weeks, then, my Xamarin and iPhone simulator got hung, requiring me to kill the process. After restart, this occurs. I've even put try/catch around just the PostAsync, but, crash is immediate. Same thing occurs in Debug or Release on iOS 12.1 simulator (tried multiple devices).

@eihctir

This comment has been minimized.

Copy link

eihctir commented Dec 17, 2018

This issue appears to fixed for me with the latest version of Xamarin.iOS.

@WebsoulTwo

This comment has been minimized.

Copy link

WebsoulTwo commented Dec 18, 2018

I get this same bug in Android when i change my code from
client.PostAsJsonAsync(url, request).GetAwaiter().GetResult();
to
await client.PostAsJsonAsync(url, request);

I have a try catch around the call but it doesnt catch it and the debugger crashes with the following message:

* Assertion at /Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/mono/mini/debugger-agent.c:4931, condition `is_ok (error)' not met, function:get_this_async_id, Could not execute the method because the containing type is not fully instantiated. assembly:<unknown assembly> type:<unknown type> member:(null)

Fatal signal 6 (SIGABRT), code -6 in tid 12850
@OmegaPrimeZ3

This comment has been minimized.

Copy link

OmegaPrimeZ3 commented Feb 2, 2019

I am seeing this as well on certain Xamarin Forms Android projects but not all, the interesting thing is that they are using the same code for this piece of logic

@mindsurfer

This comment has been minimized.

Copy link

mindsurfer commented Feb 14, 2019

I have encountered this exact same error as posted by WebSoulTwo above but it only happens when I try step through the code. If I put my breakpoint after the call, then it works and I get my result successfully. I'm using latest Xamarin.Forms and this happens on my Android device running version 9, I don't have an iOS to test on.

@jcpc91

This comment has been minimized.

Copy link

jcpc91 commented Mar 15, 2019

same issue with the post async Methods

  • Assertion at /Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/mono/mini/debugger-agent.c:4957, condition `is_ok (error)' not met, function:set_set_notification_for_wait_completion_flag, Could not execute the method because the containing type is not fully instantiated. assembly: type: member:(null)

Fatal signal 6 (SIGABRT), code -6 in tid 5980 (ysne.IoThermApp), pid 5980 (ysne.IoThermApp)


Build fingerprint: 'google/sdk_gphone_x86/generic_x86:8.1.0/OSM1.180201.007/4586646:user/release-keys'
Revision: '0'
ABI: 'x86'
pid: 5980, tid: 5980, name: ysne.IoThermApp >>> com.companyname.Sysne.IoThermApp <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Abort message: '* Assertion at /Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/mono/mini/debugger-agent.c:4957, condition `is_ok (error)' not met, function:set_set_notification_for_wait_completion_flag, Could not execute the method because the containing type is not fully instantiated. assembly: type: member:(null)
'
eax 00000000 ebx 0000175c ecx 0000175c edx 00000006
esi 00000000 edi 0000175c
xcs 00000073 xds 0000007b xes 0000007b xfs 0000003b xss 0000007b
eip a93beac4 ebp 00000000 esp bf8c6778 flags 00000286

backtrace:
#00 pc 00000ac4 [vdso:a93be000] (__kernel_vsyscall+16)
#1 pc 0001edf8 /system/lib/libc.so (syscall+40)
#2 pc 0001f073 /system/lib/libc.so (abort+115)
#3 pc 00296785 /data/app/Mono.Android.DebugRuntime--6txJ9xzLmcT66h0ZvvsCw==/lib/x86/libmonosgen-32bit-2.0.so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.