print "1..6\n";
-my $had_error = 0;
-END { $? = $had_error };
+our $had_error;
+
+# try to ensure this is the last-most END so we capture future tests
+# running in other ENDs
+require B;
+my $reinject_retries = my $max_retry = 5;
+my $end_worker;
+$end_worker = sub {
+ my $tail = (B::end_av()->ARRAY)[-1];
+ if (!defined $tail or $tail == $end_worker) {
+ $? = $had_error || 0;
+ $reinject_retries = 0;
+ }
+ elsif ($reinject_retries--) {
+ push @{B::end_av()->object_2svref}, $end_worker;
+ }
+ else {
+ print STDERR "\n\nSomething is racing with @{[__FILE__]} for final END block definition - can't win after $max_retry iterations :(\n\n";
+ require POSIX;
+ POSIX::_exit( 255 );
+ }
+};
+END { push @{B::end_av()->object_2svref}, $end_worker }
+
sub ok ($$) {
$had_error++, print "not " if !$_[0];
print "ok";