Closed leklund closed 7 years ago
Fixed via #36. Thanks @leklund!
Oops, was going quickly and didn't realize there were two issues in one here.
It is disconcerting that this would cause lots of memory usage.
Like you said, reusing producers is an easy fix for this, but I'd still like to take a look at what's causing this memory not to be freed up.
@leklund Looks like the culprit is the at_exit
handler that each new producer registers. Simplest solution would be to just remove that. I think that generally should be fine. Any particular opinions on removing that @freerobby or @alieander?
Here's the relevant line: https://github.com/wistia/nsq-ruby/blob/master/lib/nsq/producer.rb#L31
@bschwartz I can't think of any reason why removing that hander would cause issues.
@bschwartz Will take a closer look -- only initial question is, if we remove at_exit
, are we assuming people will close their connections manually?
@leklund Thanks for the socket fix. I wonder if I should add a test that sets a low open file limit (i.e. via ulimit -n
) and opens and closes a bunch of connections to ensure we don't regress here.
@freerobby Yes, exactly. I think that's probably fine as we already instruct folks to terminate
their producers and consumers in the docs.
I didn't realize it would hold on to the memory like that when I initially added the at_exit
handlers. Should have realized that!
I'm going to remove them when I get a chance.
In tracking down memory leaks in our sidekqiq workers I've discovered some problems with nsq-ruby. It appear to be leaking memory when creating and terminating large numbers of Nsq::Producer objects. The script I'm using is a bit contrived but it mimics the behavior of a sidekiq worker that was creating a new Producer per job.
First, there seems to be an issue with closing sockets. When the folloing code is run it will work for about 30k iterations before blowing up with
Too many open files
.Output:
When I add
@socket.close
before this line https://github.com/wistia/nsq-ruby/blob/master/lib/nsq/connection.rb#L360, the code will run until completion but will continue to grow in memory usage.The usage is steady for a while and then jumps significantly in spots as it iterates.
I'm opening a PR for the
@socket.close
bug. I haven't seen an obvious fix to the memory leak. My solution for our app is to re-use the producers instead of re-creating as needed.Tested using: