Index: trunk/src/org/openstreetmap/josm/data/osm/DataSet.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/osm/DataSet.java	(revision 3781)
+++ trunk/src/org/openstreetmap/josm/data/osm/DataSet.java	(revision 3782)
@@ -74,4 +74,7 @@
  * Note that it is not necessary to call beginUpdate/endUpdate for every dataset modification - dataset will get locked
  * automatically.
+ * 
+ * Note that locks cannot be upgraded - if one threads use read lock and and then write lock, dead lock will occur - see #5814 for
+ * sample ticket
  *
  * @author imi
Index: trunk/src/org/openstreetmap/josm/gui/MapStatus.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/MapStatus.java	(revision 3781)
+++ trunk/src/org/openstreetmap/josm/gui/MapStatus.java	(revision 3782)
@@ -173,9 +173,25 @@
                     // the data.
                     DataSet ds = null;
+                    // The popup != null check is required because a left-click
+                    // produces several events as well, which would make this
+                    // variable true. Of course we only want the popup to show
+                    // if the middle mouse button has been pressed in the first
+                    // place
+                    boolean isAtOldPosition = (oldMousePos != null
+                            && oldMousePos.equals(ms.mousePos)
+                            && popup != null);
+                    boolean middleMouseDown = (ms.modifiers & MouseEvent.BUTTON2_DOWN_MASK) != 0;
                     try {
                         ds = mv.getCurrentDataSet();
                         if (ds != null) {
                             // This is not perfect, if current dataset was changed during execution, the lock would be useless
-                            ds.getReadLock().lock();
+                            if(isAtOldPosition && middleMouseDown) {
+                                // Write lock is necessary when selecting in popupCycleSelection
+                                // locks can not be upgraded -> if do read lock here and write lock later (in OsmPrimitive.updateFlags)
+                                // then always occurs deadlock (#5814)
+                                ds.beginUpdate();
+                            } else {
+                                ds.getReadLock().lock();
+                            }
                         }
 
@@ -183,13 +199,4 @@
                         statusBarElementUpdate(ms);
 
-                        // The popup != null check is required because a left-click
-                        // produces several events as well, which would make this
-                        // variable true. Of course we only want the popup to show
-                        // if the middle mouse button has been pressed in the first
-                        // place
-                        boolean isAtOldPosition = (oldMousePos != null
-                                && oldMousePos.equals(ms.mousePos)
-                                && popup != null);
-                        boolean middleMouseDown = (ms.modifiers & MouseEvent.BUTTON2_DOWN_MASK) != 0;
 
                         // Popup Information
@@ -247,5 +254,9 @@
                     } finally {
                         if (ds != null) {
-                            ds.getReadLock().unlock();
+                            if(isAtOldPosition && middleMouseDown) {
+                                ds.endUpdate();
+                            } else {
+                                ds.getReadLock().unlock();
+                            }
                         }
                     }
