<p>Thinking about this more - I now understand what is happening and it is <strong>truly terrible</strong> - Devel::StackTrace is very seriously broken, by <em>potentially</em> causing all destructors caught in a frame to run an unlimited number of times (until perl itself nukes the loop from orbit).</p>

<p>The fix for this lies within Devel::StackTrace - it <strong>must</strong> be adjusted to detect it is constructing a stacktrace from within DESTROY and <strong>not</strong> create any new refs to the refaddr of the argument to any higher DESTROY frames. Consider the following small (hence contrived) example:</p>

<pre><code>perl -MDevel::StackTrace -e '
  sub DESTROY { warn "calling destructor and doing foo"; main::foo() };
  {
    my $trace;
    sub foo {
      $trace = Devel::StackTrace-&gt;new
    };

    { my $obj_to_be_destroyed = bless {} }

    print $trace-&gt;as_string
  }

  print "the end\n"
'
</code></pre>

<p>On why I qualify it as terrible - you already ran into an issue with a destructors written in a way that relies on it being a <em>highlander</em>. In this case the effect is a corrupted txn-state on a <code>$dbh</code>. There are many other cases where having a destructor execute twice will have worse consequences.</p>

<p>I am filing a Devel::StackTrace bug on RT with the above for <a href="https://github.com/autarch" class="user-mention">@autarch</a></p>

<p>Thanks a ton to <a href="https://github.com/nwellnhof" class="user-mention">@nwellnhof</a> for the initial investigative work that led to this diagnosis.</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">&mdash;<br>Reply to this email directly or <a href="https://github.com/dbsrgits/dbix-class/pull/63#issuecomment-60512767">view it on GitHub</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/AASeApxIABaUPs7CBorj9sLznUXrv_Ytks5nHMJhgaJpZM4Cx5oh.gif" width="1" /></p>
<script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"View this Pull Request on GitHub","action":{"@type":"ViewAction","url":"https://github.com/dbsrgits/dbix-class/pull/63#issuecomment-60512767","name":"View Pull Request"}}</script>