scala / pickling

Fast, customizable, boilerplate-free pickling support for Scala
lampwww.epfl.ch/~hmiller/pickling
BSD 3-Clause "New" or "Revised" License
831 stars 79 forks source link

Pickling - possible race condition? #40

Open tomasherman opened 11 years ago

tomasherman commented 11 years ago

Hey guys,

i have following problem:

i am trying to pickle this case class

trait GuiMessage
case class SkillMatrix(operators: Seq[String], skills: Seq[String], enabledSkills: Seq[(String,String)]) extends GuiMessage

The problem is that when i try to pickle this class from two different threads at the same time, i get following:

{x:Any => val pickled = x.asInstanceOf[SkillMatrix].pickle.value; println(s"pickling $x into $pickled");pickled}) //this function is called from two different threads
pickling SkillMatrix(List(1234, 4312),List(skill1, skill2),List((1234,skill1), (1234,skill2), (4312,skill1))) into {
  "tpe": "com.spinoco.horus.message.GuiMessage.SkillMessage.SkillMatrix",
  "operators": {
    "tpe": "scala.collection.Seq[java.lang.String]",
    "elems": [
      "1234",
      "4312"
    ]
  },
  "skills": {
    "tpe": "scala.collection.Seq[java.lang.String]",
    "elems": [
      "skill1",
      "skill2"
    ]
  },
  "enabledSkills": {
    "tpe": "scala.collection.Seq[scala.Tuple2[java.lang.String,java.lang.String]]",
    "elems": [
      { "$ref": 2 },
      {
      "tpe": "scala.Tuple2[java.lang.String,java.lang.String]",
      "a": "1234",
      "b": "skill2"
    },
      { "$ref": 4 }
    ]
  }
}
pickling SkillMatrix(List(1234, 4312),List(skill1, skill2),List((1234,skill1),(1234,skill2),(4312,skill1))) into {
  "tpe": "com.spinoco.horus.message.GuiMessage.SkillMessage.SkillMatrix",
  "operators": {
    "tpe": "scala.collection.Seq[java.lang.String]",
    "elems": [
      "1234",
      "4312"
    ]
  },
  "skills": {
    "tpe": "scala.collection.Seq[java.lang.String]",
    "elems": [
      "skill1",
      "skill2"
    ]
  },
  "enabledSkills": {
    "tpe": "scala.collection.Seq[scala.Tuple2[java.lang.String,java.lang.String]]",
    "elems": [
      {
      "tpe": "scala.Tuple2[java.lang.String,java.lang.String]",
      "a": "1234",
      "b": "skill1"
    },
      { "$ref": 3 },
      {
      "tpe": "scala.Tuple2[java.lang.String,java.lang.String]",
      "a": "4312",
      "b": "skill1"
    }
    ]
  }
}

as you can see, there are some really odd "$ref"s that i don't care for. Am i doing something wrong or is that error on your end? To me, it looks like some kind of race condition when pickling same object in two places at the same time.

Thanks

jsuereth commented 9 years ago

The "$ref" bits are expected. Pickling is trying to share the previous values, rather than re-serialize them.

The question is, what version of pickling was this? Could you try again with master and see if it's still an issue?