How to return the result after a action server goal has been cancelled

In the below code , when a cancel request has been called the server just breaks out of the for loop and doesn’t return result to action_server/result topic or client . Is there a way to return the result ?

def goal_callback(self, goal):
    # this callback is called when the action server is called.
    # this is the function that computes the Fibonacci sequence
    # and returns the sequence to the node that called the action server
    # helper variables
    r = rospy.Rate(1)
    success = True
    # append the seeds for the fibonacci sequence
    self._feedback.sequence = []
    # publish info to the console for the user
    rospy.loginfo('"fibonacci_as": Executing, creating fibonacci sequence of order %i with seeds %i, %i' % ( goal.order, self._feedback.sequence[0], self._feedback.sequence[1]))
    # starts calculating the Fibonacci sequence
    fibonacciOrder = goal.order
    for i in range(1, fibonacciOrder):
      # check that preempt (cancelation) has not been requested by the action client
      if self._as.is_preempt_requested():
        rospy.loginfo('The goal has been cancelled/preempted')
        # the following line, sets the client in preempted state (goal cancelled)
        success = False
        # we end the calculation of the Fibonacci sequence
      # builds the next feedback msg to be sent
      self._feedback.sequence.append(self._feedback.sequence[i] + self._feedback.sequence[i-1])
      # publish the feedback
      # the sequence is computed at 1 Hz frequency
    # at this point, either the goal has been achieved (success==true)
    # or the client preempted the goal (success==false)
    # If success, then we publish the final result
    # If not success, we do not publish anything in the result
    if success:
      self._result.sequence = self._feedback.sequence
      rospy.loginfo('Succeeded calculating the Fibonacci of order %i' % fibonacciOrder )


Returning an action result message signals the end of tracking a goal, so if the request is cancelled, you won’t get a result. What you can do is simply print a message, but not the result since it is not generated if it is cancelled.

You can checkout this link for more info, in section 3.2.5:

Thanks @roalgoal for replying and clarifying about cancelling Action Goal. This question was actually relevent to the rosject Part-III in ROSBasicsIn5Days (Python) , there we need to stop recording the odometey data once the turtlebot stops ( if wall is closer < 0.2 m) and return the result to the client . And my approach was to cancel the goal from within a Action Client python file when the forwad_range < 0.2 m (detected by subscribing to /scan topic from a node in that file and the Square move service server file only) . I guess ,i have to print the result .