Skip to content

Cache gRPC connections #1164

Description

@pierre-b

Hello guys,

I use PubSub to pulish thousands of messages and a memory leak crashes my server.

Here is a Passenger screenshot using gcloud:
gcloud

Using request:
gcloud

Here is a simple repo to reproduce it: https://github.com/pierre-b/test-leak

Environnement:

  • Ubuntu 14.04
  • node 4.4.0
  • Passenger 5.0.26
  • gcloud 0.29.0

Did I miss something?
thanks for your help

Activity

  1. pierre-b commented on Mar 13, 2016

    @pierre-b
    Author

    Forgot to mention the request interceptor does'nt work too in my test... if you can explain me how to implement it the right way ?!

  2. pierre-b commented on Mar 14, 2016

    @pierre-b
    Author

    the problem seems to come from the request package (see issue with https requests.

    I added Wreck to my test-repo and Wreck is so far the best https client. I would recommend to switch to Wreck as long as Request has a leak...

    wreck

  3. jgeewax commented on Mar 14, 2016

    @jgeewax
    Contributor

    Thanks for the note @pierre-b . @stephenplusplus , thoughts on switching our transport?

  4. stephenplusplus commented on Mar 14, 2016

    @stephenplusplus
    Contributor

    Unfortunately, wreck is only written for Node v4 and greater. We still need to support v0.12.0.

    We switched Pub/Sub to use gRPC and proto files with our last release. Because that library is used, the request interceptors don't have an impact. I'll make a note of that in the docs.

    When I'm trying to test with your repo, I'm running into:

    {"statusCode":400,"error":"Bad Request","message":"Invalid cookie value"}
    

    Is there a quick solution to get past that?

  5. pierre-b commented on Mar 14, 2016

    @pierre-b
    Author

    Thank you guys for investigating this!

    @stephenplusplus maybe you need to clear your cookies? looks like HAPI tries to parse something on your localhost?!

  6. stephenplusplus commented on Mar 14, 2016

    @stephenplusplus
    Contributor

    Thanks, that did it. Can you show the passenger command you're running to see that output?

  7. pierre-b commented on Mar 15, 2016

    @pierre-b
    Author

    sudo watch passenger-status

  8. stephenplusplus commented on Mar 15, 2016

    @stephenplusplus
    Contributor

    In your demo, after downgrading to [email protected], the memory hike was negligible. I believe this can be traced to grpc, which was introduced as the Pub/Sub transport in version [email protected].

  9. pierre-b commented on Mar 16, 2016

    @pierre-b
    Author

    I confirm 0.27.0 is much better!
    115M after 22 requests (each request publishes 100 messages in parallel)

  10. pierre-b commented on Mar 16, 2016

    @pierre-b
    Author

    Also, [email protected] used in [email protected] consumes 27% less memory than [email protected].

    22 requests (each request publishes 100 messages in parallel):
    2.53.0: 124M
    2.69.0: 171M

  11. stephenplusplus commented on Mar 16, 2016

    @stephenplusplus
    Contributor

    @pierre-b would you mind opening an issue on the grpc repo about the high memory usage? I tried digging into it, but no doubt they'd be more efficient.

  12. pierre-b commented on Mar 17, 2016

    @pierre-b
    Author

    There is no open issue regarding a memory leak on the grpc nodejs repo, are you sure the problem comes from them and not the pubsub implementation?

    As I never implemented the grpc node package I don't feel rightful to open an issue on their repo!

  13. stephenplusplus commented on Mar 17, 2016

    @stephenplusplus
    Contributor

    I completely understand :)

    @murgatroid99 The test @pierre-b put together instantiates a service 100x, which hikes the memory usage up by about 100 MB. We don't retain a reference to the object, but it seems to persist in memory. Is this something that will be resolved once we pre-compile the proto files and use message objects (#1134 (comment))?

  14. murgatroid99 commented on Mar 17, 2016

    @murgatroid99

    This usage pattern is not expected or intended to be performant. Each time you instantiate a service object, it creates a channel, which contains a number of things, including a full TCP socket/TLS session/HTTP2 session stack. This has to stay alive at least until the call you make with it is complete.

    It would be better to initialize a single client object per server/service combination, and then use it for every call to that service on that server. This will allow gRPC to multiplex the calls on a single TCP connection, and will avoid having to deal with initial connection delay every single time you make a call.

  15. stephenplusplus commented on Mar 17, 2016

    @stephenplusplus
    Contributor

    Thanks, that gives me a lot to think about. I'm still wondering why the memory remains after the service calls have been made, however. Is there an extra step to stop the channel?

    Also, regarding caching service connections, it will be difficult to know if a user is making a single call or many. Is there a default amount of time that passes that will close a channel? Would this caching be better handled within grpc?

  16. 8 remaining items

  17. stephenplusplus commented on Mar 24, 2016

    @stephenplusplus
    Contributor

    @pierre-b I put a PR together with a quick caching implementation: #1182 -- feel free to try it out and let me know how it goes!

  18. pierre-b commented on Mar 26, 2016

    @pierre-b
    Author

    Thanks, will try it out ;)

  19. stephenplusplus commented on Mar 28, 2016

    @stephenplusplus
    Contributor

    Opened an issue on the gRPC library to track the memory leak: grpc/grpc#5970

  20. pinazo commented on Jul 20, 2016

    @pinazo

    Hello guys,

    I've just started using gcloud-node, 3 days ago. I am using Pub/Sub to publish a stream of packets that my server receives, converts to JSON and publish it.
    All good until I deployed on the server and started receiving more traffic. The process increases memory usage until it crashes.

    I see all related issues to this memory leak problem are closed, so let me know if you guys would like me to open a new issue. I've isolated the problem and the memory leak happens only when using cloud topic.publish a lot of times.

    My environment is:

    • Linux Ubuntu 14.04
    • Node.js 5.6.0
    • gcloud 0.37.0

    Below is the chart showing how the memory increases, from process.memoryUsage(). From 17:10 to 17:50 there is an execution measure in the aforementioned environment.

    screen shot 2016-07-20 at 14 01 09

    Please let me know if any further information is needed.

  21. stephenplusplus commented on Jul 20, 2016

    @stephenplusplus
    Contributor

    Thanks for the report. I believe this was resolved upstream in gRPC, although it hasn't been merged yet:

    It will probably take a while for the change to appear in this library. You should be able to install grpc from that PR's branch manually in the meantime:

    $ npm install --save murgatroid99/grpc#node_client_creds_memory_leak
  22. stephenplusplus commented on Jul 20, 2016

    @stephenplusplus
    Contributor

    Sorry about the crashing :(

  23. pinazo commented on Jul 20, 2016

    @pinazo

    Ok, thanks Stephen!

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

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions