diff --git a/modules/comment/comment.module b/modules/comment/comment.module
index 429c3b0..68f1f38 100644
--- a/modules/comment/comment.module
+++ b/modules/comment/comment.module
@@ -1493,47 +1493,55 @@ function comment_save($comment) {
       if (!empty($comment->thread)) {
         // Allow calling code to set thread itself.
         $thread = $comment->thread;
-      }
-      elseif ($comment->pid == 0) {
-        // This is a comment with no parent comment (depth 0): we start
-        // by retrieving the maximum thread level.
-        $max = db_query('SELECT MAX(thread) FROM {comment} WHERE nid = :nid', array(':nid' => $comment->nid))->fetchField();
-        // Strip the "/" from the end of the thread.
-        $max = rtrim($max, '/');
-        // We need to get the value at the correct depth.
-        $parts = explode('.', $max);
-        $firstsegment = $parts[0];
-        // Finally, build the thread field for this new comment.
-        $thread = int2vancode(vancode2int($firstsegment) + 1) . '/';
+        $need_lock = FALSE;
       }
       else {
-        // This is a comment with a parent comment, so increase the part of the
-        // thread value at the proper depth.
-
-        // Get the parent comment:
-        $parent = comment_load($comment->pid);
-        // Strip the "/" from the end of the parent thread.
-        $parent->thread = (string) rtrim((string) $parent->thread, '/');
-        // Get the max value in *this* thread.
-        $max = db_query("SELECT MAX(thread) FROM {comment} WHERE thread LIKE :thread AND nid = :nid", array(
-          ':thread' => $parent->thread . '.%',
-          ':nid' => $comment->nid,
-        ))->fetchField();
-
-        if ($max == '') {
-          // First child of this parent.
-          $thread = $parent->thread . '.' . int2vancode(0) . '/';
-        }
-        else {
-          // Strip the "/" at the end of the thread.
+        if ($comment->pid == 0) {
+          // This is a comment with no parent comment (depth 0): we start
+          // by retrieving the maximum thread level.
+          $max = db_query('SELECT MAX(thread) FROM {comment} WHERE nid = :nid', array(':nid' => $comment->nid))->fetchField();
+          // Strip the "/" from the end of the thread.
           $max = rtrim($max, '/');
-          // Get the value at the correct depth.
+          // We need to get the value at the correct depth.
           $parts = explode('.', $max);
-          $parent_depth = count(explode('.', $parent->thread));
-          $last = $parts[$parent_depth];
-          // Finally, build the thread field for this new comment.
-          $thread = $parent->thread . '.' . int2vancode(vancode2int($last) + 1) . '/';
+          $n = vancode2int($parts[0]);
+          $prefix = '';
+        }
+        else {
+          // This is a comment with a parent comment, so increase the part of the
+          // thread value at the proper depth.
+
+          // Get the parent comment:
+          $parent = comment_load($comment->pid);
+          // Strip the "/" from the end of the parent thread.
+          $parent->thread = (string) rtrim((string) $parent->thread, '/');
+          // Get the max value in *this* thread.
+          $max = db_query("SELECT MAX(thread) FROM {comment} WHERE thread LIKE :thread AND nid = :nid", array(
+            ':thread' => $parent->thread . '.%',
+            ':nid' => $comment->nid,
+          ))->fetchField();
+          $prefix = $parent->thread . '.';
+
+          if ($max == '') {
+            // First child of this parent.
+            $n = -1;
+          }
+          else {
+            // Strip the "/" at the end of the thread.
+            $max = rtrim($max, '/');
+            // Get the value at the correct depth.
+            $parts = explode('.', $max);
+            $parent_depth = count(explode('.', $parent->thread));
+            $n = vancode2int($parts[$parent_depth]);
+          }
         }
+        $need_lock = TRUE;
+        // We need a lopp here to avoid race conditions: if another process
+        // grabbed the same thread, just move to the next integer.
+        do {
+          // Finally, build the thread field for this new comment.
+          $thread = $prefix . int2vancode(++$n) . '/';
+        } while (!lock_acquire("comment:$comment->nid:$thread"));
       }
 
       if (empty($comment->created)) {
@@ -1558,6 +1566,9 @@ function comment_save($comment) {
       $comment->hostname = ip_address();
 
       drupal_write_record('comment', $comment);
+      if ($need_lock) {
+        lock_release("comment:$comment->nid:$thread");
+      }
 
       // Ignore slave server temporarily to give time for the
       // created comment to be propagated to the slave.
