Bug 53509 - F# - InvalidProgramException (Invalid IL code) when RunOnUiThread called
Summary: F# - InvalidProgramException (Invalid IL code) when RunOnUiThread called
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: Compilers
Classification: Mono
Component: Other ()
Version: 4.8.0 (C9)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-03-19 17:13 UTC by Dragan
Modified: 2017-06-23 08:31 UTC (History)
4 users (show)

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


Attachments
Solution with C# and F# test (59.16 KB, application/zip)
2017-03-19 17:26 UTC, Dragan
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 GitHub or Developer Community 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_REPRODUCIBLE

Description Dragan 2017-03-19 17:13:53 UTC
Since update to Mono 4.8 I started seeing this exception whenever I try RunOnUiThread Activity method. Tried updating both VS Preview, Xamarin Studio, Xamarin Android, etc. to the latest and the greatest (beta channel) and still the same. However C# works great while F# does not. 

To reproduce use default project template and just make click callback to use RunOnUiThread like below. F# throws, C# works. Tried on both VS preview and Xamarin Studio 6.3. You have all the environment data from both IDEs below too.

##### Exception #####

System.InvalidProgramException: Invalid IL code in <StartupCode$android-fsharp-test>.$MainActivity/OnCreate@30-5:Invoke (): IL_0000: ldarg.1   


  at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/4468/2177a046/source/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36
  at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in /Users/builder/data/lanes/4468/2177a046/source/monodroid/src/Mono.Android/platforms/android-25/src/generated/Java.Lang.IRunnable.cs:81
  at at (wrapper dynamic-method) System.Object:20e039ff-a04b-4404-9087-a00bf7eda0fa (intptr,intptr)


##### Example code F# #####

type MainActivity () =
    inherit Activity ()

    let mutable count:int = 1

    override x.OnCreate (bundle) =

        base.OnCreate (bundle)

        // Set our view from the "main" layout resource
        x.SetContentView (Resources.Layout.Main)

        // Get our button from the layout resource, and attach an event to it
        let button = x.FindViewById<Button>(Resources.Id.myButton)
        button.Click.Add (fun args -> 
            x.RunOnUiThread (fun _ ->
                button.Text <- sprintf "%d clicks!" count
                count <- count + 1
            )
        )

##### Example code C# #####

public class MainActivity : Activity
	{
		int count = 1;

		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);

			// Get our button from the layout resource,
			// and attach an event to it
			Button button = FindViewById<Button>(Resource.Id.myButton);

			button.Click += delegate 
			{ 
				this.RunOnUiThread(() => button.Text = $"{count++} clicks!"); 
			};
		}
	}

##### ENVIRONMENT #####:

Visual Studio for Mac Preview
Version Preview 4 (7.0 build 1566)
Installation UUID: f37d0297-b74e-4682-9151-1c54b3fe066c
Runtime:
	Mono 4.8.0 (mono-4.8.0-branch/8f6d0f6) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 408000520

NuGet
Version: 4.0.0.2275

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

Apple Developer Tools
Xcode 8.2.1 (11766.1)
Build 8C1002

Xamarin Inspector
Version: 1.2.0-rc.3+build.1
Hash: 90011ff
Branch: d15-1
Build date: Thu, 16 Mar 2017 22:13:45 GMT

Xamarin.iOS
Version: 10.8.0.20 (Visual Studio Enterprise)
Hash: f94cf26
Branch: d15-1
Build date: 2017-03-14 12:37:51-0400

Xamarin.Android
Version: 7.2.0.2 (Visual Studio Enterprise)
Android SDK: /Users/dragan/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.4   (API level 19)
		5.0   (API level 21)
		6.0   (API level 23)
		7.0   (API level 24)
		7.1   (API level 25)

SDK Tools Version: 25.2.5
SDK Platform Tools Version: 25.0.4
SDK Build Tools Version: 23.0.2

Java SDK: /usr
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Xamarin.Mac
Version: 3.2.0.20 (Visual Studio Enterprise)

Build Information
Release ID: 700001566
Git revision: 2f11ef3c8909b95abcccc1b635c874f8eabf6ba0
Build date: 2017-03-06 18:12:59-05
Xamarin addins: 2bfd615b170b882de02930d3bceaf014c08b5540
Build lane: monodevelop-lion-vs-mac-preview4

Operating System
Mac OS X 10.12.3
Darwin Dragans-MacBook-Pro.local 16.4.0 Darwin Kernel Version 16.4.0
    Thu Dec 22 22:53:21 PST 2016
    root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64

Enabled user installed addins
Android Signature Tool 2.1.2
NuGet Package Explorer 0.1
Paket 0.3
xUnit.NET 2 testing framework support 0.7.2

##### ENVIRONMENT 2 #####:

Xamarin Studio Enterprise
Version 6.3 (build 834)
Installation UUID: 0b82817b-768a-41d0-b2ec-bdf9b1608b5d
Runtime:
	Mono 4.8.0 (mono-4.8.0-branch/8f6d0f6) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 408000520

NuGet
Version: 3.5.0.0

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

Apple Developer Tools
Xcode 8.2.1 (11766.1)
Build 8C1002

Xamarin.Mac
Version: 3.2.0.20 (Visual Studio Enterprise)

Xamarin.Android
Version: 7.2.0.2 (Visual Studio Enterprise)
Android SDK: /Users/dragan/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.4   (API level 19)
		5.0   (API level 21)
		6.0   (API level 23)
		7.0   (API level 24)
		7.1   (API level 25)

SDK Tools Version: 25.2.5
SDK Platform Tools Version: 25.0.4
SDK Build Tools Version: 23.0.2

Java SDK: /usr
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Xamarin Inspector
Version: 1.2.0-rc.3+build.1
Hash: 90011ff
Branch: d15-1
Build date: Thu, 16 Mar 2017 22:13:45 GMT

Xamarin.iOS
Version: 10.8.0.20 (Visual Studio Enterprise)
Hash: f94cf26
Branch: d15-1
Build date: 2017-03-14 12:37:51-0400

Build Information
Release ID: 603000834
Git revision: bb1dff522ee38d9963b4d97f6cfa0f92c85e8ecb
Build date: 2017-03-14 13:38:37-04
Xamarin addins: 8aac7919dc66c1ce0d9d75fe150ddc1ab15fd941
Build lane: monodevelop-lion-d15-1

Operating System
Mac OS X 10.12.3
Darwin Dragans-MacBook-Pro.local 16.4.0 Darwin Kernel Version 16.4.0
    Thu Dec 22 22:53:21 PST 2016
    root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64
Comment 1 Dragan 2017-03-19 17:26:35 UTC
Created attachment 20450 [details]
Solution with C# and F# test

Zipped sln with C# and F# projects to reproduce the bug
Comment 2 Dragan 2017-04-07 09:51:42 UTC
Bug is no longer present with VS for Mac Preview 6 and Mono Alpha 5.0.0.42. I would recommend closing the issue.
Comment 3 Prashant [MSFT] 2017-06-23 08:31:04 UTC
Resolving the bug based on reporter comment#2