Bug 300 - System.ExecutionEngineException - Attempting to JIT compile method 'System.Linq.OrderedEnumerable`1<Jrp.Snap.CardModelInfo>:GetEnumerator ()' while running with --aot-only.
Summary: System.ExecutionEngineException - Attempting to JIT compile method 'System.Li...
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 4.x
Hardware: Macintosh Mac OS
: --- major
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
URL:
Depends on:
Blocks:
 
Reported: 2011-08-15 19:27 UTC by gtissington
Modified: 2012-06-06 20:47 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Build Output from release version (91.19 KB, text/plain)
2011-08-15 19:27 UTC, gtissington
Details
Sample project to duplicate the problem (64.34 KB, application/octet-stream)
2011-08-16 14:10 UTC, gtissington
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_ON_ROADMAP

Description gtissington 2011-08-15 19:27:45 UTC
Created attachment 134 [details]
Build Output from release version

- This error did not occur before I upgraded to the latest MonoTouch.
- The code works fine in simulator and iPhone debug.
- When I use iPhone Release it produces this error at runtime (trapped and written in my log file.)

System.ExecutionEngineException - Attempting to JIT compile method 'System.Linq.OrderedEnumerable`1<Jrp.Snap.CardModelInfo>:GetEnumerator ()' while running with --aot-only.


Please help.  this is the only think delaying me from publish an update to the app store.


The build output is in the attachment in case it helps.
Comment 1 gtissington 2011-08-15 19:30:08 UTC
Oh, and, I tried adding this before the error call


List<Jrp.Snap.CardModelInfo> c = CardManager.Instance.CardModelInfos.ToList();
var a =  c.FirstOrDefault();


To force AOT to include the required method.  No change.  Also, First or default is used in many many other areas of the app.  It is only here that fails.
Comment 2 Sebastien Pouliot 2011-08-15 21:47:34 UTC
Sadly the build output won't help since everything things has been compiled.

I'm surprised that "iPhone Debug" works !?! since the JIT is not available on the device (only the simulator). If that's really the case (not a typo or mistake) then you likely have some different code paths in DEBUG that "solves" your issue (i.e. it gives the AOT compiler the hints it needs to compile the code correctly).

Please check this (iPhone Debug) behavior and if it does not work then please attach a self-contained test case and we'll help you to get past this.

