Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:CARLA simulator Carla Sensor Lifecycle Management

From Leeroopedia
Knowledge Sources
Domains Simulation, Perception, Sensor_Fusion
Last Updated 2026-02-15 00:00 GMT

Overview

Sensor lifecycle management is the discipline of properly tearing down sensors after data collection by stopping their data streams, destroying their server-side actors, and restoring the world to its original configuration.

Description

Every sensor spawned in CARLA consumes server-side resources: GPU memory for camera rendering, CPU cycles for LiDAR ray casting, and network bandwidth for streaming data to the client. Additionally, each sensor's listen() callback runs on a client-side background thread. Failing to properly clean up sensors leads to:

  • Resource leaks: GPU and CPU resources remain allocated on the server, degrading performance in subsequent operations.
  • Dangling callbacks: If a sensor is destroyed while its callback is still active, the callback thread may attempt to process data from a non-existent sensor, causing crashes or undefined behavior.
  • Orphaned actors: Sensors that are not explicitly destroyed persist in the simulation until the server is restarted, accumulating across multiple collection runs.
  • Synchronous mode deadlock: If the world remains in synchronous mode after the collection script exits, other clients (including the manual control script) will hang waiting for tick() calls that never come.

The correct teardown sequence is:

  1. Sensor.stop(): Deregisters the callback and stops the data stream. This ensures no further callback invocations occur.
  2. Sensor.destroy(): Removes the sensor actor from the server, freeing all server-side resources.
  3. World.apply_settings(original_settings): Restores the world to its pre-collection state (typically asynchronous mode).

This sequence must be executed in order, and should be wrapped in a try/finally block to ensure execution even if the collection loop raises an exception.

Usage

Sensor cleanup is performed at the end of every data collection session, or when switching between different sensor configurations. It is also necessary in error-handling paths to prevent resource accumulation from failed collection attempts.

Theoretical Basis

Resource Acquisition Is Initialization (RAII): While Python does not enforce RAII as strictly as C++, the principle applies: resources acquired during sensor setup (server actors, callback threads, GPU buffers) must be explicitly released during teardown. CARLA does not implement Python's context manager protocol (__enter__ / __exit__) for sensors, so manual cleanup is required.

Ordered Teardown: The stop-before-destroy ordering is critical because:

  • stop() signals the client-side streaming infrastructure to cease invoking callbacks. This is a client-local operation that completes quickly.
  • destroy() sends an RPC to the server to remove the actor. If a callback is in-flight when the actor is destroyed, the callback may receive corrupted or partial data.

By stopping first, we guarantee a clean boundary: no callbacks will fire after stop() returns, making destroy() safe.

Exception Safety: Data collection loops may fail due to network errors, server crashes, sensor misconfiguration, or user interruption (Ctrl+C). The try/finally pattern ensures that cleanup code executes regardless of how the loop terminates. This is especially important for restoring asynchronous mode, as leaving the world in synchronous mode without an active client results in a frozen simulation.

Idempotency: Sensor.stop() is idempotent -- calling it on an already-stopped sensor has no effect. Sensor.destroy() returns a boolean indicating success; calling it on an already-destroyed sensor returns False. This makes it safe to call cleanup code unconditionally without checking sensor state.

Batch Destruction: For large sensor suites, CARLA supports client.apply_batch_sync() with carla.command.DestroyActor() commands to destroy multiple actors in a single RPC call, reducing teardown latency. However, stop() must still be called individually on each sensor before the batch destroy.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment