diff --git a/og_subgroups.common.inc b/og_subgroups.common.inc
index f228eed..c9cc275 100644
--- a/og_subgroups.common.inc
+++ b/og_subgroups.common.inc
@@ -5,6 +5,8 @@
  * Common functions used in og_subgroups.
  */
 
+define('OG_RECURSION_LIMIT', 50);
+
 /**
  * Get parent groups.
  *
@@ -28,8 +30,12 @@
  * @param $parents
  *   An array used for tracking all parents.
  */
-function og_subgroups_get_parents($entity_type = 'node', $entity, &$paths = array(), $reverse = FALSE, &$parents = array()) {
+function og_subgroups_get_parents($entity_type = 'node', $entity, &$paths = array(), $reverse = FALSE, &$parents = array(), $count = 0) {
   static $forks;
+  if ($count > OG_RECURSION_LIMIT) {
+    watchdog('og_subgroups', "Recursion limit reached in group tree: entity type: %type, id %id", array('%type' => $entity_type, '%id' => $entity), WATCHDOG_CRITICAL);
+    return;
+  }
   $groups = og_get_entity_groups($entity_type, $entity);
   if (!empty($groups)) {
     // We still have parents to record.
@@ -374,7 +380,11 @@ function _og_subgroup_get_user_inherited_groups($account) {
  *
  * @see: _og_subgroup_get_user_inherited_groups().
  */
-function _og_subgroups_get_children_groups($group_type, $group_id, &$children_groups) {
+function _og_subgroups_get_children_groups($group_type, $group_id, &$children_groups, $count = 0) {
+  if ($count > OG_RECURSION_LIMIT) {
+    watchdog('og_subgroups', t('Recursion limit, getting children of group %gid', array('%gid' => $group_id)), WATCHDOG_CRITICAL);
+    return;
+  }
   $children = og_subgroups_get_associated_entities($group_type, $group_id);
   foreach ($children as $children_type => $children_ids) {
     $children_entities = entity_load($children_type, $children_ids);
@@ -382,7 +392,7 @@ function _og_subgroups_get_children_groups($group_type, $group_id, &$children_gr
       if (og_is_group($children_type, $child_entity)) {
         $children_groups[$children_type][$child_id] = $child_entity;
         if (isset($child_entity->{OG_USER_INHERITANCE_FIELD}[LANGUAGE_NONE][0]['value']) && $child_entity->{OG_USER_INHERITANCE_FIELD}[LANGUAGE_NONE][0]['value']) {
-          _og_subgroups_get_children_groups($children_type, $child_id, $children_groups);
+          _og_subgroups_get_children_groups($children_type, $child_id, $children_groups, $count++);
         }
       }
     }