p.s. First and FirstOrDefault have been fixed not to require the generic type parameter (so they'll always work in future MonoTouch).
Comment 3 gtissington 2011-08-16 14:06:35 UTC
I have built a smnall demo project.  It is attached.

When you run the program it will produce a list of strings that show what parts of the app have run.

On my system I get the following results.  The problem is in the final result for release on the iPhone.

Debug->Simulator
====================
a
b
Count:0
d
e

Release->Simulator
====================
a
b
Count:0
d
e

Debug->iPhone
====================
a
b
Count:0
d
e

Release->iPhone
====================
a
b
  Attempting to JIT compile method
'System.Linq.OrderedEnumerable'1<Jrp.Snap.CardModelInfo>:GetEnumerator()' while running with --aot-only.
Comment 4 gtissington 2011-08-16 14:10:00 UTC
Created attachment 143 [details]
Sample project to duplicate the problem

See my comment
Comment 5 gtissington 2011-08-16 14:11:11 UTC
Change status off NEED INFO since I supplied the requested info.
Comment 6 Sebastien Pouliot 2011-08-16 15:35:41 UTC
It works on the simulator but on Debug|iPhone I get


Aug 16 15:39:11 unknown UIKitApplication:test[0xcfc0][5761] <Notice>: [0x3e4a148c:] EXCEPTION handling: System.ExecutionEngineException: Attempting to JIT compile method 'System.Linq.Check:SourceAndSelector (object,object)' while running with --aot-only.
Aug 16 15:39:11 unknown UIKitApplication:test[0xcfc0][5761] <Notice>: "<unnamed thread>" tid=0x0x3e4a148c this=0x0x20ae748 thread handle 0x103 state : not waiting owns ()
Aug 16 15:39:11 unknown UIKitApplication:test[0xcfc0][5761] <Notice>:   at System.Linq.Enumerable.Select<System.IO.FileInfo, Jrp.Snap.CardModelInfo> (System.Collections.Generic.IEnumerable`1<System.IO.FileInfo>,System.Func`2<System.IO.FileInfo, Jrp.Snap.CardModelInfo>) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq/Enumerable.cs:2235
Aug 16 15:39:11 unknown UIKitApplication:test[0xcfc0][5761] <Notice>:   at Jrp.Snap.CardManager.RebuildCardDictionaries () [0x00016] in /Users/sebastienpouliot/Downloads/Archive-1/Jrp.Snap.Card/CardManager_Caches.cs:58
Aug 16 15:39:11 unknown UIKitApplication:test[0xcfc0][5761] <Notice>:   at Jrp.Snap.CardManager..ctor () [0x0001f] in /Users/sebastienpouliot/Downloads/Archive-1/Jrp.Snap.Card/CardManager.cs:46
Aug 16 15:39:11 unknown UIKitApplication:test[0xcfc0][5761] <Notice>:   at Jrp.Snap.CardManager.get_Instance () [0x0000a] in /Users/sebastienpouliot/Downloads/Archive-1/Jrp.Snap.Card/CardManager.cs:38
Aug 16 15:39:11 unknown UIKitApplication:test[0xcfc0][5761] <Notice>:   at Test.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication,MonoTouch.Foundation.NSDictionary) [0x0003c] in /Users/sebastienpouliot/Downloads/Archive-1/Main.cs:35
Aug 16 15:39:11 unknown UIKitApplication:test[0xcfc0][5761] <Notice>:   at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0xffffffff>
Aug 16 15:39:11 unknown UIKitApplication:test[0xcfc0][5761] <Notice>:   at MonoTouch.UIKit.UIApplication.Main (string[],string,string) <0x0010f>
Aug 16 15:39:11 unknown UIKitApplication:test[0xcfc0][5761] <Notice>:   at MonoTouch.UIKit.UIApplication.Main (string[]) <0x00023>
Aug 16 15:39:11 unknown UIKitApplication:test[0xcfc0][5761] <Notice>:   at Test.Application.Main (string[]) [0x00000] in /Users/sebastienpouliot/Downloads/Archive-1/Main.cs:17
Aug 16 15:39:11 unknown UIKitApplication:test[0xcfc0][5761] <Notice>:   at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0xffffffff>


The fact that the error points to a different method may be because my source tree is different (more recent) than your MonoTouch. OTOH I still don't see how you could get your Debug build to work on the device ?!?
Comment 7 Sebastien Pouliot 2011-08-16 15:37:32 UTC
note: you should be able to get me a stack trace similar to the above by using

/Developer/MonoTouch/usr/bin/mtouch --logdev

after you executed the application on your device. This will give me a few hints as to where things differs.

If possible both "Debug" and "Release" logs would be helpful. Thanks!
Comment 8 gtissington 2011-08-17 11:27:31 UTC
I've never used logdev before.  Could you provide more detailed instructions.  I ran the command as suggested and then ran the app on the device but I see no log?
Comment 9 gtissington 2011-08-17 11:40:13 UTC
I've never used logdev before.  Could you provide more detailed instructions. 
I ran the command as suggested and then ran the app on the device but I see no
log?

Changed status to NEW
Comment 10 Sebastien Pouliot 2011-08-17 11:42:22 UTC
a) open a terminal window (on your Mac)

b) run (and crash) your application on your iOS device. That will create the logs in your device.

c) run "/Developer/MonoTouch/usr/bin/mtouch --logdev" from the terminal window. That will load and print the logs from your device into the terminal

d) paste (or attach) the part of the log (near the end) that is related to your application

p.s. please do not change status from NEEDINFO unless the requested information is supplied. thanks!
Comment 11 gtissington 2011-08-17 11:52:25 UTC
I ran that app as you instructed but I get no log output.

