osrf/capabilities — pull request #70 by wjwwood (2014-05-24 02:22:43+00:00)
Title: Fix stop capability exceptions

This should address #68.

@bit-pirate can you test this branch and see if it addresses the issue for you?

I tried out your scenario and the PID problems have gone away.

I can give a postmortem of sorts. So the problem was that I was calling the internal stop on capabilities which had already been stopped because I didn't consider the fact that stopping one of a capability's dependents (reverse dependency) could also stop other dependents in the list. Therefore I would call stop on the first dependent and then when that was done call it on the second dependent, but the second dependent had already been stopped by the first. So the fix was just to add a check inside the loop to make sure the capability I am about to stop is already stopped. Since calling stop on the capability which had already been stopped raised an error it prevented the rest of the dependents from being shutdown, resulting in the inconsistent state.

I believe this pull request addresses that issue, however, I was still getting this in my testing:

```
[INFO] [WallTime: 1400897287.546466] Request to free usage of capability 'std_capabilities/RGBDSensor' (bond id '26891f9e-e2e8-11e3-801f-902b34db3493')
[ERROR] [WallTime: 1400897287.546847] RuntimeError: No Capability 'std_capabilities/RGBDSensor' in use
[ERROR] [WallTime: 1400897287.547099] Rapp Manager : Failed to stop capabilities. Errors: ["Error occurred while stopping capability 'std_capabilities/RGBDSensor': Exception raised while stopping cap 'std_capabilities/RGBDSensor': service [/capability_server/free_capability] responded with an error: error processing request: No Capability 'std_capabilities/RGBDSensor' in use"]
```

I believe what is happening here is that the app manager is calling free on `std_capabilities/DifferentialMobileBase` and that is causing `std_capabilities/RGBDSensor` to implicitly shutdown:

```
[INFO] [WallTime: 1400897268.640145] Request to free usage of capability 'std_capabilities/DifferentialMobileBase' (bond id '26891f9e-e2e8-11e3-801f-902b34db3493')
[INFO] [WallTime: 1400897268.640661] Capability 'std_capabilities/DifferentialMobileBase' being stopped because it has zero references
[INFO] [WallTime: 1400897268.641024] Capability 'turtlebot_capabilities/robot_state_publisher' is being stopped because it's dependency 'std_capabilities/DifferentialMobileBase' has been stopped.
[INFO] [WallTime: 1400897268.641341] Capability 'turtlebot_capabilities/turtlebot2_bringup' is being stopped because it's dependency 'std_capabilities/RobotStatePublisher' has been stopped.
[INFO] [WallTime: 1400897268.641646] Capability 'turtlebot_capabilities/rgbd_sensor' is being stopped because it's dependency 'turtlebot_capabilities/TurtleBotBringup' has been stopped.
```

Then later the app manager is calling free on the `std_capabilities/RGBDSensor` capability which was already stopped, resulting in this:

```
[INFO] [WallTime: 1400897287.546466] Request to free usage of capability 'std_capabilities/RGBDSensor' (bond id '26891f9e-e2e8-11e3-801f-902b34db3493')
[ERROR] [WallTime: 1400897287.546847] RuntimeError: No Capability 'std_capabilities/RGBDSensor' in use
[ERROR] [WallTime: 1400897287.547099] Rapp Manager : Failed to stop capabilities. Errors: ["Error occurred while stopping capability 'std_capabilities/RGBDSensor': Exception raised while stopping cap 'std_capabilities/RGBDSensor': service [/capability_server/free_capability] responded with an error: error processing request: No Capability 'std_capabilities/RGBDSensor' in use"]
```