Index: modules/locale/locale.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/locale/locale.module,v
retrieving revision 1.286
diff -u -9 -p -r1.286 locale.module
--- modules/locale/locale.module	7 Mar 2010 18:52:27 -0000	1.286
+++ modules/locale/locale.module	18 Mar 2010 16:49:16 -0000
@@ -630,31 +630,42 @@ function locale($string = NULL, $context
         // also a string-history information for later pruning of the tables.
         db_update('locales_source')
           ->fields(array('version' => VERSION))
           ->condition('lid', $translation->lid)
           ->execute();
         cache_clear_all('locale:', 'cache', TRUE);
       }
     }
     else {
-      // We don't have the source string, cache this as untranslated.
-      db_insert('locales_source')
-        ->fields(array(
-          'location' => request_uri(),
-          'source' => $string,
-          'context' => (string) $context,
-          'textgroup' => 'default',
-          'version' => VERSION,
-        ))
-        ->execute();
+      // We don't have the source string, cache this as untranslated. In order to
+      // avoid duplicates in the table, we acquire a lock and check that the
+      // translation has not been added by a parallel request in the meantime.
+      if (lock_acquire('locales_source')) {
+        $translation_exists = (bool)db_query_range("SELECT 1 FROM {locales_source} WHERE source = :source AND context = :context AND textgroup = 'default'", 0, 1, array(
+          ':source' => $string,
+          ':context' => (string) $context,
+        ))->fetchObject();
+        if (!$translation_exists) {
+          db_insert('locales_source')
+            ->fields(array(
+              'location' => request_uri(),
+              'source' => $string,
+              'context' => (string) $context,
+              'textgroup' => 'default',
+              'version' => VERSION,
+            ))
+            ->execute();
+          // Clear locale cache so this string can be added in a later request.
+          cache_clear_all('locale:', 'cache', TRUE);
+        }
+        lock_release('locales_source');
+      }
       $locale_t[$langcode][$context][$string] = TRUE;
-      // Clear locale cache so this string can be added in a later request.
-      cache_clear_all('locale:', 'cache', TRUE);
     }
   }
 
   return ($locale_t[$langcode][$context][$string] === TRUE ? $string : $locale_t[$langcode][$context][$string]);
 }
 
 /**
  * Reset static variables used by locale().
  */
