Running Mono 3.2.6 on Debian 7
When using Newtonsoft.Json to deserialize objects in a multi-threaded application, my application crashes due to a โsegmentation errorโ.
I attached gdb and here is the output:
root@ns38225 :/home/stress
Here is the code to reproduce the problem (remember the hard data directory)
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json; namespace TaskStressTest { class IntObj { public int Int; } class Program { static long highestId = 0; private static long done = 0; static object cntLock = new object(); static List<IntObj> tIds = new List<IntObj>(); static void Main(string[] args) { Thread.Sleep(60000); var filePaths = Directory.GetFiles("/home/cs/data", "*.json"); var fileCount = filePaths.Length; var f = 0; foreach (var filePath in filePaths) { var tasks = new List<Task>(); var file = File.OpenRead(filePath); var stream = new StreamReader(file); var line = ""; while (!String.IsNullOrWhiteSpace(line = stream.ReadLine())) { var line1 = line; tasks.Add(Task.Run(async() => { var tId = new IntObj {Int = Thread.CurrentThread.ManagedThreadId}; lock (cntLock) { tIds.Add(tId); var ints = tIds.Select(x => x.Int).Distinct().OrderBy(i1 => i1).ToList(); var active = ints.Count(); if (active > highestId) { highestId = active; Console.WriteLine(active); } } await DoWork(line1); lock (cntLock) { tIds.Remove(tId); } })); } tasks.ForEach(task => { task.Wait(); var doneNow = ++done;
Actually, I have no idea where to go from here, can anyone give me any instructions on what to do next?
UPDATE
I just changed my code to delegate deserialization to a loop in the main thread, and the nickname happened again, this time in a little more detail:
Stacktrace: at <unknown> <0xffffffff> at (wrapper managed-to-native) object.__icall_wrapper_mono_gc_alloc_vector (intptr,intptr,intptr) <0xffffffff> at (wrapper alloc) object.AllocVector (intptr,intptr) <0xffffffff> at Newtonsoft.Json.JsonTextReader..ctor (System.IO.TextReader) <0x0006b> at Newtonsoft.Json.JsonConvert.DeserializeObject (string,System.Type,Newtonsoft.Json.JsonSerializerSettings) <0x000af> at Newtonsoft.Json.JsonConvert.DeserializeObject (string) <0x00013> at TaskStressTest.Program/<>c__DisplayClass28.<DoWork>b__26 () <0x0001b> at TaskStressTest.Program.Main (string[]) <0x00134> at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <0xffffffff>
UPDATE 2
Another error has occurred.
* Assertion: should not be reached at sgen-scan-object.h:111 Stacktrace: at <unknown> <0xffffffff> at (wrapper managed-to-native) object.__icall_wrapper_mono_gc_alloc_vector (intptr,intptr,intptr) <0xffffffff> at (wrapper alloc) object.AllocVector (intptr,intptr) <0xffffffff> at System.Array.Resize<T> (T[]&,int) <0x000d3> at System.Collections.Generic.List`1.set_Capacity (int) <0x00053> at System.Collections.Generic.List`1.GrowIfNeeded (int) <0x0006b> at System.Collections.Generic.List`1.Insert (int,T) <0x00047> at Newtonsoft.Json.Linq.JContainer.InsertItem (int,Newtonsoft.Json.Linq.JToken,bool) <0x0022e> at Newtonsoft.Json.Linq.JProperty.InsertItem (int,Newtonsoft.Json.Linq.JToken,bool) <0x00033> at Newtonsoft.Json.Linq.JContainer.AddInternal (int,object,bool) <0x002a6> at Newtonsoft.Json.Linq.JContainer.Add (object) <0x0003f> at Newtonsoft.Json.Linq.JTokenWriter.AddValue (Newtonsoft.Json.Linq.JValue,Newtonsoft.Json.JsonToken) <0x0003c> at Newtonsoft.Json.Linq.JTokenWriter.AddValue (object,Newtonsoft.Json.JsonToken) <0x00043> at Newtonsoft.Json.Linq.JTokenWriter.WriteValue (string) <0x0003f> at Newtonsoft.Json.JsonWriter.WriteToken (Newtonsoft.Json.JsonReader,int,bool,bool) <0x003bd> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateJObject (Newtonsoft.Json.JsonReader) <0x000ab> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader,System.Type,Newtonsoft.Json.Serialization.JsonContract,Newtonsoft.Json.Serialization.JsonProperty,Newtonsoft.Json.Serialization.JsonContainerContract,Newtonsoft.Json.Serialization.JsonProperty,object) <0x000e7> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader,System.Type,Newtonsoft.Json.Serialization.JsonContract,Newtonsoft.Json.Serialization.JsonProperty,Newtonsoft.Json.Serialization.JsonContainerContract,Newtonsoft.Json.Serialization.JsonProperty,object) <0x000bf> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader,System.Type,bool) <0x001c7> at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader,System.Type) <0x00487> at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader,System.Type) <0x0001b> at Newtonsoft.Json.JsonConvert.DeserializeObject (string,System.Type,Newtonsoft.Json.JsonSerializerSettings) <0x000d7> at Newtonsoft.Json.JsonConvert.DeserializeObject (string) <0x00013> at TaskStressTest.Program/<>c__DisplayClass28.<DoWork>b__26 () <0x0001b> at TaskStressTest.Program.Main (string[]) <0x0013c> at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <0xffffffff> Native stacktrace: mono() [0x4ad6a1] /lib/x86_64-linux-gnu/libpthread.so.0(+0xf030) [0x694d59a22030] /lib/x86_64-linux-gnu/libc.so.6(gsignal+0x35) [0x694d596bb475] /lib/x86_64-linux-gnu/libc.so.6(abort+0x180) [0x694d596be6f0] mono() [0x62bcbd] mono() [0x62bdf6] mono() [0x5ef457] mono() [0x5cbc77] mono() [0x5d1576] mono() [0x5d1b08] mono() [0x5e7cba] mono() [0x5e7ecb] [0x4086d270]
UPDATE 3
When using DataContractJsonSerializer instead of Json.NET everything works fine, so there is almost certainly a problem with this library when used in mono (because it works fine on windows without mono) .del>
Ignoring this, this again happened with the DataContractJsonSerializer , this time no useful stack traces were found.
UPDATE 4
I also tried running the application under Mono on Windows (both Json.NET and DataContractJsonSerializer ), I usually get an Access violation reading location exception, but I also saw Assertion: should not be reached at sgen-scan-object.h:111 Application works fine if I do not run it under mono; /
I also tried to get rid of all the features of .NET 4.5 and switch to runtime 4.0 using Task.Factory.StartNew and remove all async / wait, but the problem Task.Factory.StartNew . Now I am trying to use other deserialization libraries.