[pve-devel] memory leak in PVE/QMPClient.pm

Stefan Priebe - Profihost AG s.priebe at profihost.ag
Fri Oct 26 12:54:30 CEST 2012


OK,

here is a complete fix. To make sure perl deletes the QMPClient object 
when going out of scope instead of keep the object until end:


commit bc6e44f6ca0025088cb75c91ef48727f31fe2539
Author: Stefan Priebe <s.priebe at profihost.ag>
Date:   Fri Oct 26 12:53:34 2012 +0200

     - fix mux memory leak in QMPClient

diff --git a/PVE/QMPClient.pm b/PVE/QMPClient.pm
index 9829986..e9d6ab9 100755
--- a/PVE/QMPClient.pm
+++ b/PVE/QMPClient.pm
@@ -7,6 +7,7 @@ use IO::Multiplex;
  use POSIX qw(EINTR EAGAIN);
  use JSON;
  use Time::HiRes qw(usleep gettimeofday tv_interval);
+use Scalar::Util qw(weaken);

  use Data::Dumper;

@@ -35,9 +36,19 @@ sub new {

      $mux->set_callback_object($self);

+    # make sure perl doesn't believe this is a circular reference as we 
delete mux in DESTROY
+    weaken($mux->{_object});
+
      return $self;
  }

+sub DESTROY {
+    my ($self) = @_;
+
+    # delete self referenced mux
+    $self->{mux} = undef;
+}
+
  # add a single command to the queue for later execution
  # with queue_execute()
  sub queue_cmd {


Stefan
Am 26.10.2012 11:36, schrieb Stefan Priebe - Profihost AG:
> Hello list,
>
> while debugging i found a memory leak in PVE/QMPClient.pm. The leak
> happens due to the self reference of $mux and setting $self as callback
> of mux.
>
> Code:
> sub new {
>      my ($class, $eventcb) = @_;
>
>      my $mux = new IO::Multiplex;
>
>      my $self = bless {
>          mux => $mux,
>          fhs => {}, # $vmid => fh
>          fhs_lookup => {}, # $fh => $vmid
>          queue => {},
>          current => {},
>          errors => {},
>      }, $class;
>
>      $self->{eventcb} = $eventcb if $eventcb;
>
>      $mux->set_callback_object($self);
>
>      return $self;
> }
>
> To fix this we need a DESTROY function in QMPClient:
> sub DESTROY {
>      my ($self) = @_;
>
>      $self->{mux} = undef;
> }
>
> This is enough to remove the circular reference.
>
> Stefan
> _______________________________________________
> pve-devel mailing list
> pve-devel at pve.proxmox.com
> http://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel



More information about the pve-devel mailing list