diff --git a/python/tools/dht/tests.py b/python/tools/dht/tests.py
index c2d016dbc6fb09d86215ed6260fe982a8e59f0a0..944a66626d95e3010b29fda1fe2df6978c683da9 100644
--- a/python/tools/dht/tests.py
+++ b/python/tools/dht/tests.py
@@ -308,15 +308,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__()