I've now fully diagnosed the issue and have a small reproducible test case. The issue is the context stack being reallocated when deeply nests sub calls are made from after_runtime(), which is called from a destructor while exiting the eval scope from the require. That exiting code isn't expecting the context stack to be reallocated from under it. Normally when perl itself calls out to code from things like destructors, tied method calls etc, it uses a new temporary set of stacks (argument, context etc). after_runtime() needs to do something similar. This is achieved via the PUSHSTACKi() macro. Look at a distribution like Async-Interrupt for an example.
Here's the reproducible code. The recursive sub is called enough times to trigger a context stack grow/realloc.
-------------------------
test.pl:
use lib '.';
use Foo;
-------------------------
Foo.pm:
package Foo;
use Bar;
1;
-------------------------
Bar.pm:
package Bar;
use B::Hooks::AtRuntime 'after_runtime';
sub recurse {
my $depth = shift;
return if $depth < 0;
recurse($depth -1);
}
sub import {
after_runtime {
recurse(20);
}
}
If this is run against a perl that has been built with -DDEBUGGING, you'll see the following assertion failure, which is Perl_cx_popeval() detecting that the current cx pointer has changed underneath it.
perl: inline.h:2921: Perl_cx_popeval: Assertion `CxTYPE(cx) == CXt_EVA
+L' failed.
Dave.