Python demo_nodes_py talker.py contains bug in usage of rlcpy.init

Open Beginner friendly
#808 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
robotics

Research direction

Start with demo_nodes_py/demo_nodes_py/topics/talker.py line 41 and listener.py line 34, then compare their rclpy.init usage with the documented return value. Verify both demo nodes start without the reported context-manager exception and continue operating as expected.

Written by the indexing model from the issue text.

Description

bug
Generated by Generative AI

No

Operating System:

Linux 6.6.87.2-microsoft-standard-WSL2 #1 SMP PREEMPT_DYNAMIC Thu Jun 5 18:30:46 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux

ROS version or commit hash:

jazzy

RMW implementation (if applicable):

rmw_fastrtps_cpp

RMW Configuration (if applicable):

The initialization pattern:

with rclpy.init(args=args):

in https://github.com/ros2/demos/blob/rolling/demo_nodes_py/demo_nodes_py/topics/talker.py (line 41) and https://github.com/ros2/demos/blob/rolling/demo_nodes_py/demo_nodes_py/topics/listener.py (line 34) fails
with following error message:
with rclpy.init(args=args):
TypeError: 'NoneType' object does not support the context manager protocol
which is logical, because rclpy.init return None according to the documentation (https://docs.ros.org/en/iron/p/rclpy/api/init_shutdown.html#rclpy.init)
If one uses the initialization sequence:

    rclpy.init(args=args)
    node = NodeClass() #i.e. Talker or Listener
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()

, then the node functions properly
One could consider as well following sequence:

    try:
        rclpy.init(args=args)
        node = NodeClass() #i.e. Talker or Listener
        try:
            rclpy.spin(node)
        finally:    
            node.destroy_node()
        rclpy.shutdown()
    except (KeyboardInterrupt, ExternalShutdownException):
        pass

which is close in spirit to one used in demo classes, and does not print stack trace on keyboard interrupt

Client library (if applicable):

No response

'ros2 doctor --report' output
ros2 doctor --report
<COPY OUTPUT HERE>
Steps to reproduce issue
  1. Create a package with publisher and subscriber node, using code from https://github.com/ros2/demos/blob/rolling/demo_nodes_py/demo_nodes_py/topics/talker.py as a base
  2. Add entry points to setup.py for newly created nodes
  3. try to get node to run
Expected behavior

No exception when starting a talker or listener node

Actual behavior

The node starts and starts listening to the topic

Additional information

No response

Dominant language
C++
Stars
653
Forks
359
Avg merge
6d 20h
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ros2/demos

All issues in ros2/demos

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.