Executive summary: What's the best way to propagate a signal received during a destructor when there are allocated resources outstanding?

Gory Details

I'm doing RAII in Perl. The script allocates a bunch of persistent resources (such as temporary files), each of which is represented by an object whose DESTROY method deallocates the resource when it goes out of scope. This is Good.

In order to promptly respond to signals while still freeing outstanding resources, I do something like this:

eval { local $SIG{INT} = sub { die "SIGINT\n" }; # Do all the resource allocation and work here. # All the resources have gone out of scope by the # end of this block. } if($@) { kill 'INT', $$ if $@ eq "SIGINT\n"; die $@; }
The problem is that Bad Things happen if a signal arrives inside a DESTROY method. (Because some of my resources can take seconds to free, this happens a lot in practice.) The issue is that (at least in Perl 5.6.1 and 5.8.2) a die inside a destructor terminates the destructor, but it does not propagate further. For example:
sub DESTROY { print "DESTROY\n"; die; print "HUH\n"; } { my $x = bless []; } print "DONE\n";
prints:
DESTROY DONE
This is understandable, because having your DESTROY methods die is fundamentally flawed (http://www.gotw.ca/gotw/047.htm). However, while it's possibly OK to leave the present object's resource behind, I still want all the other objects DESTROY'ed and the program to terminate immediately thereafter.

It seems to me that what's needed here is a way to defer signals until we are no longer inside a destructor, or at least a way for destructors to re-raise them without having them take effect while still in the destructor. I could do that by implementing deferred handling in Perl (as an additional layer over the existing deferred handling in the Perl 5.8 core), but OMFG that's ugly and fragile.

I can't believe that I'm the first person to run into this. What's the Right Way to deal with it?

&ers


In reply to Propagating a Signal from DESTROY by topnerd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.