From 9a15297a823443b19661211293bb79e7f42a44e3 Mon Sep 17 00:00:00 2001 From: Dylan Hardison Date: Mon, 4 Jul 2016 17:36:58 -0400 Subject: Bug 1282606 - Fix TrackingFlags memory leak --- Bugzilla.pm | 1 + Bugzilla/Attachment.pm | 7 ++++++- Bugzilla/Bug.pm | 14 ++++++++++++++ Bugzilla/Comment.pm | 7 ++++--- extensions/TrackingFlags/Extension.pm | 15 +++++++++++++++ extensions/TrackingFlags/lib/Flag.pm | 10 ++++++++++ extensions/TrackingFlags/lib/Flag/Value.pm | 7 +++++-- 7 files changed, 55 insertions(+), 6 deletions(-) diff --git a/Bugzilla.pm b/Bugzilla.pm index 4f80a2ed4..56bbb5c15 100644 --- a/Bugzilla.pm +++ b/Bugzilla.pm @@ -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}; diff --git a/Bugzilla/Attachment.pm b/Bugzilla/Attachment.pm index 0cdec6bf0..3b4953f64 100644 --- a/Bugzilla/Attachment.pm +++ b/Bugzilla/Attachment.pm @@ -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); @@ -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 diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 09696f97b..327fb866c 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -66,6 +66,8 @@ use base qw(Bugzilla::Object Exporter); editable_bug_fields ); +my %CLEANUP; + ##################################################################### # Constants ##################################################################### @@ -367,6 +369,9 @@ sub new { return $error_self; } + $CLEANUP{$self->id} = $self; + weaken($CLEANUP{$self->id}); + return $self; } @@ -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) = @_; diff --git a/Bugzilla/Comment.pm b/Bugzilla/Comment.pm index 69e973c7f..ff718c9e1 100644 --- a/Bugzilla/Comment.pm +++ b/Bugzilla/Comment.pm @@ -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 #### @@ -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 { diff --git a/extensions/TrackingFlags/Extension.pm b/extensions/TrackingFlags/Extension.pm index b150faea2..08e7968a0 100644 --- a/extensions/TrackingFlags/Extension.pm +++ b/extensions/TrackingFlags/Extension.pm @@ -29,6 +29,7 @@ use JSON; use List::MoreUtils qw(none); our $VERSION = '1'; +our @FLAG_CACHE; BEGIN { *Bugzilla::tracking_flags = \&_tracking_flags; @@ -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'}; diff --git a/extensions/TrackingFlags/lib/Flag.pm b/extensions/TrackingFlags/lib/Flag.pm index cfda0bad5..7b6effa98 100644 --- a/extensions/TrackingFlags/lib/Flag.pm +++ b/extensions/TrackingFlags/lib/Flag.pm @@ -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; diff --git a/extensions/TrackingFlags/lib/Flag/Value.pm b/extensions/TrackingFlags/lib/Flag/Value.pm index 964d76810..15be61bc7 100644 --- a/extensions/TrackingFlags/lib/Flag/Value.pm +++ b/extensions/TrackingFlags/lib/Flag/Value.pm @@ -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 #### @@ -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 { -- cgit v1.2.3-24-g4f1b