Skip Menu |

This queue is for tickets about the Try-Tiny CPAN distribution.

Report information
The Basics
Id: 112099
Status: resolved
Priority: 0/
Queue: Try-Tiny

People
Owner: ether [...] cpan.org
Requestors: Mai [...] jochen-schweizer.de
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: 0.25-TRIAL



Subject: finally block doesn't always run
Date: Wed, 17 Feb 2016 14:59:39 +0000
To: "bug-Try-Tiny [...] rt.cpan.org" <bug-Try-Tiny [...] rt.cpan.org>
From: Lukas Mai <Mai [...] jochen-schweizer.de>
The documentation says: "finally blocks are always executed making them suitable for cleanup code which cannot be handled using local." Here's a finally block that isn't always executed: $ cat try.t #!perl use strict; use warnings; use Try::Tiny; use Test::More tests => 3; my $finally; SKIP: { try { ok 1, "in try"; skip "whee", 1; ok 0, "you don't see me"; } finally { $finally = 1; }; } ok $finally, "finally ran"; __END__ $ perl try.t 1..3 ok 1 - in try ok 2 # skip whee not ok 3 - finally ran # Failed test 'finally ran' # at try.t line 20. # Looks like you failed 1 test of 3. Lukas Mai JEP Developer Jochen Schweizer Technology Solutions GmbH Rosenheimer Strasse 145 e-f D- 81671 Muenchen Telefon: +49 89 606089-348 Fax: +49 89 606089-949 E-Mail: mailto:mai@jochen-schweizer.de Ein Unternehmen der Jochen Schweizer Unternehmensgruppe www.jochen-schweizer.de/unternehmensgruppe HRB Muenchen 203111 Geschaeftsfuehrer: Florian Herschke Prokurist: Saad Daoud PS: Du bist, was du erlebst. Jetzt die Jochen Schweizer App kostenlos downloaden www.jochen-schweizer.de/app Hinweis: Diese Nachricht und jeder Anhang ist vertraulich und moeglicherweise rechtlich geschuetzt. Sollten Sie diese Nachricht irrtuemlich empfangen haben, benachrichtigen Sie bitte den Absender per Rueckantwort. Loeschen Sie diese Nachricht anschliessend sofort von Ihrem System. Bitte kopieren Sie diese Nachricht nicht, nutzen Sie den Inhalt der Nachricht nicht anderweitig und machen Sie die Nachricht und deren Inhalt keinem Dritten zugaenglich. Missbrauch kann strafrechtlich und zivilrechtlich verfolgt werden. NOTICE: The information contained in this e-mail is confidential or may otherwise be legally privileged. It is intended for the named recipient only. If you have received it in error, please notify us immediately by reply and delete this message and all its attachments. Please note that any unauthorised review, copying, disclosing or otherwise making use of the information is strictly prohibited.
Okay, I guess that should say: ""finally blocks are always executed, unless you use a goto to jump out..." :p
Subject: RE: [rt.cpan.org #112099] finally block doesn't always run
Date: Thu, 18 Feb 2016 07:47:16 +0000
To: "bug-Try-Tiny [...] rt.cpan.org" <bug-Try-Tiny [...] rt.cpan.org>
From: Lukas Mai <Mai [...] jochen-schweizer.de>
The documentation says: Furthermore exceptions in finally blocks are not trappable and are unable to influence the execution of your program. This is due to limitation of DESTROY-based scope guards, which finally is implemented on top of. This may change in a future version of Try::Tiny. So what's the point of Try::Tiny using scope guards if they don't always execute when leaving a try block (including with goto)? Show quoted text
-----Original Message----- From: Karen Etheridge via RT [mailto:bug-Try-Tiny@rt.cpan.org] Sent: Wednesday, February 17, 2016 7:16 PM To: Lukas Mai <Mai@jochen-schweizer.de> Subject: [rt.cpan.org #112099] finally block doesn't always run <URL: https://rt.cpan.org/Ticket/Display.html?id=112099 > Okay, I guess that should say: ""finally blocks are always executed, unless you use a goto to jump out..." :p
On 2016-02-17 23:47:33, Mai@jochen-schweizer.de wrote: Show quoted text
> The documentation says: > > Furthermore exceptions in finally blocks are not trappable and are > unable to influence the execution of your program. This is due to > limitation of DESTROY-based scope guards, which finally is implemented > on top of. This may change in a future version of Try::Tiny.
Isn't the issue that there is a goto in the *try* block, not the finally block? Let's tackle that first. Show quoted text
> So what's the point of Try::Tiny using scope guards if they don't > always execute when leaving a try block (including with goto)?
Looking at the code, I don't see why the Try::Tiny::ScopeGuard::DESTROY shouldn't catch an exiting of the scope via goto. But perhaps there is a hole here that pure perl code cannot close?
On Thu Feb 18 12:20:09 2016, ETHER wrote: Show quoted text
> On 2016-02-17 23:47:33, Mai@jochen-schweizer.de wrote: >
> > So what's the point of Try::Tiny using scope guards if they don't > > always execute when leaving a try block (including with goto)?
> > Looking at the code, I don't see why the > Try::Tiny::ScopeGuard::DESTROY shouldn't catch an exiting of the scope > via goto. But perhaps there is a hole here that pure perl code cannot > close?
DESTROY isn't working because at that time the ScopeGuards object haven't been created yet. A solution would be to create the ScopeGuards before entering the try block. I have a patch that does that, but it breaks the second test in t/global_destruction_forked.t: # Failed test 'nested try in cleanup after fork does not maintain outer finally block' # at t/global_destruction_forked.t line 53. # got: '256' # expected: '0' # Looks like you failed 1 test of 3. t/global_destruction_forked.t .. Dubious, test returned 1 (wstat 256, 0x100) Failed 1/3 subtests That's because this change makes 'try { exit } finally { ... }' run the '...' part. I'm not sure what to do about that.
On Fri Feb 19 11:34:00 2016, MAUKE wrote: Show quoted text
> On Thu Feb 18 12:20:09 2016, ETHER wrote:
> > On 2016-02-17 23:47:33, Mai@jochen-schweizer.de wrote: > >
> > > So what's the point of Try::Tiny using scope guards if they don't > > > always execute when leaving a try block (including with goto)?
> > > > Looking at the code, I don't see why the > > Try::Tiny::ScopeGuard::DESTROY shouldn't catch an exiting of the > > scope > > via goto. But perhaps there is a hole here that pure perl code cannot > > close?
> > DESTROY isn't working because at that time the ScopeGuards object > haven't been created yet. A solution would be to create the > ScopeGuards before entering the try block. > > I have a patch that does that, but it breaks the second test in > t/global_destruction_forked.t: > > # Failed test 'nested try in cleanup after fork does not maintain > outer finally block' > # at t/global_destruction_forked.t line 53. > # got: '256' > # expected: '0' > # Looks like you failed 1 test of 3. > t/global_destruction_forked.t .. Dubious, test returned 1 (wstat 256, > 0x100) > Failed 1/3 subtests > > That's because this change makes 'try { exit } finally { ... }' run > the '...' part. I'm not sure what to do about that.
I have a slightly disgusting fix that 1. changes t/global_destruction_forked.t, introducing a global variable, and 2. changes Try::Tiny to store @guards in a local()ized package variable Part 2 is required for perls < 5.20 due to https://rt.perl.org/Public/Bug/Display.html?id=119311. :-(
On Fri Feb 19 12:13:36 2016, MAUKE wrote: Show quoted text
> > I have a slightly disgusting fix that > 1. changes t/global_destruction_forked.t, introducing a global > variable, and > 2. changes Try::Tiny to store @guards in a local()ized package > variable > > Part 2 is required for perls < 5.20 due to > https://rt.perl.org/Public/Bug/Display.html?id=119311. :-(
Patch pushed to https://github.com/karenetheridge/Try-Tiny/pull/1. Since my last comment, I made t/global_destruction_forked.t a bit less ugly and added comments to lib/Try/Tiny.pm.
Thanks! https://github.com/karenetheridge/Try-Tiny/pull/1 has been merged and released as 0.25-TRIAL.