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 dead actor accumulation in CDP #1091

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 15 additions & 8 deletions srunner/scenariomanager/carla_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,24 @@ def on_carla_tick():
Callback from CARLA
"""
with CarlaDataProvider._lock:
for actor in CarlaDataProvider._actor_velocity_map:
for actor in CarlaDataProvider._actor_velocity_map.copy():
if actor is not None and actor.is_alive:
CarlaDataProvider._actor_velocity_map[actor] = calculate_velocity(actor)
else:
del CarlaDataProvider._actor_velocity_map[actor]

for actor in CarlaDataProvider._actor_location_map:
for actor in CarlaDataProvider._actor_location_map.copy():
if actor is not None and actor.is_alive:
CarlaDataProvider._actor_location_map[actor] = actor.get_location()
else:
del CarlaDataProvider._actor_location_map[actor]

for actor in CarlaDataProvider._actor_transform_map:
for actor in CarlaDataProvider._actor_transform_map.copy():
if actor is not None and actor.is_alive:
CarlaDataProvider._actor_transform_map[actor] = actor.get_transform()

else:
del CarlaDataProvider._actor_transform_map[actor]

world = CarlaDataProvider._world
if world is None:
print("WARNING: CarlaDataProvider couldn't find the world")
Expand Down Expand Up @@ -1003,11 +1009,12 @@ def remove_actor_by_id(actor_id):
Remove an actor from the pool using its ID
"""
if actor_id in CarlaDataProvider._carla_actor_pool:
CarlaDataProvider._carla_actor_pool[actor_id].destroy()
CarlaDataProvider._carla_actor_pool[actor_id] = None # type: ignore
was_destroyed = CarlaDataProvider._carla_actor_pool[actor_id].destroy()
CarlaDataProvider._carla_actor_pool[actor_id] = None
CarlaDataProvider._carla_actor_pool.pop(actor_id)
else:
print("Trying to remove a non-existing actor id {}".format(actor_id))
return was_destroyed
print("Trying to remove a non-existing actor id {}".format(actor_id))
return None

@staticmethod
def remove_actors_in_surrounding(location, distance):
Expand Down
20 changes: 10 additions & 10 deletions srunner/scenariomanager/scenarioatomics/atomic_behaviors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3471,7 +3471,7 @@ def update(self):
sensor.stop()
sensor.destroy()
self._collision_sensor_list.remove(sensor)
actor.destroy()
CarlaDataProvider.remove_actor_by_id(actor.id)
self._actor_list.remove(actor)

# Spawn new actors if needed
Expand Down Expand Up @@ -3519,7 +3519,7 @@ def terminate(self, new_status):
actor.set_target_velocity(carla.Vector3D(0,0,0))
actor.set_target_angular_velocity(carla.Vector3D(0,0,0))
try:
actor.destroy()
CarlaDataProvider.remove_actor_by_id(actor.id)
except RuntimeError:
pass # Actor was already destroyed

Expand Down Expand Up @@ -3635,7 +3635,7 @@ def update(self):
continue
sink_distance = self._sink_location.distance(location)
if sink_distance < self._sink_dist:
actor.destroy()
CarlaDataProvider.remove_actor_by_id(actor.id)
self._actor_list.remove(actor_data)
else:
actor.apply_control(controller.run_step())
Expand Down Expand Up @@ -3668,7 +3668,7 @@ def terminate(self, new_status):
actor.set_target_velocity(carla.Vector3D(0,0,0))
actor.set_target_angular_velocity(carla.Vector3D(0,0,0))
try:
actor.destroy()
CarlaDataProvider.remove_actor_by_id(actor.id)
except RuntimeError:
pass # Actor was already destroyed

Expand Down Expand Up @@ -3750,7 +3750,7 @@ def update(self):
continue
sink_distance = self._sink_location.distance(location)
if sink_distance < self._sink_dist:
actor.destroy()
CarlaDataProvider.remove_actor_by_id(actor.id)
self._actor_list.remove(actor_data)
else:
actor.apply_control(controller.run_step())
Expand Down Expand Up @@ -3783,7 +3783,7 @@ def terminate(self, new_status):
actor.set_target_velocity(carla.Vector3D(0,0,0))
actor.set_target_angular_velocity(carla.Vector3D(0,0,0))
try:
actor.destroy()
CarlaDataProvider.remove_actor_by_id(actor.id)
except RuntimeError:
pass # Actor was already destroyed

