carla: Synchronous mode - tick hangs
Intro
I am running a simple script in synchronous mode which does the following:
- spawn an ego vehicle
- in a loop do:
- set velocity
- initiate tick
- wait to receive data
- store it.
Here is the code (i’ve simplified the synchronous mode example):
import carla
import queue
snapshots = queue.Queue()
def push_snapshot(snapshot):
snapshots.put(snapshot)
def get_position(frame_id, vehicle_id):
snapshot = snapshots.get(timeout=2)
assert(frame_id == snapshot.frame)
transform = snapshot.find(vehicle_id).get_transform()
return transform.location.x, transform.location.y, transform.location.z
def main():
sim_time = 0.0
sim_time_end = 20.0
delta_time = 0.05
results = []
client = carla.Client("127.0.0.1", 2000)
world = client.get_world()
# set mode to synchronous
frame_id = world.apply_settings(carla.WorldSettings(True, False, delta_time))
print("Synchronous mode will be applied in frame: {}".format(frame_id))
# spawn car
spectator = world.get_spectator()
blueprint_library = world.get_blueprint_library()
map_ = world.get_map()
vehicles = blueprint_library.filter("vehicle")
bp = vehicles[0]
vehicle = world.spawn_actor(bp, map_.get_spawn_points()[0])
world.tick() # just so that I see it in the simulator
world.on_tick(push_snapshot)
cmd = [carla.command.ApplyVelocity(vehicle.id, carla.Vector3D(y=2.0))] # constant for the minimal example
while sim_time <= sim_time_end:
client.apply_batch_sync(cmd)
print("before tick")
frame_id = world.tick() # on newer versions raises an Exception from which you can't rly recover
print("after tick {}".format(frame_id))
sim_time += 0.05
results.append((sim_time, get_position(frame_id, vehicle.id)))
world.apply_settings(carla.WorldSettings(False, False, 0))
vehicle.destroy()
if __name__ == '__main__':
main()
Problem
The problem happens when running this multiple times, sometimes the tick call will just hang. I originally encountered this with the C++ API (0.9.7). After that, I was able to reproduce it with the PythonAPI on 0.9.7, 0.9.8, 0.9.9 as well.
In case of 0.9.7, the script would just hang forever. With the newer versions, an exception would be thrown because of the recently added tick timeout (#2556). But in my opinion this just hides the root problem.
In my opinion this is really a big shortcoming of Carla and the synchronous mode, and it basically makes the synchronous mode not that usable. If you want to have reproducible results, it is not possible as the frame (corresponding to the “hanging” tick call) would be dropped. The exception being thrown in newer versions due to a timeout is also not useful as you can’t really recover from it.
I’ve already found a few related issues:
- #2556
- #2070 - sadly closed, but I can see that the author and @nsubiron also commented about the same problem. It seems like the issue was forgotten.
- #1803
- #2581
- #2038
So here are my questions:
- Do you agree with me that this is a problem (and can be reproduced)?
- Is anything already planned regarding the fix (as I can see in the linked issue that @nsubiron also suggests getting rid of the cause of the problem instead of applying workarounds?
Additional info
IMPORTANT: The described problem doesn’t occur all the time. You might have to run the script multiple times to see it. I typically run it like:
FOR /L %v IN (1,1,50) DO python tick_hangs.py
Environment
OS: Win 10 Python: 3.7 Carla versions:
- 0.9.7 (locally built from 1f7669e4b308aa8e8f7612dde6666ba6eda14e9f) - tried both the python and C++ API
- 0.9.8 (official package) - tried only the python API
- 0.9.9 (official package) - tried only the python API
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 7
- Comments: 29 (7 by maintainers)
Hi, I have opened a PR fixing this problem (https://github.com/carla-simulator/carla/pull/3394). It seems it was a race condition and a wrong management of a handler, that was queued instead of executed in real time.
ok, I have been able to reproduce using a package a let the script a bit more of time to run. It happens at random, but now I can try to check further. I will tell you what I find, thank you.
@nsubiron Would be really great if you could escalate the priority of this issue among devs as this is a major problem for reinforcement learning research with Carla.