Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix SystemStackError with some deep work package trees

Christophe Bliard171bfaec3fc1
diff --git a/app/services/work_packages/schedule_dependency.rb b/app/services/work_packages/schedule_dependency.rb
index 7cbe83bb640..33eb3bd595f 100644
--- a/app/services/work_packages/schedule_dependency.rb
+++ b/app/services/work_packages/schedule_dependency.rb
@@ -93,6 +93,8 @@ class WorkPackages::ScheduleDependency
end
def descendants(work_package)
+ # Avoid using WorkPackage.with_ancestors to save database requests.
+ # All needed data is already loaded.
@descendants ||= {}
@descendants[work_package] ||= begin
children = children_by_parent_id(work_package.id)
@@ -101,15 +103,14 @@ class WorkPackages::ScheduleDependency
end
end
+ # Get relations of type follows for which the given work package is a direct
+ # follower, or an indirect follower (through parent and/or children).
+ #
+ # Used by +Dependency#dependent_ids+ to get work packages that must be
+ # scheduled prior to the given work package.
def follows_relations(work_package)
@follows_relations ||= {}
- @follows_relations[work_package] ||= begin
- line = [work_package] + ancestors(work_package) + descendants(work_package)
- @follows_relations_by_from_id ||= known_follows_relations.group_by(&:from_id)
- @follows_relations_by_from_id
- .fetch_values(*line.map(&:id)) { [] }
- .flatten
- end
+ @follows_relations[work_package] ||= all_direct_and_indirect_follows_relations_for(work_package)
end
private
@@ -117,6 +118,18 @@ class WorkPackages::ScheduleDependency
attr_accessor :known_follows_relations,
:moved_work_packages
+ def all_direct_and_indirect_follows_relations_for(work_package)
+ family = ancestors(work_package) + [work_package] + descendants(work_package)
+ follows_relations_by_follower_id
+ .fetch_values(*family.pluck(:id)) { [] }
+ .flatten
+ .uniq
+ end
+
+ def follows_relations_by_follower_id
+ @follows_relations_by_follower_id ||= known_follows_relations.group_by(&:from_id)
+ end
+
def create_dependencies
moving_work_packages.index_with { |work_package| Dependency.new(work_package, self) }
end
@@ -168,6 +181,7 @@ class WorkPackages::ScheduleDependency
WorkPackage
.with_ancestor(known_work_packages)
.where.not(id: known_work_packages.map(&:id))
+ .distinct
end
# Load all the predecessors of follows relations that are not already loaded.
diff --git a/spec/services/work_packages/set_schedule_service_spec.rb b/spec/services/work_packages/set_schedule_service_spec.rb
index 63236ce4adc..48e693c4d62 100644
--- a/spec/services/work_packages/set_schedule_service_spec.rb
+++ b/spec/services/work_packages/set_schedule_service_spec.rb
@@ -73,14 +73,7 @@ describe WorkPackages::SetScheduleService do
let(:parent_follower1_due_date) { follower1_due_date }
let(:parent_following_work_package1) do
- work_package = create_follower(parent_follower1_start_date,
- parent_follower1_due_date,
- {})
-
- following_work_package1.parent = work_package
- following_work_package1.save
-
- work_package
+ create_parent(following_work_package1)
end
let(:follower_sibling_work_package) do
@@ -109,6 +102,16 @@ describe WorkPackages::SetScheduleService do
work_package
end
+ def create_parent(child, start_date: child.start_date, due_date: child.due_date)
+ create(:work_package,
+ subject: "parent of #{child.subject}",
+ start_date:,
+ due_date:).tap do |parent|
+ child.parent = parent
+ child.save
+ end
+ end
+
def create_child(parent, start_date, due_date)
create(:work_package,
subject: "child of #{parent.subject}",
@@ -836,4 +839,34 @@ describe WorkPackages::SetScheduleService do
end
end
end
+
+ context 'with deep hierarchy of work packages' do
+ before do
+ work_package.due_date = Time.zone.today - 5.days
+ end
+
+ def create_hierarchy(parent, nb_children_by_levels)
+ nb_children, *remaining_levels = nb_children_by_levels
+ children = create_list(:work_package, nb_children, parent:)
+ if remaining_levels.any?
+ children.each do |child|
+ create_hierarchy(child, remaining_levels)
+ end
+ end
+ end
+
+ it 'does not fail with a SystemStackError (regression #43894)' do
+ parent = create(:work_package, start_date: Date.current, due_date: Date.current)
+ hierarchy = [1, 1, 1, 1, 2, 4, 4, 4]
+ create_hierarchy(parent, hierarchy)
+
+ # The bug triggers when moving work package is in the middle of the
+ # hierarchy
+ work_package.parent = parent.children.first.children.first.children.first
+ work_package.save
+
+ expect { instance.call(attributes) }
+ .not_to raise_error
+ end
+ end
end