Skip to content

Commit

Permalink
Bug 1282606 - Fix TrackingFlags memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Hardison committed Jul 4, 2016
1 parent 3bf65ba commit 9a15297
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 6 deletions.
1 change: 1 addition & 0 deletions Bugzilla.pm
Expand Up @@ -827,6 +827,7 @@ sub _cleanup {

# BMO - allow "end of request" processing
Bugzilla::Hook::process('request_cleanup');
Bugzilla::Bug->CLEANUP;

my $main = Bugzilla->request_cache->{dbh_main};
my $shadow = Bugzilla->request_cache->{dbh_shadow};
Expand Down
7 changes: 6 additions & 1 deletion Bugzilla/Attachment.pm
Expand Up @@ -62,6 +62,7 @@ use Bugzilla::Hook;

use File::Copy;
use List::Util qw(max);
use Scalar::Util qw(weaken);
use Storable qw(dclone);

use base qw(Bugzilla::Object);
Expand Down Expand Up @@ -157,8 +158,12 @@ the bug object to which the attachment is attached
=cut

sub bug {
my ($self) = @_;
require Bugzilla::Bug;
return $_[0]->{bug} //= Bugzilla::Bug->new({ id => $_[0]->bug_id, cache => 1 });
return $self->{bug} if defined $self->{bug};
my $bug = $self->{bug} = Bugzilla::Bug->new({ id => $_[0]->bug_id, cache => 1 });
weaken($self->{bug});
return $bug;
}

=over
Expand Down
14 changes: 14 additions & 0 deletions Bugzilla/Bug.pm
Expand Up @@ -66,6 +66,8 @@ use base qw(Bugzilla::Object Exporter);
editable_bug_fields
);

my %CLEANUP;

#####################################################################
# Constants
#####################################################################
Expand Down Expand Up @@ -367,6 +369,9 @@ sub new {
return $error_self;
}

$CLEANUP{$self->id} = $self;
weaken($CLEANUP{$self->id});

return $self;
}

Expand All @@ -381,6 +386,15 @@ sub object_cache_key {
return $key . ',' . Bugzilla->user->id;
}

sub CLEANUP {
foreach my $bug (values %CLEANUP) {
next unless $bug;
delete $bug->{depends_on_obj};
delete $bug->{blocks_obj};
}
%CLEANUP = ();
}

sub check {
my $class = shift;
my ($param, $field) = @_;
Expand Down
7 changes: 4 additions & 3 deletions Bugzilla/Comment.pm
Expand Up @@ -34,7 +34,7 @@ use Bugzilla::User;
use Bugzilla::Util;

use List::Util qw(first);
use Scalar::Util qw(blessed);
use Scalar::Util qw(blessed weaken);

###############################
#### Initialization ####
Expand Down Expand Up @@ -241,8 +241,9 @@ sub collapsed_reason {
sub bug {
my $self = shift;
require Bugzilla::Bug;
$self->{bug} ||= new Bugzilla::Bug($self->bug_id);
return $self->{bug};
my $bug = $self->{bug} ||= new Bugzilla::Bug($self->bug_id);
weaken($self->{bug});
return $bug;
}

sub is_about_attachment {
Expand Down
15 changes: 15 additions & 0 deletions extensions/TrackingFlags/Extension.pm
Expand Up @@ -29,6 +29,7 @@ use JSON;
use List::MoreUtils qw(none);

our $VERSION = '1';
our @FLAG_CACHE;

BEGIN {
*Bugzilla::tracking_flags = \&_tracking_flags;
Expand Down Expand Up @@ -552,6 +553,20 @@ sub _tracking_flags_search_nonchanged {
}
}

sub request_cleanup {
foreach my $flag (@FLAG_CACHE) {
my $bug_flag = delete $flag->{bug_flag};
if ($bug_flag) {
delete $bug_flag->{bug};
delete $bug_flag->{tracking_flag};
}
foreach my $value (@{ $flag->{values} }) {
delete $value->{tracking_flag};
}
}
@FLAG_CACHE = ();
}

sub bug_end_of_create {
my ($self, $args) = @_;
my $bug = $args->{'bug'};
Expand Down
10 changes: 10 additions & 0 deletions extensions/TrackingFlags/lib/Flag.pm
Expand Up @@ -87,6 +87,16 @@ sub new {
return $class->SUPER::new($param);
}

sub new_from_hash {
my $class = shift;
my $cache = Bugzilla->request_cache->{'tracking_flags'} //= {};
my $flag = $class->SUPER::new_from_hash(@_);
if ($flag) {
push @Bugzilla::Extension::TrackingFlags::FLAG_CACHE, $flag;
}
return $flag;
}

sub create {
my $class = shift;
my $params = shift;
Expand Down
7 changes: 5 additions & 2 deletions extensions/TrackingFlags/lib/Flag/Value.pm
Expand Up @@ -15,7 +15,7 @@ use warnings;
use Bugzilla::Error;
use Bugzilla::Group;
use Bugzilla::Util qw(detaint_natural trim);
use Scalar::Util qw(blessed);
use Scalar::Util qw(blessed weaken);

###############################
#### Initialization ####
Expand Down Expand Up @@ -118,9 +118,12 @@ sub is_active { return $_[0]->{'is_active'}; }
sub comment { return $_[0]->{'comment'}; }

sub tracking_flag {
return $_[0]->{'tracking_flag'} ||= Bugzilla::Extension::TrackingFlags::Flag->new({
return $_[0]->{'tracking_flag'} if $_[0]->{'tracking_flag'};
my $tf = $_[0]->{'tracking_flag'} = Bugzilla::Extension::TrackingFlags::Flag->new({
id => $_[0]->tracking_flag_id, cache => 1
});
weaken($_[0]->{'tracking_flag'});
return $tf;
}

sub setter_group {
Expand Down

0 comments on commit 9a15297

Please sign in to comment.