Expand Down Expand Up @@ -3889,7 +3889,7 @@ def update(self):
continue
sink_distance = self._sink_location.distance(location)
if sink_distance < self._sink_dist:
actor.destroy()
CarlaDataProvider.remove_actor_by_id(actor.id)
self._actor_data.remove(actor_data)
else:
actor.apply_control(controller.run_step())
Expand Down Expand Up @@ -3926,7 +3926,7 @@ def terminate(self, new_status):
actor.set_target_velocity(carla.Vector3D(0,0,0))
actor.set_target_angular_velocity(carla.Vector3D(0,0,0))
try:
actor.destroy()
CarlaDataProvider.remove_actor_by_id(actor.id)
except RuntimeError:
pass # Actor was already destroyed

Expand Down Expand Up @@ -4725,7 +4725,7 @@ def update(self):
def _destroy_walker(self, walker, controller):
controller.stop()
controller.destroy()
walker.destroy()
CarlaDataProvider.remove_actor_by_id(walker.id)

def terminate(self, new_status):
"""
Expand Down Expand Up @@ -4805,7 +4805,7 @@ def _destroy_walker(self, walker, controller):
controller.stop()
controller.destroy()
if walker:
walker.destroy()
CarlaDataProvider.remove_actor_by_id(walker.id)

def terminate(self, new_status):
"""
Expand Down
2 changes: 1 addition & 1 deletion srunner/scenarios/background_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2549,7 +2549,7 @@ def _destroy_actor(self, actor):
self._remove_actor_info(actor)
try:
actor.set_autopilot(False, self._tm_port)
actor.destroy()
CarlaDataProvider.remove_actor_by_id(actor.id)
except RuntimeError:
pass

Expand Down
8 changes: 8 additions & 0 deletions srunner/scenarios/basic_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ def _initialize_actors(self, config):

for new_actor in new_actors:
self.other_actors.append(new_actor)

def _destroy_other_actors(self):
"""
Remove all actors upon deletion
"""
for actor in self.other_actors:
if CarlaDataProvider.remove_actor_by_id(actor.id) is None:
actor.destroy()

