JAL-1115 change implementation to avoid possibility of deadlock
authorJim Procter <jprocter@issues.jalview.org>
Thu, 14 Jul 2016 10:43:53 +0000 (11:43 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Thu, 14 Jul 2016 10:43:53 +0000 (11:43 +0100)
src/jalview/datamodel/Alignment.java

index 0ee0344..d737bd5 100755 (executable)
@@ -1373,10 +1373,6 @@ public class Alignment implements AlignmentI
   @Override
   public void append(AlignmentI toappend)
   {
-    if (toappend == this)
-    {
-      System.err.println("Self append may cause a deadlock.");
-    }
     // TODO JAL-1270 needs test coverage
     // currently tested for use in jalview.gui.SequenceFetcher
     boolean samegap = toappend.getGapCharacter() == getGapCharacter();
@@ -1388,6 +1384,8 @@ public class Alignment implements AlignmentI
             .getFullAlignment().getSequences() : toappend.getSequences();
     if (sqs != null)
     {
+      // avoid self append deadlock by
+      List<SequenceI> toappendsq = new ArrayList<SequenceI>();
       synchronized (sqs)
       {
         for (SequenceI addedsq : sqs)
@@ -1403,9 +1401,13 @@ public class Alignment implements AlignmentI
               }
             }
           }
-          addSequence(addedsq);
+          toappendsq.add(addedsq);
         }
       }
+      for (SequenceI addedsq : toappendsq)
+      {
+        addSequence(addedsq);
+      }
     }
     AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
     for (int a = 0; alan != null && a < alan.length; a++)