Bug 26660

Summary: Running Microsoft.AspNet.SelfHost.Samples with latest mono branch (3.99) throws exception while it works in stable branch
Product: [Mono] Runtime Reporter: Dan Jarvis <dan>
Component: GeneralAssignee: Bugzilla <bugzilla>
Status: RESOLVED DUPLICATE    
Severity: normal CC: gerald.lochner, masafa, mono-bugs+mono, mono-bugs+runtime
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: Other   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:

Description Dan Jarvis 2015-02-02 17:28:43 UTC
Running this sample (or any time you insert SignalR into the OWIN pipeline by using map.UseSignalR()) works with stable mono release but fails with this exception in latest branch:


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_004f: 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 
  at Owin.OwinExtensions.UseSignalRMiddleware[PersistentConnectionMiddleware] (IAppBuilder builder, System.Object[] args) [0x00000] in <filename unknown>:0 
  at Owin.OwinExtensions.RunSignalR (IAppBuilder builder, System.Type connectionType, Microsoft.AspNet.SignalR.ConnectionConfiguration configuration) [0x00000] in <filename unknown>:0 
  at Owin.OwinExtensions.RunSignalR[RawConnection] (IAppBuilder builder, Microsoft.AspNet.SignalR.ConnectionConfiguration configuration) [0x00000] in <filename unknown>:0 
  at Owin.OwinExtensions.RunSignalR[RawConnection] (IAppBuilder builder) [0x00000] in <filename unknown>:0 
  at Microsoft.AspNet.SelfHost.Samples.Startup.<Configuration>b__0 (IAppBuilder map) [0x00000] in <filename unknown>:0 
  at Owin.MapExtensions.Map (IAppBuilder app, PathString pathMatch, System.Action`1 configuration) [0x00000] in <filename unknown>:0 
  at Owin.MapExtensions.Map (IAppBuilder app, System.String pathMatch, System.Action`1 configuration) [0x00000] in <filename unknown>:0 
  at Microsoft.AspNet.SelfHost.Samples.Startup.Configuration (IAppBuilder app) [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <filename unknown>:0 
  at Owin.Loader.DefaultLoader+<>c__DisplayClass12.<MakeDelegate>b__b (IAppBuilder builder) [0x00000] in <filename unknown>:0 
  at Owin.Loader.DefaultLoader+<>c__DisplayClass1.<LoadImplementation>b__0 (IAppBuilder builder) [0x00000] in <filename unknown>:0 
  at Microsoft.Owin.Hosting.Engine.HostingEngine.ResolveApp (Microsoft.Owin.Hosting.Engine.StartContext context) [0x00000] in <filename unknown>:0 
  at Microsoft.Owin.Hosting.Engine.HostingEngine.Start (Microsoft.Owin.Hosting.Engine.StartContext context) [0x00000] in <filename unknown>:0 
  at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start (Microsoft.Owin.Hosting.StartOptions options) [0x00000] in <filename unknown>:0 
  at Microsoft.Owin.Hosting.Starter.HostingStarter.Start (Microsoft.Owin.Hosting.StartOptions options) [0x00000] in <filename unknown>:0 
  at Microsoft.Owin.Hosting.WebApp.StartImplementation (IServiceProvider services, Microsoft.Owin.Hosting.StartOptions options) [0x00000] in <filename unknown>:0 
  at Microsoft.Owin.Hosting.WebApp.Start (Microsoft.Owin.Hosting.StartOptions options) [0x00000] in <filename unknown>:0 
  at Microsoft.Owin.Hosting.WebApp.Start[Startup] (Microsoft.Owin.Hosting.StartOptions options) [0x00000] in <filename unknown>:0 
  at Microsoft.Owin.Hosting.WebApp.Start[Startup] (System.String url) [0x00000] in <filename unknown>:0 
  at Microsoft.AspNet.SelfHost.Samples.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[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_004f: castclass 0x00000007
Comment 1 Marek Safar 2015-02-04 13:08:56 UTC
It looks like miscompiled lambda you are trying to register.

Could you attach some kind of repro
Comment 2 Gerald Lochner 2015-05-09 02:51:52 UTC
I've the same issue. 

@Marek: 
use: https://github.com/SignalR/SignalR
Open: Microsoft.AspNet.SignalR.Mono.sln in MonoDevelop or use make
Start Project: samples\Microsoft.AspNet.SelfHost.Samples

Now you get the exception Dan reported at:

Microsoft.AspNet.SignalR.Core\Infrastructure\CancellationTokenExtensions.cs
Line 22: 
            // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
            CancellationTokenRegistration registration = _tokenRegister(ref cancellationToken, s => InvokeCallback(s), callbackWrapper);

Now the question, what is _tokenRegister?

It is a delegate created by method ResolveRegisterDelegate of the same class. The delegate references to a method in System.Threading.CancellationToken.InternalRegisterWithoutEC (EC: Execution Context). OK. My read is that the error occurs when executing the delegate with lambda expressions... 

For NETFX_CORE a fallback is defined, so a workaround is for compiling under Mono to use that one.


#if NETFX_CORE || PORTABLE
            return fallback;
#else 

Hopefully that helps a little bit.
Comment 4 Marek Safar 2015-05-09 05:45:35 UTC
duplicate

*** This bug has been marked as a duplicate of bug 29665 ***