Skip to content

Fix ROS 2 node initialization by removing invalid context manager usage#6129

Open
ayush-shirode wants to merge 1 commit into
ros2:rollingfrom
ayush-shirode:patch-1
Open

Fix ROS 2 node initialization by removing invalid context manager usage#6129
ayush-shirode wants to merge 1 commit into
ros2:rollingfrom
ayush-shirode:patch-1

Conversation

@ayush-shirode

Copy link
Copy Markdown

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 <ayushshirode2006@gmail.com>

@fujitatomoya fujitatomoya left a comment

Copy link
Copy Markdown
Collaborator

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

ayush-shirode commented Jan 1, 2026 via email

Copy link
Copy Markdown
Author

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