I'm using perl 5.24.4 under Windows 10 that have the latest LibXML installed (2.0134). Below is the code that crashes on Windows 10 (It works on Linux)
crash.pl
use strict;
use warnings;
use XML::LibXML;
use Utils;
use Log::Log4perl;
Log::Log4perl->init('logger.conf');
my $logger = Log::Log4perl->get_logger();
sub generate_word_stats_for_chunk
{
return Utils::load_sql();
}
my $num_process = 4;
my @modes = (1,3,5);
foreach my $mode (@modes) {
my @pid_list;
$logger->info("Forking $num_process");
for my $i (0 .. $num_process - 1) {
my $pid = fork();
$logger->logconfess("Unable to fork $!") if not defined $pid;
if ($pid == 0) {
# child process
generate_word_stats_for_chunk($i);
sleep 2;
# make sure that the child process exit
exit;
}
# parent
# Store the PIDs, so that waitpid() can call them later
push @pid_list, $pid;
}
for my $pid (@pid_list) {
waitpid $pid, 0;
}
$logger->info("finished mode $mode");
Utils.pm
package Utils;
use strict;
use warnings;
use XML::LibXML;
use Log::Log4perl;
my $logger = Log::Log4perl->get_logger();
sub load_sql
{
$logger->info("parsing XML");
my $dom;
eval {
$dom = XML::LibXML->load_xml(location => 'sql.xml');
};
if ($@) {
$logger->error("parse error $@");
return;
}
$logger->info("finished parsing XML");
# dummy
return {};
}
# perl package ends with 1
1;
sql.xml
<?xml version="1.0" encoding="UTF-8"?>
<sqls>
<sql name="CREATE_COMMODITY_META">
test
</sql>
</sqls>
Running crash.pl gives me the following output
INFO main::(crash.pl:37) Forking 4
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:31) finished parsing XML
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:31) finished parsing XML
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:31) finished parsing XML
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:31) finished parsing XML
INFO main::(crash.pl:62) finished mode 1
INFO main::(crash.pl:37) Forking 4
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:21) parsing XML
INFO Utils::load_sql(Utils.pm:21) parsing XML
The code works if I remove the Utils::load_sql()
statement. This might be similar to Why does Perl crash when using LibXML after a fork?, but I have some doubts as the code in that post works only machine. My code actually works if you remove the outer loop (e.g. the foreach my $mode...
). Also another reason why I doubt it is that post is already 8 years old.
Also, why isn't the eval statement in Utils.pm
not catching the crash?