develooper Front page | perl.perl5.porters | Postings from August 2019

[perl #134313] Blessed anonymous subroutines not reliablydeterministically destroyed.

From:
Tony Cook via RT
Date:
August 28, 2019 06:32
Subject:
[perl #134313] Blessed anonymous subroutines not reliablydeterministically destroyed.
Message ID:
rt-4.0.24-11263-1566973960-1140.134313-15-0@perl.org
On Thu, 25 Jul 2019 03:38:34 -0700, demerphq wrote:
> The following two one liners should output the same thing, in
> particular they should both output the same thing as the second piece
> of code.
> 
> This potentially could screw up code that is trying to do "ScopeExit"
> type behavior. I find it particularly confusing and wrong that the
> first example does not print out two "destroying lines".
> 
> $ perl -le'sub DestroyNow::DESTROY { print "destroying $_[0] in
> ${^GLOBAL_PHASE}" } sub foo { my $x= bless sub {}, "DestroyNow"; undef
> $x; return undef;}  foo(); foo(); print "All done\n" '
> All done
> 
> destroying DestroyNow=CODE(0x8bc6f8) in DESTRUCT
> 
> $ perl -le'sub DestroyNow::DESTROY { print "destroying $_[0] in
> ${^GLOBAL_PHASE}" } sub foo { my $y= shift; my $x= bless sub {$y},
> "DestroyNow"; undef $x; return undef;}  foo(); foo(); print "All
> done\n" '
> destroying DestroyNow=CODE(0xf83218) in RUN
> destroying DestroyNow=CODE(0xf83218) in RUN
> All done
> 
> I'm told this is an optimisation, but I dont think the optimisation is
> safe given this. As soon as a coderef is blessed it needs to be
> treated like a closure and be distinct from any copies.
> 
> The tested Perl was 5.18.4, but I have replicated this behavior on
> perl 5.24 as well. (Haven't checked blead yet.)

This behaviour has been around since at least 5.6, and I expect since 5.000, so I suspect fixing it might break some code.

Fixing it would be kind of messy, if pp_anoncode sees that the CV in the pad is blessed, it would need to clone the SV, replace that in the pad and turn the CvCLONE() flag on so that future referents are cloned.

cv_clone() would have to handle cloning a CV without a pad.

Tony

---
via perlbug:  queue: perl5 status: new
https://rt.perl.org/Ticket/Display.html?id=134313



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About