Index: modules/contextual/contextual.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/contextual/contextual.module,v
retrieving revision 1.7
diff -u -p -r1.7 contextual.module
--- modules/contextual/contextual.module	24 Jul 2010 17:28:25 -0000	1.7
+++ modules/contextual/contextual.module	31 Jul 2010 19:06:15 -0000
@@ -85,7 +85,11 @@ function contextual_preprocess(&$variabl
     $element = $variables[$key];
   }
 
-  if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) {
+  // If the element has theme wrappers, we only render the contextual links
+  // when the outermost theme wrapper function is being called. This allows
+  // blocks and other containing elements to properly inherit the contextual
+  // links of their children, without the links being rendered multiple times.
+  if (isset($element) && is_array($element) && !empty($element['#contextual_links']) && (empty($element['#theme_wrappers']) || $hook == end($element['#theme_wrappers']))) {
     $variables['title_suffix']['contextual_links'] = contextual_links_view($element);
     if (!empty($variables['title_suffix']['contextual_links'])) {
       $variables['classes_array'][] = 'contextual-links-region';
