1 package DBICTest::Util::LeakTracer;
7 use Scalar::Util qw(isweak weaken blessed reftype);
8 use DBIx::Class::_Util 'refcount';
9 use DBIx::Class::Optional::Dependencies;
10 use Data::Dumper::Concise;
11 use DBICTest::Util 'stacktrace';
13 CV_TRACING => DBIx::Class::Optional::Dependencies->req_ok_for ('test_leaks_heavy'),
14 SKIP_SCALAR_REFS => ( $] > 5.017 ) ? 1 : 0,
18 our @EXPORT_OK = qw(populate_weakregistry assert_empty_weakregistry hrefaddr visit_refs);
24 sub hrefaddr { sprintf '0x%x', &Scalar::Util::refaddr }
26 # so we don't trigger stringification
29 (defined blessed $_[0]) ? blessed($_[0]) . '=' : '',
35 sub populate_weakregistry {
36 my ($weak_registry, $target, $note) = @_;
38 croak 'Expecting a registry hashref' unless ref $weak_registry eq 'HASH';
39 croak 'Target is not a reference' unless length ref $target;
41 my $refaddr = hrefaddr $target;
43 # a registry could be fed to itself or another registry via recursive sweeps
44 return $target if $reg_of_regs{$refaddr};
46 weaken( $reg_of_regs{ hrefaddr($weak_registry) } = $weak_registry )
47 unless( $reg_of_regs{ hrefaddr($weak_registry) } );
49 # an explicit "garbage collection" pass every time we store a ref
50 # if we do not do this the registry will keep growing appearing
51 # as if the traced program is continuously slowly leaking memory
52 for my $reg (values %reg_of_regs) {
53 (defined $reg->{$_}{weakref}) or delete $reg->{$_}
57 # FIXME/INVESTIGATE - something fishy is going on with refs to plain
58 # strings, perhaps something to do with the CoW work etc...
59 return $target if SKIP_SCALAR_REFS and reftype($target) eq 'SCALAR';
61 if (! defined $weak_registry->{$refaddr}{weakref}) {
62 $weak_registry->{$refaddr} = {
63 stacktrace => stacktrace(1),
66 weaken( $weak_registry->{$refaddr}{weakref} );
70 my $desc = _describe_ref($target);
71 $weak_registry->{$refaddr}{slot_names}{$desc} = 1;
73 $note =~ s/\s*\Q$desc\E\s*//g;
74 $weak_registry->{$refaddr}{slot_names}{$note} = 1;
80 # Regenerate the slots names on a thread spawn
82 my @individual_regs = grep { scalar keys %{$_||{}} } values %reg_of_regs;
85 for my $reg (@individual_regs) {
86 my @live_slots = grep { defined $_->{weakref} } values %$reg
89 $reg = {}; # get a fresh hashref in the new thread ctx
90 weaken( $reg_of_regs{hrefaddr($reg)} = $reg );
92 for my $slot_info (@live_slots) {
93 my $new_addr = hrefaddr $slot_info->{weakref};
95 # replace all slot names
96 $slot_info->{slot_names} = { map {
98 $name =~ s/\(0x[0-9A-F]+\)/sprintf ('(%s)', $new_addr)/ieg;
100 } keys %{$slot_info->{slot_names}} };
102 $reg->{$new_addr} = $slot_info;
108 my $args = { (ref $_[0]) ? %{$_[0]} : @_ };
110 $args->{seen_refs} ||= {};
112 my $visited_cnt = '0E0';
113 for my $i (0 .. $#{$args->{refs}} ) {
114 next if isweak($args->{refs}[$i]);
116 my $r = $args->{refs}[$i];
118 next unless length ref $r;
120 next if $args->{seen_refs}{my $dec_addr = Scalar::Util::refaddr($r)}++;
123 $args->{action}->($r) or next;
125 my $type = reftype $r;
126 if ($type eq 'HASH') {
127 $visited_cnt += visit_refs({ %$args, refs => [ map {
128 ( !isweak($r->{$_}) ) ? $r->{$_} : ()
131 elsif ($type eq 'ARRAY') {
132 $visited_cnt += visit_refs({ %$args, refs => [ map {
133 ( !isweak($r->[$_]) ) ? $r->[$_] : ()
136 elsif ($type eq 'REF' and !isweak($$r)) {
137 $visited_cnt += visit_refs({ %$args, refs => [ $$r ] });
139 elsif (CV_TRACING and $type eq 'CODE') {
140 $visited_cnt += visit_refs({ %$args, refs => [ map {
141 ( !isweak($_) ) ? $_ : ()
142 } scalar PadWalker::closed_over($r) ] }); # scalar due to RT#92269
148 sub assert_empty_weakregistry {
149 my ($weak_registry, $quiet) = @_;
151 # in case we hooked bless any extra object creation will wreak
152 # havoc during the assert phase
153 local *CORE::GLOBAL::bless;
154 *CORE::GLOBAL::bless = sub { CORE::bless( $_[0], (@_ > 1) ? $_[1] : caller() ) };
156 croak 'Expecting a registry hashref' unless ref $weak_registry eq 'HASH';
158 return unless keys %$weak_registry;
160 my $tb = eval { Test::Builder->new }
161 or croak "Calling assert_empty_weakregistry in $0 without a loaded Test::Builder makes no sense";
163 for my $addr (keys %$weak_registry) {
164 $weak_registry->{$addr}{display_name} = join ' | ', (
166 { length $a <=> length $b or $a cmp $b }
167 keys %{$weak_registry->{$addr}{slot_names}}
170 $tb->BAILOUT("!!!! WEAK REGISTRY SLOT $weak_registry->{$addr}{display_name} IS NOT A WEAKREF !!!!")
171 if defined $weak_registry->{$addr}{weakref} and ! isweak( $weak_registry->{$addr}{weakref} );
174 # compile a list of refs stored as globals (possibly even catching
175 # class data in the form of method closures), so we can skip them
177 my ($seen_refs, $classdata_refs) = ({}, undef);
179 # the walk is very expensive - if we are $quiet (running in an END block)
180 # we do not really need to be too thorough
182 my ($symwalker, $symcounts);
185 my $pkg = shift || '::';
187 # any non-weak globals are "clasdata" in all possible sense
189 # the unless regex at the end skips some dangerous namespaces outright
190 # (but does not prevent descent)
191 $symcounts->{$pkg} += visit_refs (
192 seen_refs => $seen_refs,
193 action => sub { ++$classdata_refs->{hrefaddr $_[0]} },
194 refs => [ map { my $sym = $_;
195 # *{"$pkg$sym"}{CODE} won't simply work - MRO-cached CVs are invisible there
196 ( CV_TRACING ? Class::MethodCache::get_cv("${pkg}$sym") : () ),
198 ( defined *{"$pkg$sym"}{SCALAR} and length ref ${"$pkg$sym"} and ! isweak( ${"$pkg$sym"} ) )
202 ( defined *{"$pkg$sym"}{$_} and ! isweak(defined *{"$pkg$sym"}{$_}) )
205 } qw(HASH ARRAY IO GLOB) ),
207 ) unless $pkg =~ /^ :: (?:
208 DB | next | B | .+? ::::ISA (?: ::CACHE ) | Class::C3
211 $symwalker->("${pkg}$_") for grep { $_ =~ /(?<!^main)::$/ } keys %$pkg;
218 # { { $_ => $symcounts->{$_} } }
220 # {$symcounts->{$a} <=> $symcounts->{$b} }
225 delete $weak_registry->{$_} for keys %$classdata_refs;
227 for my $addr (sort { $weak_registry->{$a}{display_name} cmp $weak_registry->{$b}{display_name} } keys %$weak_registry) {
229 next if ! defined $weak_registry->{$addr}{weakref};
231 $leaks_found++ unless $tb->in_todo;
232 $tb->ok (0, "Leaked $weak_registry->{$addr}{display_name}");
235 local $Data::Dumper::Maxdepth = 1;
236 sprintf "\n%s (refcnt %d) => %s\n",
237 $weak_registry->{$addr}{display_name},
238 refcount($weak_registry->{$addr}{weakref}),
240 ref($weak_registry->{$addr}{weakref}) eq 'CODE'
242 B::svref_2object($weak_registry->{$addr}{weakref})->XSUB
243 ) ? '__XSUB__' : Dumper( $weak_registry->{$addr}{weakref} )
247 # FIXME - need to add a circular reference seeker based on the visitor
248 # (will need a bunch of modifications, punting with just a stub for now)
250 $diag .= Devel::FindRef::track ($weak_registry->{$addr}{weakref}, 50) . "\n"
251 if ( $ENV{TEST_VERBOSE} && eval { require Devel::FindRef });
255 if (my $stack = $weak_registry->{$addr}{stacktrace}) {
256 $diag .= " Reference first seen$stack";
262 if (! $quiet and !$leaks_found and ! $tb->in_todo) {
263 $tb->ok(1, sprintf "No leaks found at %s line %d", (caller())[1,2] );
268 if ($INC{'Test/Builder.pm'}) {
269 my $tb = Test::Builder->new;
271 # we check for test passage - a leak may be a part of a TODO
272 if ($leaks_found and !$tb->is_passing) {
275 "\n\n%s\n%s\n\nInstall Devel::FindRef and re-run the test with set "
276 . '$ENV{TEST_VERBOSE} (prove -v) to see a more detailed leak-report'
277 . "\n\n%s\n%s\n\n", ('#' x 16) x 4
278 ) if ( !$ENV{TEST_VERBOSE} or !$INC{'Devel/FindRef.pm'} );
282 $tb->note("Auto checked $refs_traced references for leaks - none detected");