question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leak in client when using python wrapper to send request to service

See original GitHub issue

I found this problem in latest Galactic release, it is simple to reproduce, write a simple service(C++) and a client (Python), memory leak will definitly happen. It will not happen when using C++ in client.

  1. Data struct:
int32 seq
uint64 time
byte[1048576] input_tensor
---
int32 seq
uint64 time
byte[1048576] output_tensor

  1. Client side code:
import sys

from my_struct.srv import InferService
import rclpy
from rclpy.node import Node

import time


class MinimalClientAsync(Node):

    def __init__(self):
        super().__init__('minimal_client_async')
        self.cli = self.create_client(InferService, 'infer')
        while not self.cli.wait_for_service(timeout_sec=1.0):
            self.get_logger().info('service not available, waiting again...')
        self.req = InferService.Request()

    def send_request(self):
        self.req.seq = 2000
        self.req.time = 200
        self.future = self.cli.call_async(self.req)


def main(args=None):
    rclpy.init(args=args)

    minimal_client = MinimalClientAsync()
    for _ in range(100000):
        minimal_client.send_request()

        while rclpy.ok():
            rclpy.spin_once(minimal_client)
            if minimal_client.future.done():
                try:
                    response = minimal_client.future.result()
                except Exception as e:
                    minimal_client.get_logger().info(
                        'Service call failed %r' % (e,))
                else:
                    minimal_client.get_logger().info(
                        'Result of inference: resp seq %d' % (response.seq))
                break
            #time.sleep(0.5)


    minimal_client.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

You can write a simple service code, just receive the request and do nothing and send a response to client.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
fujitatomoyacommented, Sep 21, 2021

problem confirmed, in the process space there is a lot of heap memory area mapped. as long as client/service running, virtual/physical memory increases. i created the reproducible sample program, https://github.com/fujitatomoya/ros2_test_prover/tree/master/prover_rclpy.

under colcon envirnoment,

colcon build --symlink-install --packages-select prover_interfaces prover_rclpy
source install/local_setup.bash
ros2 run prover_rclpy rclpy_server_822
ros2 run prover_rclpy rclpy_client_822

CC: @Barry-Xu-2018 @iuhilnehc-ynos could you take a look if you have time? i guess this is memory leak, if i am not mistaken…

2reactions
iuhilnehc-ynoscommented, Sep 28, 2021

You don’t need to do reset() manually, just let the unique_ptr with its destroy_ros_message_function do the magic.

Refer to https://github.com/ros2/rclpy/blob/691e4fbfcb4bd4cc2a01182a7dced3105a78200b/rclpy/src/rclpy/action_client.cpp#L102-L121

Read more comments on GitHub >

github_iconTop Results From Across the Web

Requests memory leak · Issue #4601 · psf/requests - GitHub
I have tryied with 2 - 200 threads and memory leaks anyway. Im not using a tool, i just did this changes to...
Read more >
Memory Leak in Python requests - GeeksforGeeks
When a programmer forgets to clear a memory allocated in heap memory, the memory leak occurs. It's a type of resource leak or...
Read more >
Memory leak in c++ python wrapper - Stack Overflow
I have finally found the segfault. The header I was using for compiling the program was different from the one used by the...
Read more >
Debugging and preventing memory errors in Python - Medium
This case is essentially the same case outlined in the intro: a server runs out of memory while processing a request. The dangerous...
Read more >
Memory leak in sockets - Discussions on Python.org
The client send a random data to forwarder server that forward data to “echo server”, and the echo server send back received data....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found