when I run --logdev  the terminal pauses for about 1 second. and then returns to the command prompt. with no output.

What am I doing wrong?
Comment 12 Sebastien Pouliot 2011-08-17 11:57:07 UTC
* Is your device connected to your Mac ?

* What version of MonoTouch are you using ?

   run "/Developer/MonoTouch/usr/bin/mtouch --version"
Comment 13 gtissington 2011-08-17 12:03:06 UTC
mtouch version is   3.2.6.9797.    I have tried 4.0 also.  Same situation but 4.0 caused other errors too so I had to down grade.

yes. the device is connected via USB cable.   FYI, it is an ipod touch not an iPhone.
Comment 14 Sebastien Pouliot 2011-08-17 13:44:04 UTC
I never used MonoTouch 3.2 myself so I'm not sure if the --logdev options existed or if it required some other option to be specified (--help might have some hints).

Your other choice is to use XCode's organizer, select your device and check the "console" (not the device log). That should give you the same content as --logdev should provide.

It would be nice if you could open bug reports about any issue you encounter with MonoTouch 4.x since all future work is being done there. Thanks.

p.s. afaik ipod touch, iphone or ipad support should be identical
Comment 15 gtissington 2011-08-17 17:01:55 UTC
release version of the error:
===================================
Aug 17 14:00:10 unknown SpringBoard[29] <Notice>: MultitouchHID(1dd18330) uilock state: 1 -> 0
Aug 17 14:00:10 unknown kernel[0] <Debug>: set_crc_notification_state 0
Aug 17 14:00:11 unknown MobileStorageMounter[2384] <Notice>: (0x3ec9f48c) platform_supports_camera_import: Platform is configured for camera import
Aug 17 14:00:11 unknown MobileStorageMounter[2384] <Notice>: (0x3ec9f48c) media_attach_callback: Inspecting the attached media
Aug 17 14:00:11 unknown MobileStorageMounter[2384] <Notice>: (0x3ec9f48c) handle_attached_media: The attached media object is a disk image. Dropping like it's hot.
Aug 17 14:00:11 unknown MobileStorageMounter[2384] <Notice>: (0x3ec9f48c) media_attach_callback: Inspecting the attached media
Aug 17 14:00:11 unknown MobileStorageMounter[2384] <Notice>: (0x3ec9f48c) handle_attached_media: The attached media disk0s2s1 is not removable. Ignoring.
Aug 17 14:00:11 unknown MobileStorageMounter[2384] <Notice>: (0x3ec9f48c) media_attach_callback: Inspecting the attached media
Aug 17 14:00:11 unknown MobileStorageMounter[2384] <Notice>: (0x3ec9f48c) handle_attached_media: The attached media disk0s2 is not removable. Ignoring.
Aug 17 14:00:11 unknown MobileStorageMounter[2384] <Notice>: (0x3ec9f48c) media_attach_callback: Inspecting the attached media
Aug 17 14:00:11 unknown MobileStorageMounter[2384] <Notice>: (0x3ec9f48c) handle_attached_media: The attached media disk0s1 is not removable. Ignoring.
Aug 17 14:00:11 unknown MobileStorageMounter[2384] <Notice>: (0x3ec9f48c) media_attach_callback: Inspecting the attached media
Aug 17 14:00:11 unknown MobileStorageMounter[2384] <Notice>: (0x3ec9f48c) handle_attached_media: The attached media disk0 is not removable. Ignoring.
Aug 17 14:00:13 unknown kernel[0] <Debug>: launchd[2385] Builtin profile: container (sandbox)
Aug 17 14:00:13 unknown kernel[0] <Debug>: launchd[2385] Container: /private/var/mobile/Applications/7B95CB4A-5D46-4C09-BEC2-4B85744BA784 [69] (sandbox)
Aug 17 14:00:15 unknown UIKitApplication:test[0xe842][2385] <Notice>: Unhandled Exception: System.ExecutionEngineException: Attempting to JIT compile method 'System.Linq.OrderedEnumerable`1<Jrp.Snap.CardModelInfo>:GetEnumerator ()' while running with --aot-only.
Aug 17 14:00:15 unknown UIKitApplication:test[0xe842][2385] <Notice>:   at Jrp.Snap.CardManager+<>c__IteratorA.MoveNext () [0x00000] in <filename unknown>:0 
Aug 17 14:00:15 unknown UIKitApplication:test[0xe842][2385] <Notice>:   at System.Collections.Generic.List`1[Jrp.Snap.CardModelInfo].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in <filename unknown>:0 
Aug 17 14:00:15 unknown UIKitApplication:test[0xe842][2385] <Notice>:   at System.Collections.Generic.List`1[Jrp.Snap.CardModelInfo]..ctor (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
Aug 17 14:00:15 unknown UIKitApplication:test[0xe842][2385] <Notice>:   at System.Linq.Enumerable.ToList[CardModelInfo] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
Aug 17 14:00:15 unknown UIKitApplication:test[0xe842][2385] <Notice>:   at Test.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x00000] in <filename unknown>:0 
Aug 17 14:00:15 unknown UIKitApplication:test[0xe842][2385] <Notice>:   at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00000] in <filename unknown>:0 
Aug 17 14:00:15 unknown UIKitApplication:test[0xe842][2385] <Notice>:   at MonoTouch.UIKit.UIApplication.Main (Sys
Aug 17 14:00:15 unknown UIKitApplication:test[0xe842][2385] <Notice>: tem.String[] args) [0x00000] in <filename unknown>:0 
Aug 17 14:00:15 unknown UIKitApplication:test[0xe842][2385] <Notice>:   at Test.Application.Main (System.String[] args) [0x00000] in <filename unknown>:0 
Aug 17 14:00:15 unknown com.apple.launchd[1] <Warning>: (UIKitApplication:test[0xe842]) Exited with exit code: 1
Aug 17 14:00:15 unknown SpringBoard[29] <Warning>: Application 'Test' exited abnormally with exit status 1
Aug 17 14:00:21 unknown MobileStorageMounter[2384] <Notice>: (0x3ec9f48c) idle_timer_callback: Exiting after idle timeout



