diff --git a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php
index c005cb0..b3fb565 100644
--- a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php
+++ b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php
@@ -137,8 +137,9 @@ protected function preSave(EntityInterface $comment) {
         // has the lock, just move to the next integer.
         do {
           $thread = $prefix . comment_int_to_alphadecimal(++$n) . '/';
-        } while (!lock()->acquire("comment:{$comment->nid->target_id}:$thread"));
-        $this->threadLock = $thread;
+          $lock_name = "comment:{$comment->nid->target_id}:$thread";
+        } while (!lock()->acquire($lock_name));
+        $this->threadLock = $lock_name;
       }
       if (empty($comment->created->value)) {
         $comment->created->value = REQUEST_TIME;
diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php
index 5585650..f459248 100644
--- a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php
+++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php
@@ -150,6 +150,12 @@ function setEnvironment(array $info) {
           'comment_body' => array(LANGUAGE_NOT_SPECIFIED => array($this->randomName())),
         ));
         comment_save($comment);
+        // Make sure the lock cleared.
+        $subject = $this->randomName();
+        $comment->subject->value = $subject;
+        $comment->save();
+        $comment = entity_load('comment', $comment->id());
+        $this->assertEqual($comment->subject->value, $subject);
         $this->comment = $comment;
 
         // comment_num_new() relies on history_read(), so ensure that no one has
