perlquestion
banhmi
<p>Hi all. I'm aware Perl and threading don't have the best relationship, but this crash has been bugging me for a while. I'm wondering if anyone has encountered similar issues, or know what is going on here.</p>
<code>
#!/usr/bin/perl -l
use strict;
use warnings FATAL => 'all';
use Data::Dumper;
use threads;
use threads::shared;
use Thread::Queue;
use HTTP::Headers;
use LWP::UserAgent;
my $counter_thread : shared;
my $a = threads->create(sub () {
my $method = "GET";
my $uri = "https://www.google.com/";
my $header = HTTP::Headers->new();
$header->push_header("Content-Type" => "application/json");
my $ua = LWP::UserAgent->new;
$ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00);
my $request = HTTP::Request->new();
$request->method($method);
$request->uri($uri);
$request->headers($header);
my $response = $ua->request($request);
print $counter_thread++, "th :", "Dumped Data Returned:\n\n ", Dumper($response->decoded_content);
print "Response code: ", $response->code, "\tHTTP Response Message: ",
$response->message, " ";
print threads->tid();
});
$a-> join;
my $b = threads->create(sub () {
my $method = "GET";
my $uri = "https://www.google.com/";
my $header = HTTP::Headers->new();
$header->push_header("Content-Type" => "application/json");
my $ua = LWP::UserAgent->new;
$ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00);
my $request = HTTP::Request->new();
$request->method($method);
$request->uri($uri);
$request->headers($header);
my $response = $ua->request($request);
print $counter_thread++, "th :", "Dumped Data Returned:\n\n ", Dumper($response->decoded_content);
print "Response code: ", $response->code, "\tHTTP Response Message: ",
$response->message, " ";
print threads->tid();
});
$b-> join;
</code>
<p>Basically, I want to create two threads that do a similar GET HTTP request and print the response out (please ignore the clumsy code, I tried to repro the crash in a rush). The first thread would run fine. But the second thread would always cause a crash with one of these errors:</p>
<code>Process finished with exit code -1073741816 (0xC0000008)
Process finished with exit code -1073741819 (0xC0000005)
Process finished with exit code -1073741790 (0xC0000022)
</code>
<p>I'm pretty sure one time both of the threads work, so the issue might be intermittent. I haven't had much luck looking for any clues. If anyone could point me in the right direction, I'd appreciate it greatly! </p>
<p><b>Edit:</b> I've been spending some time looking more at this. What I noticed is for my machine, specifically the fail conditions are, the first request sent by the user agent has to be in a thread that's joined right after its created. On the second request, thread or non-thread, it will mostly crash (around 98% for me, there were 3-4 runs that worked randomly). That said, a quick workaround I'm implementing right now is sending a simple <b>non-thread</b> GET request just to "plant the seed". Then the subsequent requests in threads or non-threads would run without problems.
I'm using <code>This is perl 5, version 24, subversion 3 (v5.24.3) built for MSWin32-x86-multi-thread-64int</code></p>
<p><b>Edit 2:</b> Per @ikegami and @Corion's comments, I tried using an <code>http</code> uri instead of <code>https</code> and that seems to run fine. It might be the case that SSL libraries (which are not pure Perl) are only loaded if we use <code>https</code>.</p>