{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/corefx/issues/28030","repository_url":"https://api.github.com/repos/dotnet/corefx","labels_url":"https://api.github.com/repos/dotnet/corefx/issues/28030/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/28030/comments","events_url":"https://api.github.com/repos/dotnet/corefx/issues/28030/events","html_url":"https://github.com/dotnet/corefx/issues/28030","id":304956677,"number":28030,"title":"BlockingCollection.TryTake throws ArgumentNullException","user":{"login":"wisexp","id":12142053,"avatar_url":"https://avatars2.githubusercontent.com/u/12142053?v=4","gravatar_id":"","url":"https://api.github.com/users/wisexp","html_url":"https://github.com/wisexp","followers_url":"https://api.github.com/users/wisexp/followers","following_url":"https://api.github.com/users/wisexp/following{/other_user}","gists_url":"https://api.github.com/users/wisexp/gists{/gist_id}","starred_url":"https://api.github.com/users/wisexp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wisexp/subscriptions","organizations_url":"https://api.github.com/users/wisexp/orgs","repos_url":"https://api.github.com/users/wisexp/repos","events_url":"https://api.github.com/users/wisexp/events{/privacy}","received_events_url":"https://api.github.com/users/wisexp/received_events","type":"User","site_admin":false},"labels":[{"id":154923328,"url":"https://api.github.com/repos/dotnet/corefx/labels/area-System.Collections","name":"area-System.Collections","color":"d4c5f9","default":false},{"id":145399955,"url":"https://api.github.com/repos/dotnet/corefx/labels/bug","name":"bug","color":"207de5","default":true}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2018-03-13T22:20:29Z","updated_at":"2018-03-20T15:28:40Z","closed_at":"2018-03-20T15:28:40Z","author_association":"NONE","body":"Per MSDN https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.blockingcollection-1.trytake?view=netframework-4.7.1, BlockingCollection.TryTake should not throw ArgumentNullException, however we found this is not true, which cause unhandled exception.\r\n\r\nThe source code: https://referencesource.microsoft.com/#System/sys/system/collections/concurrent/BlockingCollection.cs,462c00bfa4630957 shows the function will call CheckDisposed() then proceed if it is not disposed, however this is not thread safe because another thread could dispose it once it passes the CheckDispose() check.  \r\n\r\nIs this a known issue?  Below is a mini-repro app:\r\n\r\n    class Test\r\n    {\r\n        public static async Task Run()\r\n        {\r\n            BlockingCollection<int> bd = new BlockingCollection<int>();\r\n            bd.Add(0);\r\n            Task[] tasks = new Task[2];\r\n\r\n            tasks[0] = Task.Run(() => TakeTask(bd));\r\n            tasks[1] = Task.Run(() => DisposeTask(bd));\r\n\r\n            await Task.WhenAll(tasks).ConfigureAwait(false);\r\n        }\r\n\r\n        private static void TakeTask(BlockingCollection<int> bd)\r\n        {            \r\n            try\r\n            {\r\n                int unused = 0;\r\n                bd.TryTake(out unused);\r\n            }\r\n            catch (ObjectDisposedException)\r\n            {\r\n                //expected, swallow\r\n            }\r\n            catch (ArgumentNullException ex)\r\n            {\r\n                // UNEXPECTED ArgumentNullException exception\r\n                Console.WriteLine(ex);\r\n            }\r\n        }\r\n\r\n        private static void DisposeTask(BlockingCollection<int> bd)\r\n        {\r\n            bd.Dispose();\r\n        }\r\n    }\r\n    class Program\r\n    {\r\n        \r\n        static void Main(string[] args)\r\n        {\r\n            Task.WhenAll(Enumerable.Range(0, 1000).Select(i => Test.Run())).GetAwaiter().GetResult();\r\n        }\r\n}\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/dotnet/corefx/issues/comments/374640439","html_url":"https://github.com/dotnet/corefx/issues/28030#issuecomment-374640439","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/28030","id":374640439,"user":{"login":"safern","id":22899328,"avatar_url":"https://avatars1.githubusercontent.com/u/22899328?v=4","gravatar_id":"","url":"https://api.github.com/users/safern","html_url":"https://github.com/safern","followers_url":"https://api.github.com/users/safern/followers","following_url":"https://api.github.com/users/safern/following{/other_user}","gists_url":"https://api.github.com/users/safern/gists{/gist_id}","starred_url":"https://api.github.com/users/safern/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/safern/subscriptions","organizations_url":"https://api.github.com/users/safern/orgs","repos_url":"https://api.github.com/users/safern/repos","events_url":"https://api.github.com/users/safern/events{/privacy}","received_events_url":"https://api.github.com/users/safern/received_events","type":"User","site_admin":false},"created_at":"2018-03-20T15:28:40Z","updated_at":"2018-03-20T15:28:40Z","author_association":"MEMBER","body":"Thanks @stephentoub. Closing by design then."}}
{"actor":{"display_login":"safern"}}