{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/corefx/issues/6258","repository_url":"https://api.github.com/repos/dotnet/corefx","labels_url":"https://api.github.com/repos/dotnet/corefx/issues/6258/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/6258/comments","events_url":"https://api.github.com/repos/dotnet/corefx/issues/6258/events","html_url":"https://github.com/dotnet/corefx/issues/6258","id":135005515,"number":6258,"title":"Infinite loop in dynamic dispatch code when calling through contravariant delegate","user":{"login":"gregg-miskelly","id":11078489,"avatar_url":"https://avatars.githubusercontent.com/u/11078489?v=3","gravatar_id":"","url":"https://api.github.com/users/gregg-miskelly","html_url":"https://github.com/gregg-miskelly","followers_url":"https://api.github.com/users/gregg-miskelly/followers","following_url":"https://api.github.com/users/gregg-miskelly/following{/other_user}","gists_url":"https://api.github.com/users/gregg-miskelly/gists{/gist_id}","starred_url":"https://api.github.com/users/gregg-miskelly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gregg-miskelly/subscriptions","organizations_url":"https://api.github.com/users/gregg-miskelly/orgs","repos_url":"https://api.github.com/users/gregg-miskelly/repos","events_url":"https://api.github.com/users/gregg-miskelly/events{/privacy}","received_events_url":"https://api.github.com/users/gregg-miskelly/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/corefx/labels/System.Linq.Expressions","name":"System.Linq.Expressions","color":"d4c5f9"}],"state":"open","locked":false,"assignee":{"login":"VSadov","id":8218165,"avatar_url":"https://avatars.githubusercontent.com/u/8218165?v=3","gravatar_id":"","url":"https://api.github.com/users/VSadov","html_url":"https://github.com/VSadov","followers_url":"https://api.github.com/users/VSadov/followers","following_url":"https://api.github.com/users/VSadov/following{/other_user}","gists_url":"https://api.github.com/users/VSadov/gists{/gist_id}","starred_url":"https://api.github.com/users/VSadov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/VSadov/subscriptions","organizations_url":"https://api.github.com/users/VSadov/orgs","repos_url":"https://api.github.com/users/VSadov/repos","events_url":"https://api.github.com/users/VSadov/events{/privacy}","received_events_url":"https://api.github.com/users/VSadov/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/dotnet/corefx/milestones/6","html_url":"https://github.com/dotnet/corefx/milestones/1.0.0-rc2","labels_url":"https://api.github.com/repos/dotnet/corefx/milestones/6/labels","id":1365455,"number":6,"title":"1.0.0-rc2","description":"","creator":{"login":"joshfree","id":1302850,"avatar_url":"https://avatars.githubusercontent.com/u/1302850?v=3","gravatar_id":"","url":"https://api.github.com/users/joshfree","html_url":"https://github.com/joshfree","followers_url":"https://api.github.com/users/joshfree/followers","following_url":"https://api.github.com/users/joshfree/following{/other_user}","gists_url":"https://api.github.com/users/joshfree/gists{/gist_id}","starred_url":"https://api.github.com/users/joshfree/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joshfree/subscriptions","organizations_url":"https://api.github.com/users/joshfree/orgs","repos_url":"https://api.github.com/users/joshfree/repos","events_url":"https://api.github.com/users/joshfree/events{/privacy}","received_events_url":"https://api.github.com/users/joshfree/received_events","type":"User","site_admin":false},"open_issues":54,"closed_issues":443,"state":"open","created_at":"2015-10-20T04:29:15Z","updated_at":"2016-02-20T00:08:06Z","due_on":"2016-02-02T08:00:00Z","closed_at":null},"comments":6,"created_at":"2016-02-19T23:53:38Z","updated_at":"2016-02-20T03:25:13Z","closed_at":null,"body":"When upgrading from 23803 to 23818 of System.Linq.Expression.dll we are now seeing an infinite loop in the dynamic dispatch code. I have not been able to come up with a minimal repro that I can attach to this bug, but I have a trivial one that I can email anyone who wants it. In the bug we are calling into a delegate of type Action<... my-class>, and the target of the delegate uses an interface for the last argument. If I change the delegate to also use an interface, the issue goes away.\r\n\r\nThe infinite loop is in code labeled as 'Lightweight Function' by the managed debugger:\r\n\r\n`System.Dynamic.Runtime.dll!System.Runtime.CompilerServices.CallSiteBinder.BindCore<System.Action<System.Runtime.CompilerServices.CallSite, System.Action<string, object, OpenDebug.DispatchResponder>, string, object, OpenDebug.DispatchResponder>>(System.Runtime.CompilerServices.CallSite<System.Action<System.Runtime.CompilerServices.CallSite, System.Action<string, object, OpenDebug.DispatchResponder>, string, object, OpenDebug.DispatchResponder>> site, object[] args) Line 133` <-- This function is called infinately\r\n `[Lightweight Function]` **<-- Hang here**\r\n`OpenDebugAD7.dll!OpenDebug.DispatcherProtocol.Dispatch(string req)` <-- My code calling delegate.Invoke\r\n\r\nLooking at the stack in Windbg, I think Windbg gives the method this name:\r\n`26 00000027`ac5fec80 0000019d`bcc88cc8 DynamicClass.CallSite.Target(System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, System.Action'3<System.String,System.Object,OpenDebug.DispatchResponder>, System.String, System.Object, OpenDebug.DispatchResponder)+0x2c2`\r\n \r\nThis looks similar to #5610, but I should have the fix for that and I am still seeing the problem.\r\n"},"comment":{"url":"https://api.github.com/repos/dotnet/corefx/issues/comments/186497232","html_url":"https://github.com/dotnet/corefx/issues/6258#issuecomment-186497232","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/6258","id":186497232,"user":{"login":"VSadov","id":8218165,"avatar_url":"https://avatars.githubusercontent.com/u/8218165?v=3","gravatar_id":"","url":"https://api.github.com/users/VSadov","html_url":"https://github.com/VSadov","followers_url":"https://api.github.com/users/VSadov/followers","following_url":"https://api.github.com/users/VSadov/following{/other_user}","gists_url":"https://api.github.com/users/VSadov/gists{/gist_id}","starred_url":"https://api.github.com/users/VSadov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/VSadov/subscriptions","organizations_url":"https://api.github.com/users/VSadov/orgs","repos_url":"https://api.github.com/users/VSadov/repos","events_url":"https://api.github.com/users/VSadov/events{/privacy}","received_events_url":"https://api.github.com/users/VSadov/received_events","type":"User","site_admin":false},"created_at":"2016-02-20T03:25:13Z","updated_at":"2016-02-20T03:25:13Z","body":"Here is a small attempt to repro that I could infer from the binaries.\r\nI did not try to repro on actual CoreFx, but I think it will repro.\r\n\r\nI think the issue here is that we've added an optimization based on incorrect assertion that if static Expr type is sealed it is guaranteed to have values of only that type.\r\nThat is inaccurate because of delegate variance. Delegate types are sealed but could yet be assignment compatible.\r\n\r\n```C#\r\n    class Program\r\n    {\r\n        class C1 : ICloneable\r\n        {\r\n            public object Clone()\r\n            {\r\n                throw new NotImplementedException();\r\n            }\r\n        }\r\n\r\n        private static Action<string, dynamic, C1> a;\r\n        // the following change fixes the problem\r\n        // private static Action<string, dynamic, ICloneable> a;\r\n\r\n        private static void Foo(string s, dynamic d, ICloneable s1)\r\n        {\r\n            System.Console.WriteLine(\"hello\");\r\n\r\n            // static type\r\n            System.Console.WriteLine(\"expr type: \" + typeof(Action<string, dynamic, C1>));\r\n\r\n            // dynamic type\r\n            System.Console.WriteLine(\"actual type: \" + a.GetType());\r\n\r\n            // is it sealed?\r\n            System.Console.WriteLine(\"issealed: \" + typeof(Action<string, dynamic, C1>).IsSealed);\r\n\r\n        }\r\n\r\n        private static void Init()\r\n        {\r\n            Action<string, dynamic, ICloneable> del = Foo;        \r\n            a = del;\r\n        }\r\n\r\n        static void Main(string[] args)\r\n        {\r\n            Init();\r\n\r\n            dynamic arg = \"ww\";\r\n\r\n            a.Invoke(\"qq\", arg, new C1());\r\n        }\r\n    }\r\n```\r\n\r\n\r\n"}}