Skip to content

Conversation

@ayush-shirode
Copy link

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

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

Signed-off-by: ayush-shirode <[email protected]>
Copy link
Collaborator

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ayush-shirode thanks for creating PR.

can you share the exact stack that generates the exception from this code in your local environment?
i am not sure if there is a problem that you are trying to explain here, because the same codes are used in else where, e.g https://github.com/ros2/examples/blob/rolling/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/publisher_local_function.py

@fujitatomoya fujitatomoya added the more-information-needed Further information is required label Jan 1, 2026
@ayush-shirode
Copy link
Author

ayush-shirode commented Jan 1, 2026 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

more-information-needed Further information is required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants