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 logic that moves goal handles when one expires #360

Merged
merged 1 commit into from
Dec 18, 2018
Merged
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
14 changes: 9 additions & 5 deletions rcl_action/src/rcl_action/action_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,16 @@ rcl_action_expire_goals(
}
goal_time = _goal_info_stamp_to_nanosec(info_ptr);
if ((current_time - goal_time) > timeout) {
// Stop tracking goal handle
// Fill in any gaps left in the array with pointers from the end
// Deallocate space used to store pointer to goal handle
allocator.deallocate(action_server->impl->goal_handles[i], allocator.state);
action_server->impl->goal_handles[i] = NULL;
// Move all pointers after backwards one to fill the gap
for (size_t post_i = i; (post_i + 1) < num_goal_handles; ++post_i) {
action_server->impl->goal_handles[post_i] = action_server->impl->goal_handles[post_i + 1];
}
// decrement i to check the same index again now that it has a new goal handle
--i;
--num_goal_handles;
action_server->impl->goal_handles[i] = action_server->impl->goal_handles[num_goal_handles];
allocator.deallocate(action_server->impl->goal_handles[num_goal_handles], allocator.state);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the logic also be fixed if this deallocate was for index i and moved one line up?
For sure the --i; is needed.

allocator.deallocate(action_server->impl->goal_handles[i], allocator.state);
action_server->impl->goal_handles[i] = action_server->impl->goal_handles[num_goal_handles];
action_server->impl->goal_handles[num_goal_handles] = NULL;
// decrement i to check the same index again now that it has a new goal handle
--i;

Just want to make sure I understand the bug :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be an improvement, though it would still have a bug. All goal handles need to be moved to shrink the array, but this shrinking logic is doing it one at a time in a block that only executes if the goal handle at i has expired. An active goal at the end of the array would be lost when the array shrinks because this bit wouldn't be reached. If there were two goals that expired at once then goal handles after the second expired one would only be moved back one place when they need to be moved back two places.

This PR adds a for-loop to make sure all goals after the expired one are moved.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to me, thanks for the explanation!

action_server->impl->goal_handles[num_goal_handles] = NULL;
++num_goals_expired;
}
}
Expand Down