Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(start/goal_planner): fix multi thread memory crash #6322

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <autoware_auto_planning_msgs/msg/path_with_lane_id.hpp>
#include <autoware_auto_vehicle_msgs/msg/hazard_lights_command.hpp>

#include <atomic>
#include <deque>
#include <limits>
#include <memory>
Expand Down Expand Up @@ -290,6 +291,35 @@
std::unordered_map<std::string, std::shared_ptr<ObjectsOfInterestMarkerInterface>> &
objects_of_interest_marker_interface_ptr_map);

~GoalPlannerModule()
{
if (lane_parking_timer_) {
lane_parking_timer_->cancel();

Check warning on line 297 in planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp#L294-L297

Added lines #L294 - L297 were not covered by tests
}
if (freespace_parking_timer_) {
freespace_parking_timer_->cancel();

Check warning on line 300 in planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp#L299-L300

Added lines #L299 - L300 were not covered by tests
}

while (is_lane_parking_cb_running_.load() || is_freespace_parking_cb_running_.load()) {
const std::string running_callbacks = std::invoke([&]() {
if (is_lane_parking_cb_running_ && is_freespace_parking_cb_running_) {

Check warning on line 305 in planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp#L303-L305

Added lines #L303 - L305 were not covered by tests
return "lane parking and freespace parking";
}
if (is_lane_parking_cb_running_) {
return "lane parking";

Check warning on line 309 in planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp#L308-L309

Added lines #L308 - L309 were not covered by tests
}
return "freespace parking";
});
RCLCPP_INFO_THROTTLE(

Check warning on line 313 in planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp#L312-L313

Added lines #L312 - L313 were not covered by tests
getLogger(), *clock_, 1000, "Waiting for %s callback to finish...",
running_callbacks.c_str());
std::this_thread::sleep_for(std::chrono::milliseconds(10));

Check warning on line 316 in planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp#L316

Added line #L316 was not covered by tests
}

RCLCPP_INFO_THROTTLE(

Check warning on line 319 in planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp#L319

Added line #L319 was not covered by tests
getLogger(), *clock_, 1000, "lane parking and freespace parking callbacks finished");
}

Check warning on line 321 in planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_goal_planner_module/include/behavior_path_goal_planner_module/goal_planner_module.hpp#L321

Added line #L321 was not covered by tests

void updateModuleParams(const std::any & parameters) override
{
parameters_ = std::any_cast<std::shared_ptr<GoalPlannerParameters>>(parameters);
Expand Down Expand Up @@ -330,6 +360,18 @@
CandidateOutput planCandidate() const override { return CandidateOutput{}; }

private:
// Flag class for managing whether a certain callback is running in multi-threading
class ScopedFlag
{
public:
explicit ScopedFlag(std::atomic<bool> & flag) : flag_(flag) { flag_.store(true); }

~ScopedFlag() { flag_.store(false); }

private:
std::atomic<bool> & flag_;
};

/*
* state transitions and plan function used in each state
*
Expand Down Expand Up @@ -403,10 +445,12 @@
// pre-generate lane parking paths in a separate thread
rclcpp::TimerBase::SharedPtr lane_parking_timer_;
rclcpp::CallbackGroup::SharedPtr lane_parking_timer_cb_group_;
std::atomic<bool> is_lane_parking_cb_running_;

// generate freespace parking paths in a separate thread
rclcpp::TimerBase::SharedPtr freespace_parking_timer_;
rclcpp::CallbackGroup::SharedPtr freespace_parking_timer_cb_group_;
std::atomic<bool> is_freespace_parking_cb_running_;

// debug
mutable GoalPlannerDebugData debug_data_;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Tier IV, Inc.

Check notice on line 1 in planning/behavior_path_goal_planner_module/src/goal_planner_module.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Lines of Code in a Single File

The lines of code increases from 1588 to 1590, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -62,6 +62,8 @@
parameters_{parameters},
vehicle_info_{vehicle_info_util::VehicleInfoUtil(node).getVehicleInfo()},
thread_safe_data_{mutex_, clock_},
is_lane_parking_cb_running_{false},
is_freespace_parking_cb_running_{false},
debug_stop_pose_with_info_{&stop_pose_}
{
LaneDepartureChecker lane_departure_checker{};
Expand Down Expand Up @@ -171,6 +173,8 @@
// generate pull over candidate paths
void GoalPlannerModule::onTimer()
{
const ScopedFlag flag(is_lane_parking_cb_running_);

Check notice on line 177 in planning/behavior_path_goal_planner_module/src/goal_planner_module.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Complex Method

GoalPlannerModule::onTimer already has high cyclomatic complexity, and now it increases in Lines of Code from 95 to 96. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
if (getCurrentStatus() == ModuleStatus::IDLE) {
return;
}
Expand Down Expand Up @@ -287,6 +291,8 @@

void GoalPlannerModule::onFreespaceParkingTimer()
{
const ScopedFlag flag(is_freespace_parking_cb_running_);

if (getCurrentStatus() == ModuleStatus::IDLE) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include <tf2/utils.h>

#include <atomic>
#include <deque>
#include <memory>
#include <string>
Expand Down Expand Up @@ -83,6 +84,21 @@
std::unordered_map<std::string, std::shared_ptr<ObjectsOfInterestMarkerInterface>> &
objects_of_interest_marker_interface_ptr_map);

~StartPlannerModule()
{
if (freespace_planner_timer_) {
freespace_planner_timer_->cancel();

Check warning on line 90 in planning/behavior_path_start_planner_module/include/behavior_path_start_planner_module/start_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_start_planner_module/include/behavior_path_start_planner_module/start_planner_module.hpp#L87-L90

Added lines #L87 - L90 were not covered by tests
}

while (is_freespace_planner_cb_running_.load()) {
RCLCPP_INFO_THROTTLE(

Check warning on line 94 in planning/behavior_path_start_planner_module/include/behavior_path_start_planner_module/start_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_start_planner_module/include/behavior_path_start_planner_module/start_planner_module.hpp#L93-L94

Added lines #L93 - L94 were not covered by tests
getLogger(), *clock_, 1000, "Waiting for freespace planner callback to finish...");
std::this_thread::sleep_for(std::chrono::milliseconds(100));

Check warning on line 96 in planning/behavior_path_start_planner_module/include/behavior_path_start_planner_module/start_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_start_planner_module/include/behavior_path_start_planner_module/start_planner_module.hpp#L96

Added line #L96 was not covered by tests
}

RCLCPP_INFO_THROTTLE(getLogger(), *clock_, 1000, "freespace planner callback finished");
}

Check warning on line 100 in planning/behavior_path_start_planner_module/include/behavior_path_start_planner_module/start_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_start_planner_module/include/behavior_path_start_planner_module/start_planner_module.hpp#L99-L100

Added lines #L99 - L100 were not covered by tests

void updateModuleParams(const std::any & parameters) override
{
parameters_ = std::any_cast<std::shared_ptr<StartPlannerParameters>>(parameters);
Expand Down Expand Up @@ -135,6 +151,18 @@
bool isFreespacePlanning() const { return status_.planner_type == PlannerType::FREESPACE; }

private:
// Flag class for managing whether a certain callback is running in multi-threading
class ScopedFlag
{
public:
explicit ScopedFlag(std::atomic<bool> & flag) : flag_(flag) { flag_.store(true); }

~ScopedFlag() { flag_.store(false); }

private:
std::atomic<bool> & flag_;
};

bool canTransitSuccessState() override;

bool canTransitFailureState() override { return false; }
Expand Down Expand Up @@ -202,6 +230,8 @@
std::unique_ptr<PullOutPlannerBase> freespace_planner_;
rclcpp::TimerBase::SharedPtr freespace_planner_timer_;
rclcpp::CallbackGroup::SharedPtr freespace_planner_timer_cb_group_;
std::atomic<bool> is_freespace_planner_cb_running_;

// TODO(kosuke55)
// Currently, we only do lock when updating a member of status_.
// However, we need to ensure that the value does not change when referring to it.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 TIER IV, Inc.

Check notice on line 1 in planning/behavior_path_start_planner_module/src/start_planner_module.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Lines of Code in a Single File

The lines of code increases from 1283 to 1284, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,7 +57,8 @@
objects_of_interest_marker_interface_ptr_map)
: SceneModuleInterface{name, node, rtc_interface_ptr_map, objects_of_interest_marker_interface_ptr_map}, // NOLINT
parameters_{parameters},
vehicle_info_{vehicle_info_util::VehicleInfoUtil(node).getVehicleInfo()}
vehicle_info_{vehicle_info_util::VehicleInfoUtil(node).getVehicleInfo()},
is_freespace_planner_cb_running_{false}

Check warning on line 61 in planning/behavior_path_start_planner_module/src/start_planner_module.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_start_planner_module/src/start_planner_module.cpp#L60-L61

Added lines #L60 - L61 were not covered by tests
{
lane_departure_checker_ = std::make_shared<LaneDepartureChecker>();
lane_departure_checker_->setVehicleInfo(vehicle_info_);
Expand Down Expand Up @@ -88,6 +89,8 @@

void StartPlannerModule::onFreespacePlannerTimer()
{
const ScopedFlag flag(is_freespace_planner_cb_running_);

if (getCurrentStatus() == ModuleStatus::IDLE) {
return;
}
Expand Down
Loading