def _setup_scenario_trigger(self, config):
"""
Expand Down
30 changes: 10 additions & 20 deletions srunner/scenarios/cut_in_with_static_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ def _initialize_actors(self, config):
# Move to the side
side_wp = blocker_wp.get_left_lane() if self._direction == 'left' else blocker_wp.get_right_lane()
if not side_wp:
for actor in self.other_actors:
actor.destroy()
self._destroy_other_actors()
raise ValueError("Couldn't find a proper position for the cut in vehicle")

if i == 1:
Expand All @@ -106,8 +105,7 @@ def _initialize_actors(self, config):
blocker_actor = CarlaDataProvider.request_new_actor(
'vehicle.*', side_wp.transform, 'scenario', attribute_filter=self._attributes)
if not blocker_actor:
for actor in self.other_actors:
actor.destroy()
self._destroy_other_actors()
raise ValueError("Couldn't spawn an actor")
blocker_actor.apply_control(carla.VehicleControl(hand_brake=True))

Expand All @@ -119,8 +117,7 @@ def _initialize_actors(self, config):
# Move to the front
next_wps = blocker_wp.next(self._vehicle_gap)
if not next_wps:
for actor in self.other_actors:
actor.destroy()
self._destroy_other_actors()
raise ValueError("Couldn't find a proper position for the cut in vehicle")
blocker_wp = next_wps[0]

Expand All @@ -132,8 +129,7 @@ def _initialize_actors(self, config):
while dist < self._adversary_end_distance:
next_wps = next_wp.next(step)
if not next_wps:
for actor in self.other_actors:
actor.destroy()
self._destroy_other_actors()
raise ValueError("Couldn't find a proper position for the cut in vehicle")
next_wp = next_wps[0]
self._plan.append([next_wp, RoadOption.STRAIGHT])
Expand All @@ -143,15 +139,13 @@ def _initialize_actors(self, config):
# Spawn the cut in vehicle
side_wp = blocker_wp.get_left_lane() if self._direction == 'left' else blocker_wp.get_right_lane()
if not side_wp:
for actor in self.other_actors:
actor.destroy()
self._destroy_other_actors()
raise ValueError("Couldn't find a proper position for the cut in vehicle")

self._adversary_actor = CarlaDataProvider.request_new_actor(
'vehicle.*', side_wp.transform, 'scenario', attribute_filter=self._attributes)
if not self._adversary_actor:
for actor in self.other_actors:
actor.destroy()
self._destroy_other_actors()
raise ValueError("Couldn't spawn an actor")

self._adversary_actor.set_simulate_physics(False)
Expand All @@ -165,8 +159,7 @@ def _initialize_actors(self, config):
# Move to the front
next_wps = blocker_wp.next(self._vehicle_gap)
if not next_wps:
for actor in self.other_actors:
actor.destroy()
self._destroy_other_actors()
raise ValueError("Couldn't find a proper position for the cut in vehicle")
blocker_wp = next_wps[0]

Expand All @@ -175,16 +168,14 @@ def _initialize_actors(self, config):
# Move to the side
side_wp = blocker_wp.get_left_lane() if self._direction == 'left' else blocker_wp.get_right_lane()
if not side_wp:
for actor in self.other_actors:
actor.destroy()
self._destroy_other_actors()
raise ValueError("Couldn't find a proper position for the cut in vehicle")

# Spawn the actor
blocker_actor = CarlaDataProvider.request_new_actor(
'vehicle.*', side_wp.transform, 'scenario', attribute_filter=self._attributes)
if not blocker_actor:
for actor in self.other_actors:
actor.destroy()
self._destroy_other_actors()
raise ValueError("Couldn't spawn an actor")
blocker_actor.apply_control(carla.VehicleControl(hand_brake=True))

Expand All @@ -196,8 +187,7 @@ def _initialize_actors(self, config):
# Move to the front
next_wps = blocker_wp.next(self._vehicle_gap)
if not next_wps:
for actor in self.other_actors:
actor.destroy()
self._destroy_other_actors()
raise ValueError("Couldn't find a proper position for the cut in vehicle")
blocker_wp = next_wps[0]

Expand Down
2 changes: 1 addition & 1 deletion srunner/scenarios/object_crash_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def __del__(self):
def _replace_walker(self, adversary):
"""As the adversary is probably, replace it with another one"""
type_id = adversary.type_id
adversary.destroy()
CarlaDataProvider.remove_actor_by_id(adversary.id)
spawn_transform = self.ego_vehicles[0].get_transform()
spawn_transform.location.z -= 50
adversary = CarlaDataProvider.request_new_actor(type_id, spawn_transform)
Expand Down
6 changes: 3 additions & 3 deletions srunner/scenarios/object_crash_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def _initialize_actors(self, config):
self._adversary_transform = self._get_sidewalk_transform(walker_wp, offset)
adversary = CarlaDataProvider.request_new_actor('walker.*', self._adversary_transform)
if adversary is None:
blocker.destroy()
CarlaDataProvider.remove_actor_by_id(blocker.id)
self._number_of_attempts -= 1
move_dist = self._retry_dist
print("Failed to spawn an adversary")
Expand Down Expand Up @@ -350,7 +350,7 @@ def __del__(self):
def _replace_walker(self, adversary):
"""As the adversary is probably, replace it with another one"""
type_id = adversary.type_id
adversary.destroy()
CarlaDataProvider.remove_actor_by_id(adversary.id)
spawn_transform = self.ego_vehicles[0].get_transform()
spawn_transform.location.z -= 50
adversary = CarlaDataProvider.request_new_actor(type_id, spawn_transform)
Expand Down Expand Up @@ -557,7 +557,7 @@ def __del__(self):
def _replace_walker(self, walker):
"""As the adversary is probably, replace it with another one"""
type_id = walker.type_id
walker.destroy()
CarlaDataProvider.remove_actor_by_id(walker.id)
spawn_transform = self.ego_vehicles[0].get_transform()
spawn_transform.location.z -= 50
walker = CarlaDataProvider.request_new_actor(type_id, spawn_transform)
Expand Down
2 changes: 1 addition & 1 deletion srunner/scenarios/parking_exit.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _initialize_actors(self, config):
actor_behind = CarlaDataProvider.request_new_actor(
'vehicle.*', behind_points[0].transform, rolename='scenario no lights', attribute_filter=self._bp_attributes)
if actor_behind is None:
actor_front.destroy()
CarlaDataProvider.remove_actor_by_id(actor_front.id)
raise ValueError("Couldn't spawn the vehicle behind the parking point")
actor_behind.apply_control(carla.VehicleControl(hand_brake=True))
self.other_actors.append(actor_behind)
Expand Down
5 changes: 2 additions & 3 deletions srunner/scenarios/pedestrian_crossing.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ def _initialize_actors(self, config):
spawn_transform = self._get_walker_transform(start_wp, walker_data)
walker = CarlaDataProvider.request_new_actor('walker.*', spawn_transform)
if walker is None:
for walker in self.other_actors:
walker.destroy()
self._destroy_other_actors()
raise ValueError("Failed to spawn an adversary")

walker.set_location(spawn_transform.location + carla.Location(z=-200))
Expand Down Expand Up @@ -220,7 +219,7 @@ def __del__(self):
def _replace_walker(self, walker):
"""As the adversary is probably, replace it with another one"""
type_id = walker.type_id
walker.destroy()
CarlaDataProvider.remove_actor_by_id(walker.id)
spawn_transform = self.ego_vehicles[0].get_transform()
spawn_transform.location.z -= 50
walker = CarlaDataProvider.request_new_actor(type_id, spawn_transform)
Expand Down
Loading