majensen / perlbolt

Neo4j server agent using Bolt protocol
Apache License 2.0
4 stars 2 forks source link

Possible Memory leak within fetch_next #61

Open AndreaAstronaut opened 5 days ago

AndreaAstronaut commented 5 days ago

To reproduce the issue I am using the movies database available to download here: https://github.com/neo4j-graph-examples/movies

This is the script:

use URI::bolt;
use Neo4j::Bolt;

my $url = URI::bolt->new("bolt://localhost");
$url->userinfo('user:password’);
$cxn = Neo4j::Bolt->connect($url);
unless ($cxn->connected) {
  die "Problem connecting: ".$cxn->errmsg;
}

my $iterationCount = 0;
while ($iterationCount < 100000) {
   $iterationCount++;
   $stream = $cxn->run_query(
      'MATCH (keanu:Person {name:"Keanu Reeves"}) RETURN keanu.born AS born',{},'movies'
   );
   # Below causes memory leak
   my $row = ($stream->fetch_next)[0];
   print "Iteration $iterationCount: "  . $row ."\n";
   # Below does NOT cause memory leak
   #my $names = ($stream->field_names)[0];
   #print "Iteration $iterationCount: "  . $names ."\n";
   unless ( $stream->success ) {
      print STDERR "Uh oh: ".($stream->client_errmsg || $stream->server_errmsg);
    }
}

Running above the way it is gives me a memory growth of 500 KiB every 10 seconds.

Running above with the commented-out lines instead (stream->field_names instead of $stream_fetch_next) there is zero memory growth.

I managed to trace the problem down to the following line in fetch_next: https://github.com/majensen/perlbolt/blob/4e5e932ae1ef9c928bf03b36bdb896fc4aed2abf/lib/Neo4j/Bolt/ResultStream.xs#L49

From this function the memory leak seems to be coming from the following line for the examples I tried: https://github.com/majensen/perlbolt/blob/4e5e932ae1ef9c928bf03b36bdb896fc4aed2abf/lib/Neo4j/Bolt/CTypeHandlers.xs#L345

From this function it seems to be coming from the following line:

https://github.com/majensen/perlbolt/blob/4e5e932ae1ef9c928bf03b36bdb896fc4aed2abf/lib/Neo4j/Bolt/CTypeHandlers.xs#L312

I encountered this on Oracle Linux 7 running Neo4j 5.13 and Perl 5.16.3. Neo4j::Bolt 0.4203 and Neo4j::Client 0.46 (also tried 0.52 with the same outcome).

I verified this is also happening on Oracle Linux 8 with Perl version 5.38.2 and the same versions of Neo4j::Bolt and Neo4j::Client.

johannessen commented 5 days ago

Hi @AndreaAstronaut, thanks a lot for the report and your detailed investigation! You’re right, there are memory leaks in the lines you mention.

I’ve pushed a fix to the fix-memory-leaks branch.

AndreaAstronaut commented 4 days ago

Hi @johannessen thank you for the quick turnaround! I can confirm no memory leak for me with the fix.