Bug 29665

Summary: [Regression] System.InvalidProgramException: Invalid IL code
Product: [Mono] Runtime Reporter: moozzyk
Component: JITAssignee: Ludovic Henry <ludovic>
Status: RESOLVED FIXED    
Severity: normal CC: bbennewitz, bvinodkumar2008, dan, directhex, jo.shields, kakamatyi, masafa, mono-bugs+mono, mono-bugs+runtime, vargaz, wmmihaa
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:

Description moozzyk 2015-05-01 15:24:52 UTC
People are getting InvalidProgramExceptions with later versions of Mono on Linux. This seems like a regression in Mono because apparently things stopped working after moving to a newer version. 

There are two reports of this on stackoverflow:

http://stackoverflow.com/questions/29992634/issue-after-updating-to-mono-4-0-1-on-ubuntu

http://stackoverflow.com/questions/28288136/running-signalr-self-host-samples-throws-exception-with-latest-mono-dev-branch

I believe the code that triggers the error is this:
https://github.com/SignalR/SignalR/blob/8b7c92faf050a621abfd7dcfbe5fcdbe0a1c643a/src/Microsoft.AspNet.SignalR.Core/Infrastructure/CancellationTokenExtensions.cs#L59

Thanks, 
Pawel
Comment 1 Marek Safar 2015-05-04 12:54:56 UTC
This looks like JIT regression. Same code compiled with master/4.0 mcs works when executed with Mono 3.12
Comment 2 Marek Safar 2015-05-04 13:01:42 UTC
4.0 stack trace

[ERROR] FATAL UNHANDLED EXCEPTION: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidProgramException: Invalid IL code in (wrapper delegate-invoke) <Module>:invoke_callvirt_CancellationTokenRegistration_CancellationToken&_Action`1<object>_object (System.Threading.CancellationToken&,System.Action`1<object>,object): IL_0057: castclass 0x00000007


  at Microsoft.AspNet.SignalR.Infrastructure.CancellationTokenExtensions.SafeRegister (CancellationToken cancellationToken, System.Action`1 callback, System.Object state) [0x00000] in <filename unknown>:0
  at Microsoft.AspNet.SignalR.Hosting.HostDependencyResolverExtensions.InitializeResolverDispose (IDependencyResolver resolver, CancellationToken hostShutdownToken) [0x00000] in <filename unknown>:0
  at Microsoft.AspNet.SignalR.Hosting.HostDependencyResolverExtensions.InitializeHost (IDependencyResolver resolver, System.String instanceName, CancellationToken hostShutdownToken) [0x00000] in <filename unknown>:0

master stack trace

ERROR] FATAL UNHANDLED EXCEPTION: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidProgramException: Invalid IL code in (wrapper delegate-invoke) <Module>:invoke_callvirt_CancellationTokenRegistration_CancellationToken&_Action`1<object>_object (System.Threading.CancellationToken&,System.Action`1<object>,object): IL_0053: castclass 0x00000006


  at Microsoft.AspNet.SignalR.Infrastructure.CancellationTokenExtensions.SafeRegister (CancellationToken cancellationToken, System.Action`1 callback, System.Object state) <0x2069350 + 0x000ca> in <filename unknown>:0
  at Microsoft.AspNet.SignalR.Hosting.HostDependencyResolverExtensions.InitializeResolverDispose (IDependencyResolver resolver, CancellationToken hostShutdownToken) <0x2068eb8 + 0x00077> in <filename unknown>:0
  at Microsoft.AspNet.SignalR.Hosting.HostDependencyResolverExtensions.InitializeHost (IDependencyResolver resolver, System.String instanceName, CancellationToken hostShutdownToken) <0x2068dd0 + 0x0005f> in <filename unknown>:0
Comment 3 Zoltan Varga 2015-05-06 04:15:21 UTC
How I can reproduce this ?
Comment 4 Marek Safar 2015-05-06 04:19:09 UTC
Quite simple

git clone git@github.com:SignalR/SignalR.git
./build.sh
cd samples/Microsoft.AspNet.SelfHost.Samples/bin/Debug/
mono Microsoft.AspNet.SelfHost.Samples.exe
Comment 5 Zoltan Varga 2015-05-06 04:55:57 UTC
Fixed in mono master ae495e8bd485f48ecdb7e53d7e98771220f31997.
Comment 6 Marek Safar 2015-05-09 05:45:35 UTC
*** Bug 26660 has been marked as a duplicate of this bug. ***
Comment 7 Vinod 2015-06-03 22:05:57 UTC
Hi,

I'm using Mono JIT compiler version 4.0.1, Microsoft.AspNet.SignalR.Core nuget package and i am getting the same above exception on running the below section of code in owin startup class:
 // Run the SignalR pipeline. We're not using MapSignalR
 // since this branch already runs under the "/signalr"
 // path.
map.RunSignalR(hubConfiguration);

Am i missing anything? Any help is highly appreciated !!
Comment 8 Zoltan Varga 2015-06-03 22:37:02 UTC
The fix will be in the upcoming mono 4.2 version.
Comment 9 Mikael 2015-06-06 01:24:11 UTC
Would you be able to give us an estimated timeframe for when the 4.2 alpha release will be available?
Comment 10 Zoltan Varga 2015-06-06 09:17:29 UTC
Probly in july. It will be built from this git branch:
https://github.com/mono/mono/tree/mono-4.2.0-pre-branch
Comment 11 Mikael 2015-06-12 08:02:14 UTC
Zoltan,
I've been trying to downgrade to 3.12.1 for about a week. I've tried everything! I finally reverted back to reinstalling the machine (Ubuntu 12.04), but I still can't get it to work. This is what I've tried, after re-installing the server:

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

$ echo "deb http://download.mono-project.com/repo/debian wheezy/snapshots/3.12.1 main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list

$ sudo apt-get update
echo "deb http://download.mono-project.com/repo/debian wheezy-libtiff-compat main" | sudo tee -a /etc/apt/sources.list.d/mono-xamarin.list

$ sudo apt-get install mono-complete

After installing I run: mono --version:

myuser@msbubuntu2:~/host$ mono --version
Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-1ubuntu2.3)
Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            Included Boehm (with typed GC and Parallel Mark)


I don't know why I get 2.10.8.1, and my application is not running.
PLEASE HELP!
Comment 12 Mikael 2015-06-12 08:03:58 UTC
Zoltan,
I've been trying to downgrade to 3.12.1 for about a week. I've tried everything! I finally reverted back to reinstalling the machine (Ubuntu 12.04), but I still can't get it to work. This is what I've tried, after re-installing the server:

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

$ echo "deb http://download.mono-project.com/repo/debian wheezy/snapshots/3.12.1 main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list

$ sudo apt-get update
echo "deb http://download.mono-project.com/repo/debian wheezy-libtiff-compat main" | sudo tee -a /etc/apt/sources.list.d/mono-xamarin.list

$ sudo apt-get install mono-complete

After installing I run: mono --version:

myuser@msbubuntu2:~/host$ mono --version
Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-1ubuntu2.3)
Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            Included Boehm (with typed GC and Parallel Mark)


I don't know why I get 2.10.8.1, and my application is not running.
PLEASE HELP!
Comment 13 Zoltan Varga 2015-06-13 00:27:57 UTC
@Jo, could you answer that ?
Comment 14 Jo Shields 2015-06-13 04:48:03 UTC
If that commit applies against 4.0.2, I can hotfix it on Linux later today. Easily done. 

Presumably the user is having issues because the snapshot is in 3.12.0, not 3.12.1
Comment 15 Mikael 2015-06-13 06:10:06 UTC
I've tried using 3.12.0 as well. After running sudo apt-get install mono-complete I get this error:

myuser@msbUbuntu1:~$ sudo apt-get install mono-complete
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 mono-complete : Depends: mono-devel (= 3.12.1-0xamarin1) but it is not going to be installed
                 Depends: mono-4.0-service (= 3.12.1-0xamarin1) but it is not going to be installed
                 Depends: libmono-cil-dev (= 3.12.1-0xamarin1) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
myuser@msbUbuntu1:~$
Comment 16 Jo Shields 2015-06-13 06:11:40 UTC
Given you're on Ubuntu 12.04, you added the mandatory extra repository?

http://www.mono-project.com/docs/getting-started/install/linux/#older-ubuntu-releases-ubuntu-1210-and-1204
Comment 17 Jo Shields 2015-06-13 06:38:19 UTC
Hotfix for 4.0 uploaded to alpha repository (ARM builds aren't finished yet)

http://www.mono-project.com/docs/getting-started/install/linux/#debian-ubuntu-and-derivatives-alpha-channel
Comment 18 Mikael 2015-06-14 20:15:41 UTC
@Jo, thanks for the help and I can confirm that my application using SignalR works with the new build.
Comment 19 kakamatyi 2015-08-02 14:20:35 UTC
Hi guys,
Can you give me some hints how to fix this issue compiling mono from source release 4.0.2.5?
Comment 20 Brian Bennewitz 2015-08-27 03:33:05 UTC
https://insights.xamarin.com/app/SportsLock/issues/133

I haven't seen this is any Mono.Android release notes, is there an ETA on this?
Comment 21 Jo Shields 2015-08-27 03:49:27 UTC
(In reply to comment #20)
> https://insights.xamarin.com/app/SportsLock/issues/133
> 
> I haven't seen this is any Mono.Android release notes, is there an ETA on this?

directhex@marceline:~/Projects/mono$ git tag --contains ae495e8bd485f48ecdb7e53d7e98771220f31997
mono-4.2.0.179

Currently in alpha branch.