Debug version
=================================

Aug 17 14:01:51 unknown installd[2392] <Error>: libMobileGestalt copyInternationalMobileEquipmentIdentity: Could not get mobile equipment info dictionary
Aug 17 14:01:52 unknown lockdownd[17] <Error>: 2ff43000 handle_connection: Could not receive USB message #19 from mtouch. Killing connection
Aug 17 14:01:52 unknown lockdownd[17] <Error>: 001a2000 handle_connection: Could not receive USB message #8 from mtouch. Killing connection
Aug 17 14:01:52 unknown SpringBoard[29] <Warning>: Reloading application state for 'test' as its modification date has changed
Aug 17 14:01:52 unknown com.apple.mobile.lockdown[17] <Notice>: Could not receive size of messageCould not receive size of message
Aug 17 14:01:52 unknown SpringBoard[29] <Warning>: Reloading and rendering all application icons.
Aug 17 14:02:02 unknown SpringBoard[29] <Notice>: MultitouchHID(1dd18330) uilock state: 1 -> 0
Aug 17 14:02:02 unknown kernel[0] <Debug>: set_crc_notification_state 0
Aug 17 14:02:03 unknown MobileStorageMounter[2397] <Notice>: (0x3ec9f48c) platform_supports_camera_import: Platform is configured for camera import
Aug 17 14:02:03 unknown MobileStorageMounter[2397] <Notice>: (0x3ec9f48c) media_attach_callback: Inspecting the attached media
Aug 17 14:02:03 unknown MobileStorageMounter[2397] <Notice>: (0x3ec9f48c) handle_attached_media: The attached media object is a disk image. Dropping like it's hot.
Aug 17 14:02:03 unknown MobileStorageMounter[2397] <Notice>: (0x3ec9f48c) media_attach_callback: Inspecting the attached media
Aug 17 14:02:03 unknown MobileStorageMounter[2397] <Notice>: (0x3ec9f48c) handle_attached_media: The attached media disk0s2s1 is not removable. Ignoring.
Aug 17 14:02:03 unknown MobileStorageMounter[2397] <Notice>: (0x3ec9f48c) media_attach_callback: Inspecting the attached media
Aug 17 14:02:03 unknown MobileStorageMounter[2397] <Notice>: (0x3ec9f48c) handle_attached_media: The attached media disk0s2 is not removable. Ignoring.
Aug 17 14:02:03 unknown MobileStorageMounter[2397] <Notice>: (0x3ec9f48c) media_attach_callback: Inspecting the attached media
Aug 17 14:02:03 unknown MobileStorageMounter[2397] <Notice>: (0x3ec9f48c) handle_attached_media: The attached media disk0s1 is not removable. Ignoring.
Aug 17 14:02:03 unknown MobileStorageMounter[2397] <Notice>: (0x3ec9f48c) media_attach_callback: Inspecting the attached media
Aug 17 14:02:03 unknown MobileStorageMounter[2397] <Notice>: (0x3ec9f48c) handle_attached_media: The attached media disk0 is not removable. Ignoring.
Aug 17 14:02:05 unknown kernel[0] <Debug>: launchd[2398] Builtin profile: container (sandbox)
Aug 17 14:02:05 unknown kernel[0] <Debug>: launchd[2398] Container: /private/var/mobile/Applications/7B95CB4A-5D46-4C09-BEC2-4B85744BA784 [69] (sandbox)
Aug 17 14:02:13 unknown MobileStorageMounter[2397] <Notice>: (0x3ec9f48c) idle_timer_callback: Exiting after idle timeout
Comment 16 Sebastien Pouliot 2011-08-19 11:10:14 UTC
I cannot said for sure why this worked, on debug builds, on 3.2.x. My guess is that the debug builds included some extra code allowed the AOT compiler to generate the required code. Maybe the "linker" options are different between your Debug and Release builds ?

