{"action":"created","issue":{"url":"https://api.github.com/repos/JamesNK/Newtonsoft.Json/issues/469","labels_url":"https://api.github.com/repos/JamesNK/Newtonsoft.Json/issues/469/labels{/name}","comments_url":"https://api.github.com/repos/JamesNK/Newtonsoft.Json/issues/469/comments","events_url":"https://api.github.com/repos/JamesNK/Newtonsoft.Json/issues/469/events","html_url":"https://github.com/JamesNK/Newtonsoft.Json/issues/469","id":55076193,"number":469,"title":"Apparent concurrency issue with JsonConvert.DeserializeObject","user":{"login":"mattklein999","id":5473675,"avatar_url":"https://avatars.githubusercontent.com/u/5473675?v=3","gravatar_id":"","url":"https://api.github.com/users/mattklein999","html_url":"https://github.com/mattklein999","followers_url":"https://api.github.com/users/mattklein999/followers","following_url":"https://api.github.com/users/mattklein999/following{/other_user}","gists_url":"https://api.github.com/users/mattklein999/gists{/gist_id}","starred_url":"https://api.github.com/users/mattklein999/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mattklein999/subscriptions","organizations_url":"https://api.github.com/users/mattklein999/orgs","repos_url":"https://api.github.com/users/mattklein999/repos","events_url":"https://api.github.com/users/mattklein999/events{/privacy}","received_events_url":"https://api.github.com/users/mattklein999/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-01-21T20:59:32Z","updated_at":"2015-01-22T17:41:41Z","closed_at":"2015-01-22T17:41:41Z","body":"We're currently using `JsonConvert.SerializeObject` and `JsonConvert.DeserializeObject<T>` to send data between a client and server.\r\n\r\nI created a tool that creates 10 clients, sends a command to 10 different servers, the servers serialize a response and send it back over the network, then the 10 clients deserialize the object on the local machine.\r\n\r\nI'm running these 10 tasks concurrently in the thread pool, and about 20% of the time **all** of the 'JsonConvert.DeserializeObject<T>' calls fail with the following stack trace:\r\n\r\n    Error: Index was outside the bounds of the array.\r\n    at System.Collections.Generic.List1.Add(T item)\r\n    at System.Collections.Generic.List1.System.Collections.IList.Add(Object item)\r\n    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)\r\n    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)\r\n    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)\r\n    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)\r\n    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)\r\n    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)\r\n    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)\r\n    at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)\r\n    at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)\r\n    at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)\r\n    at MyClientCode()\r\n\r\n`MyClientCode()` is using `DeserializeObject` like this:\r\n\r\n    string json = GetServerResponse();\r\n    return JsonConvert.DeserializeObject<ResponseObject>(json);\r\n\r\nThe `ResponseObject` is pretty big, and includes several composite objects. However, I can save the `json` and deserialize it properly with `DeserializeObject`, so I don't think the object structure is the problem.\r\n\r\nDoing some research of the `List` error indicates that this occurs when trying to modify a `List` object concurrently.\r\n\r\nI looked inside `JsonSerializerInternalReader` and noticed that it doesn't have any `lock`s or use the `System.Threading` namespace, but since it's an internal class I assume that thread synchronization is being handled by a public class.\r\n\r\nPlease let me know if I can provide any more information.\r\n\r\nThanks!"},"comment":{"url":"https://api.github.com/repos/JamesNK/Newtonsoft.Json/issues/comments/71063015","html_url":"https://github.com/JamesNK/Newtonsoft.Json/issues/469#issuecomment-71063015","issue_url":"https://api.github.com/repos/JamesNK/Newtonsoft.Json/issues/469","id":71063015,"user":{"login":"mattklein999","id":5473675,"avatar_url":"https://avatars.githubusercontent.com/u/5473675?v=3","gravatar_id":"","url":"https://api.github.com/users/mattklein999","html_url":"https://github.com/mattklein999","followers_url":"https://api.github.com/users/mattklein999/followers","following_url":"https://api.github.com/users/mattklein999/following{/other_user}","gists_url":"https://api.github.com/users/mattklein999/gists{/gist_id}","starred_url":"https://api.github.com/users/mattklein999/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mattklein999/subscriptions","organizations_url":"https://api.github.com/users/mattklein999/orgs","repos_url":"https://api.github.com/users/mattklein999/repos","events_url":"https://api.github.com/users/mattklein999/events{/privacy}","received_events_url":"https://api.github.com/users/mattklein999/received_events","type":"User","site_admin":false},"created_at":"2015-01-22T17:41:40Z","updated_at":"2015-01-22T17:41:40Z","body":"Thanks for the suggestions James.\r\n\r\nAfter digging deeper I found that you're right, we were using the same list object for multiple instances of the deserialized type. Specifically, the object looked something like this:\r\n\r\n    class Obj {\r\n        static List<string> _validSelections = new List<string>() { \"One\", \"Two\", \"Three\", \"Four\" };\r\n        public IEnumerable<string> ValidSelections { get { return _validSelections; } }\r\n        ... more ...\r\n    }\r\n\r\nThe exception was being thrown on line 1261 of JsonSerializerInternalReader.cs when trying to add objects to the list concurrently.\r\n\r\nAfter seeing how this is implemented in our code, I am going to get rid of the static backing since it wasn't providing us anything anyway. However, even if I lock around the deserializing, there is still an issue of having too many objects in each `obj.ValidSelections`.\r\n\r\nFor example, if I have 10 `Obj` objects, each with 4 values in `obj.ValidSelections`, then _every_ instance of `Obj` will end up with 40 elements in its `obj.ValidSelections` property. I'm not sure that this is really an issue with Json.NET or just bad design. If you feel that this is an actual issue, then I can create a new issue with more details.\r\n\r\nThanks again for your help and time!\r\n\r\n---\r\nI posted most of this thread to Stack Overflow just in case it helps others in the future:\r\n\r\nhttp://stackoverflow.com/questions/28095244/jsonconvert-deserializeobject-index-was-outside-the-bounds-of-the-array/28095245#28095245"}}