Opened 10 years ago

Closed 10 years ago

#1544 closed defect (done)

Persistence is not thread-safe

Reported by: cneumuel Owned by: swagner
Priority: high Milestone: HeuristicLab 3.3.5
Component: Persistence Version: 3.3.5
Keywords: Cc:

Description

Serializing multiple (different) objects at the same time may result in the following exception:

System.ArgumentException occurred
  Message=An item with the same key has already been added.
  Source=mscorlib
  StackTrace:
       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
       at HeuristicLab.Persistence.Default.CompositeSerializers.NumberEnumerable2StringSerializer.GetGenericEnumerableInterface(Type type)
  InnerException: 

in HeuristicLab.Persistence.Default.CompositeSerializers.NumberEnumerable2StringSerializer:60.

This happens due to a race condition in the method GetGenericEnumerableInterface.

Here is some code to reproduce the issue:

    private static void TestPersistenceThreadSafe() {
      int n = 20;
      Task[] tasks = new Task[n];
      for (int i = 0; i < n; i++) {
        tasks[i] = Task.Factory.StartNew((idx) => {
          var alg = new GeneticAlgorithm { Engine = new SequentialEngine.SequentialEngine(), Problem = new SingleObjectiveTestFunctionProblem() };
          byte[] data = Serialize(alg);
          Console.WriteLine("Object #{0} serialized", idx);
        }, i);
      }
      Task.WaitAll(tasks);
    }
    public static byte[] Serialize(object obj) {
      MemoryStream memStream = new MemoryStream();
      XmlGenerator.Serialize(obj, memStream);
      byte[] jobByteArray = memStream.ToArray();
      memStream.Dispose();
      return jobByteArray;
    }

This issue is rated with high priority, because it has been identified to be causing problems in HeuristicLab Hive both on Slaves and Clients where parallel serialization is common.

Change History (5)

comment:1 Changed 10 years ago by epitzer

  • Status changed from new to accepted

comment:2 Changed 10 years ago by epitzer

Added additional locks to improve concurrent persistence (r6356)

comment:3 Changed 10 years ago by epitzer

  • Owner changed from epitzer to cneumuel
  • Status changed from accepted to reviewing

comment:4 Changed 10 years ago by cneumuel

  • Owner changed from cneumuel to swagner
  • Status changed from reviewing to readytorelease

comment:5 Changed 10 years ago by swagner

  • Resolution set to done
  • Status changed from readytorelease to closed
  • Version changed from 3.3.4 to 3.3.5
Note: See TracTickets for help on using tickets.