Index: /trunk/src/VBox/Main/src-server/MediumImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/src-server/MediumImpl.cpp	(revision 92463)
+++ /trunk/src/VBox/Main/src-server/MediumImpl.cpp	(revision 92464)
@@ -1839,4 +1839,24 @@
     AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
 
+    /* Wait for a concurrently running Medium::i_queryInfo to complete. */
+    while (m->queryInfoRunning)
+    {
+        mlock.release();
+        autoCaller.release();
+        treeLock.release();
+        /* Must not hold the media tree lock, as Medium::i_queryInfo needs
+         * this lock and thus we would run into a deadlock here. */
+        Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
+        /* must not hold the object lock now */
+        Assert(!isWriteLockOnCurrentThread());
+        {
+            AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
+        }
+        treeLock.acquire();
+        autoCaller.add();
+        if (FAILED(autoCaller.rc())) return autoCaller.rc();
+        mlock.acquire();
+    }
+
     switch (m->state)
     {
@@ -2111,4 +2131,22 @@
     AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
 
+    /* Wait for a concurrently running Medium::i_queryInfo to complete. */
+    if (m->queryInfoRunning)
+    {
+        /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
+         * lock and thus we would run into a deadlock here. */
+        Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
+        while (m->queryInfoRunning)
+        {
+            alock.release();
+            /* must not hold the object lock now */
+            Assert(!isWriteLockOnCurrentThread());
+            {
+                AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
+            }
+            alock.acquire();
+        }
+    }
+
     switch (m->state)
     {
@@ -2498,4 +2536,22 @@
 {
     AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
+
+    /* Wait for a concurrently running Medium::i_queryInfo to complete. */
+    if (m->queryInfoRunning)
+    {
+        /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
+         * lock and thus we would run into a deadlock here. */
+        Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
+        while (m->queryInfoRunning)
+        {
+            mlock.release();
+            /* must not hold the object lock now */
+            Assert(!isWriteLockOnCurrentThread());
+            {
+                AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
+            }
+            mlock.acquire();
+        }
+    }
 
     switch (m->state)
@@ -5230,4 +5286,22 @@
     if (FAILED(autoCaller.rc())) return autoCaller.rc();
 
+    /* Wait for a concurrently running Medium::i_queryInfo to complete. */
+    while (m->queryInfoRunning)
+    {
+        autoCaller.release();
+        multilock.release();
+        /* Must not hold the media tree lock, as Medium::i_queryInfo needs
+         * this lock and thus we would run into a deadlock here. */
+        Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
+        /* must not hold the object lock now */
+        Assert(!isWriteLockOnCurrentThread());
+        {
+            AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
+        }
+        multilock.acquire();
+        autoCaller.add();
+        if (FAILED(autoCaller.rc())) return autoCaller.rc();
+    }
+
     LogFlowFunc(("ENTER for %s\n", i_getLocationFull().c_str()));
 
@@ -5318,5 +5392,5 @@
  */
 HRESULT Medium::i_deleteStorage(ComObjPtr<Progress> *aProgress,
-                              bool aWait, bool aNotify)
+                                bool aWait, bool aNotify)
 {
     AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
@@ -5347,24 +5421,21 @@
         /** @todo r=klaus would be great if this could be moved to the async
          * part of the operation as it can take quite a while */
-        if (m->queryInfoRunning)
-        {
-            while (m->queryInfoRunning)
-            {
-                alock.release();
-                autoCaller.release();
-                treelock.release();
-                /* Must not hold the media tree lock or the object lock, as
-                 * Medium::i_queryInfo needs this lock and thus we would run
-                 * into a deadlock here. */
-                Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
-                Assert(!isWriteLockOnCurrentThread());
-                {
-                    AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
-                }
-                treelock.acquire();
-                autoCaller.add();
-                AssertComRCThrowRC(autoCaller.rc());
-                alock.acquire();
-            }
+        while (m->queryInfoRunning)
+        {
+            alock.release();
+            autoCaller.release();
+            treelock.release();
+            /* Must not hold the media tree lock or the object lock, as
+             * Medium::i_queryInfo needs this lock and thus we would run
+             * into a deadlock here. */
+            Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
+            Assert(!isWriteLockOnCurrentThread());
+            {
+                AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
+            }
+            treelock.acquire();
+            autoCaller.add();
+            AssertComRCThrowRC(autoCaller.rc());
+            alock.acquire();
         }
 
