Bug 33981 - Possible race condition in Registrar.NewRefCountDynamicRegistrar.AddCustomType
Summary: Possible race condition in Registrar.NewRefCountDynamicRegistrar.AddCustomType
Status: VERIFIED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.0 (iOS9)
Hardware: PC Mac OS
: --- major
Target Milestone: 9.2 (C6)
Assignee: Rolf Bjarne Kvinge [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2015-09-15 15:04 UTC by Todd Diehl
Modified: 2015-11-03 03:38 UTC (History)
5 users (show)

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

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:
VERIFIED FIXED

Description Todd Diehl 2015-09-15 15:04:50 UTC
In the latest Xamarin iOS 9 build, we are seeing what looks like a race condition in the Registrar.  We were not observing this when building with Xamarin iOS 8.  The issue can be observed both in the simulator and on device (below is a device error message).

Sep 12 11:29:34 iPad-Mini Logos[920] <Warning>: Unhandled managed exception:
        An item with the same key has already been added. (System.ArgumentException)
          at System.Collections.Generic.Dictionary`2[TKey,TValue].Insert (System.Collections.Generic.TKey key, System.Collections.Generic.TValue value, Boolean add) [0x00000] in <filename unknown>:0 
          at System.Collections.Generic.Dictionary`2[TKey,TValue].Add (System.Collections.Generic.TKey key, System.Collections.Generic.TValue value) [0x00000] in <filename unknown>:0 
          at Registrar.NewRefCountDynamicRegistrar.AddCustomType (System.Type type) [0x00000] in <filename unknown>:0 
          at Registrar.DynamicRegistrar.Lookup (IntPtr class, Boolean throw_on_error) [0x00000] in <filename unknown>:0 
          at ObjCRuntime.Class.Lookup (IntPtr klass, Boolean throw_on_error) [0x00000] in <filename unknown>:0 
          at ObjCRuntime.Runtime.GetNSObject[T] (IntPtr ptr) [0x00000] in <filename unknown>:0 
          at Mapbox.RMAbstractMercatorTileSource.get_MercatorToTileProjection () [0x00000] in <filename unknown>:0 
          at Faithlife.Atlas.Mobile.iOS.AtlasMapboxSource.ImageForTile (RMTile tile, Mapbox.RMTileCache tileCache) [0x00000] in <filename unknown>:0
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-10-05 06:31:10 UTC
Fixed.

maccore/master: 4af7cc543881b587a58b708a146a45502a3e0990
maccore/master: 954136188ed425108e4501c2728e7064641f26f6
Comment 2 Rolf Bjarne Kvinge [MSFT] 2015-10-05 06:52:55 UTC
QA: unit tests were added with the fixes, but there's also a test project here: https://github.com/rolfbjarne/bug33981 (run and the screen turns red on failure and green on success).
Comment 4 Peter Major 2015-10-23 04:30:40 UTC
We're seeing this crash on 8/10 launches of our application?

Is downgrading to Xamarin.iOS 8.x a workaround? Any other suggestions?
Comment 5 Rolf Bjarne Kvinge [MSFT] 2015-10-23 04:35:03 UTC
@Peter, yes, downgrading to Xamarin.iOS 8.x is a workaround.

Another potential workaround would be to collect all the types in your app inheriting from NSObject, and do something like this on each of them (on the main thread before doing anything on any other threads):

    ObjCRuntime.Class.Lookup (new ObjcRuntime.Class (type));
Comment 6 Shruti 2015-10-28 10:16:33 UTC
***********************************************************
Reproduce Status:
***********************************************************
I am able to reproduce this issue using monotouch-9.1.0.18.pkg and getting red screen on device and simulator after launching the attached project given in comment(2).
Screencast: http://www.screencast.com/t/GMXSL5NupniZ

***********************************************************
Verify Status (Trello card build) : Not working on device
***********************************************************
Simulator: Getting green screen on launching the project on simulator.
Device : Getting red screen on launching the project on device.
Below are the log which got during deploying app on device:
Application Output: https://gist.github.com/shrutis360/4a2615a1d53aa5a39632
Build Output:https://gist.github.com/shrutis360/3a12106a44cd75b1d3ed
Environment Info: https://gist.github.com/shrutis360/c692461344d035f8e2d8

***********************************************************
Verify Status  (Master build) : Not working on device
***********************************************************
Simulator: Getting green screen on launching the project on simulator.
Device : Getting red screen on launching the project on device.
Below are the log which got during deploying app on device:
Application Output: https://gist.github.com/shrutis360/ff7b9a15ab31905fb27e
Build Output: https://gist.github.com/shrutis360/be98212d88a018ada059
Environment Info: https://gist.github.com/shrutis360/7820072e2b750ec4ab97

Device: 4S iOS 9.1
Comment 7 Rolf Bjarne Kvinge [MSFT] 2015-11-02 06:46:47 UTC
@Shruti, did you clean the project before trying with master? From the build log the project wasn't actually rebuilt:

>	Target _CompileToNative:
>	Skipping target "_CompileToNative" because its outputs are up-to-date.

Can you also update your repro project checkout before trying master again? I added a Console.WriteLine that should give more information (if it breaks).
Comment 8 Shruti 2015-11-02 08:57:03 UTC
I have checked this issue again on latest Master build and getting green screen after launching app   given in comment(2)  on simulator and device as well.

Screencast: http://www.screencast.com/t/XV1v2z2zDcD
Environment Info:https://gist.github.com/shrutis360/f9d7f26a15bb659b2fb7

I will close this issue once I get the fix of this issue in C6 build.
Comment 9 Shruti 2015-11-03 03:38:47 UTC
This is working fine with C6 monotouch-9.2.1.35_39f1950d94912e5b86e6793bb850340efd3cc2fa. Getting green screen after launching app  given in comment(2)  on simulator and device as well.


Environment Info https://gist.github.com/shrutis360/d1f4c74df116b46c71ce