Fix ROS 2 node initialization by removing invalid context manager usage #6129
+19
−17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes an incorrect usage of rclpy.init() as a context manager in the static TF broadcaster node. The previous implementation used with rclpy.init():, which is not supported in ROS 2 Python and caused a runtime AttributeError: enter.
Problem
rclpy.init() does not implement Python’s context manager protocol (enter / exit)
Using it inside a with block causes the node to crash before spinning
This prevented the static transform from being published on /tf_static
Solution
Replaced with rclpy.init(): with the correct ROS 2 lifecycle pattern:
Explicit rclpy.init()
Explicit rclpy.shutdown() in a finally block
Preserved existing argument validation and exception handling
Why this change is necessary
ROS 2 Python initialization is a process-level operation, not a scoped resource. The explicit init/shutdown pattern is the officially supported and recommended approach and ensures predictable node lifecycle management.
Impact
Fixes runtime crash
Allows static TF broadcaster to start correctly
Aligns code with ROS 2 Python best practices
Description
Fixes # (issue)
Did you use Generative AI?
Additional Information