{"action":"opened","issue":{"number":62,"created_at":"2011-10-14T12:50:42Z","pull_request":{"diff_url":null,"patch_url":null,"html_url":null},"body":"Calling substitutes from multiple threads (e.g. using plinq / TPL) can cause problems.\r\n\r\nOriginal report used parallel exec in the business logic and resulted in \"Index outside of range exceptions\" in System.Collections.Generic.Stack`1.Push(T item) inside the NSubstitute code. Exception frequency was 1 to 5 times out of 500 tests. Making `CallStack.cs` thread-safe seemed to fix this problem. \r\n\r\nReport mentioned similar probs with Moq: http://code.google.com/p/moq/issues/detail?id=249 and http://code.google.com/p/moq/issues/detail?id=265\r\n\r\nExample code: \r\n````csharp\r\nQueue<Task> fetchTaskQueue = new Queue<Task>();       \r\nfetchTaskQueue.Enqueue(Task.Factory.StartNew(() => task1 = this.dataAccess.GetMethod1(Id)));\r\nfetchTaskQueue.Enqueue(Task.Factory.StartNew(() => task2 = this.dataAccess.GetMethod2(Id)));\r\nfetchTaskQueue.Enqueue(Task.Factory.StartNew(() => task3 = this.dataAccess.GetMethod3(Id)));\r\nfetchTaskQueue.Enqueue(Task.Factory.StartNew(() => task4 = this.dataAccess.GetMethod4(Id)));\r\nfetchTaskQueue.Enqueue(Task.Factory.StartNew(() => task5 = this.dataAccess.GetMethod5(Id)));\r\nfetchTaskQueue.Enqueue(Task.Factory.StartNew(() => task6 = this.dataAccess.GetMethod6(Id)));\r\nfetchTaskQueue.Enqueue(Task.Factory.StartNew(() => task7 = this.dataAccess.GetMethod7(Id)));\r\nTask.WaitAll(fetchTaskQueue.ToArray());\r\n````\r\n\r\nThanks to Aleksander Spro for the report.","comments":0,"title":"Multi-threaded calls to a substitute","updated_at":"2011-10-14T12:50:42Z","url":"https://api.github.com/repos/nsubstitute/NSubstitute/issues/62","id":1910052,"assignee":null,"milestone":null,"closed_at":null,"user":{"gravatar_id":"1390df8051dd4455a02939d2ec3c6d55","avatar_url":"https://secure.gravatar.com/avatar/1390df8051dd4455a02939d2ec3c6d55?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","url":"https://api.github.com/users/dtchepak","id":70105,"login":"dtchepak"},"html_url":"https://github.com/nsubstitute/NSubstitute/issues/62","labels":[],"state":"open"},"legacy":{"number":62,"action":"opened","issue":1910052}}