Anyway the issue exists in both Debug and Release builds on 4.x. Sadly there's a ton a generics hidden in the LINQ queries of your sample code. That makes it hard, without rewritting them, to fix your issue. I'll ask around if people have some tricks to ease fixing such cases.
Comment 17 gtissington 2011-08-19 11:13:54 UTC
Thanks,

But this query in the test program I sent you is the only one causing problems.

I have, as you say, 100's of Linq queries elsewhere in my full program that work just fine in both debug and release builds.

I'm really just looking for a work around for that one query.
Comment 18 Sebastien Pouliot 2011-08-21 10:10:43 UTC
I cannot duplicate the same error (on 4.x) as yourself (on 3.2.x) so all I can offer is some advice.

If you're sure it's only one query (I can't be) then work on this one. Try declaring/using the same "System.Linq.OrderedEnumerable`1<Jrp.Snap.CardModelInfo>:GetEnumerator ()" (from the message) in your application (make sure it won't be removed by the linker, e.g. not in some method that is never used). Wash, rinse, repeat for similar errors (hopefully there won't be many).

The second way to to drop the LINQ query and replace it with code. That will generally use less generics (like nested generic) and they will be declared as such (so less likely to be ambiguous to the AOT compiler).
Comment 19 gtissington 2011-08-22 19:07:16 UTC
I believe I have identified the underlying cause of this error.


The type in use in the collections that cause the error is a 'struct' not a 'class'.

Ergo when linq is trying to create the result sets it is attempting to cal the default parameterless constructor on the struct.  There is not default constructor and an exception is thrown.

I converted the 'struct's to 'class'es and everything appears to be fine now.


Does that make any sense to you?
Comment 20 Sebastien Pouliot 2011-08-22 19:36:30 UTC
Hey, I'm glad you found your issue. This looks like the limitation documented in:

http://ios.xamarin.com/Documentation/Limitations#Value_types_as_Dictionary_Keys

There's another workaround documented in case you wish to contionue using structs. No clue how this worked, but just under Debug, for 3.2.x :(