diff --git a/python/tools/dht/tests.py b/python/tools/dht/tests.py
index 8c63fc248c592ca05f37882b7c700f963f1a3ce0..2c65563e92da824c44a11aed7188d3679f6870b7 100644
--- a/python/tools/dht/tests.py
+++ b/python/tools/dht/tests.py
@@ -277,15 +277,15 @@ class PhtTest(FeatureTest):
         # Index all entries.
         for key in keys:
             PhtTest.key = key
-            pht.insert(key, IndexValue(random_hash()), PhtTest.insertDoneCb)
             with FeatureTest.lock:
+                pht.insert(key, IndexValue(random_hash()), PhtTest.insertDoneCb)
                 FeatureTest.lock.wait()
 
         # Recover entries now that the trie is complete.
         for key in keys:
             PhtTest.key = key
-            pht.lookup(key, PhtTest.lookupCb, PhtTest.lookupDoneCb)
             with FeatureTest.lock:
+                pht.lookup(key, PhtTest.lookupCb, PhtTest.lookupDoneCb)
                 FeatureTest.lock.wait()
 
             all_entries[PhtTest.prefix] = [e.__str__()