Skip to content

Instantly share code, notes, and snippets.

@dakkar
Last active October 12, 2016 16:06
Show Gist options
  • Select an option

  • Save dakkar/2a45a87a5845353e3ad8f645a26ae616 to your computer and use it in GitHub Desktop.

Select an option

Save dakkar/2a45a87a5845353e3ad8f645a26ae616 to your computer and use it in GitHub Desktop.
A weird problem with sleep/alarm/die/eval
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
# this works in 5.8 and 5.24, fails under 5.18 5.20 and 5.22
sub do_it {
my ($call) = @_;
eval {
local $SIG{ALRM} = sub { die 'time out' };
alarm 1;
# the chained call is required to trigger the strange
# behaviour
$call->();
alarm 0;
return "ok";
};
if ($@) {
return "failed: $@";
};
}
sub test_it {
my ($call,$message) = @_;
subtest $message => sub {
local $@;
my $return = eval { do_it($call) };
my $err = $@;
is(
$err,
'',
'no exception should leak',
);
like(
$return,
qr{^failed:},
'the exception should be captured',
);
};
}
sub just_sleep { sleep 10 }
sub sleep_and_value { sleep 10; 1 }
# this works
test_it(
sub { just_sleep() },
'just a sleep',
);
# this works
test_it(
sub { sleep_and_value() },
'a sleep and another value',
);
# this fails
test_it(
sub { just_sleep()->chained },
'just a sleep, plus chained call',
);
# this works
test_it(
sub { sleep_and_value()->chained },
'a sleep and another value, plus chained call',
);
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment