Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memory leak on service call #774

Merged
merged 6 commits into from
Oct 31, 2022
Merged

fix memory leak on service call #774

merged 6 commits into from
Oct 31, 2022

Conversation

obi-t4
Copy link
Contributor

@obi-t4 obi-t4 commented Aug 3, 2022

Public API Changes

None

Description

A memory leak occurs each time a service call is invoked from rosbridge client.
This means calling service call from the client leads to resource exhaustion.
This fixes #772

#772

@obi-t4 obi-t4 changed the title add destroy_client fix memory leak on service call Aug 4, 2022
@lroop
Copy link

lroop commented Sep 20, 2022

I can confirm that I have observed this same issue with rosbridge gradually consuming a lot of CPU and memory after running for several hours, causing other ROS nodes to slow down, and this PR appears to fix the issue.

Copy link
Contributor

@achim-k achim-k left a comment

Choose a reason for hiding this comment

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

This does not work if you have multiple services with the same service type. I would propose to simply destroy the service client after the call.

Comment on lines 125 to 129
if service_class in clients:
client = clients[service_class]
else:
client = node_handle.create_client(service_class, service)
clients[service_class] = client
Copy link
Contributor

Choose a reason for hiding this comment

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

This does not work if you have multiple services with the same service type but different names. Let's say you have two identical services under a different namespace:

  • /robot_1/do_something
  • /robot_2/do_something

You will only call one of these services (the one you called first), as they have the same service type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll consider it, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@achim-k

I would propose to simply destroy the service client after the call.

Calling rclpy.service.Service.destroy is user responsibility, so I think it cannot be used in the library.
And I know that node_handle.destroy_client(client) does not free all memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@achim-k 1909996 should work.

@obi-t4 obi-t4 requested a review from achim-k October 14, 2022 12:01
@obi-t4
Copy link
Contributor Author

obi-t4 commented Oct 26, 2022

@achim-k
We must fix this memory leak issue for service call. Otherwise service call can't be used.

Copy link
Contributor

@achim-k achim-k left a comment

Choose a reason for hiding this comment

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

I think the simplest solution would be to destroy the service client again after use. Would that solution be ok with you?


result = client.call(inst)

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
node_handle.destroy_client(client)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@achim-k

No.

And I know that node_handle.destroy_client(client) does not free all memory.

Adding this code, memory leak issue is not solved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My first commit was this, but I found it didn't release all the memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@achim-k

releasing resources by node_handle.destroy_client(client) is rclpy responsibility, so I'll change this PR, and I'll also submit to fix this issue to rclpy repo.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you create a rclpy issue / PR, could you add a link here? Thanks!

@obi-t4
Copy link
Contributor Author

obi-t4 commented Oct 31, 2022

@achim-k PR has been updated, please have a check!

@obi-t4 obi-t4 requested a review from achim-k October 31, 2022 07:59

result = client.call(inst)

Copy link
Contributor

Choose a reason for hiding this comment

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

If you create a rclpy issue / PR, could you add a link here? Thanks!

@achim-k achim-k merged commit 7af3683 into RobotWebTools:ros2 Oct 31, 2022
@obi-t4 obi-t4 mentioned this pull request Nov 1, 2022
@obi-t4
Copy link
Contributor Author

obi-t4 commented Nov 2, 2022

@achim-k The isue: ros2/rclpy#1034 has been created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CPU & Memory usage increases over time.
3 participants