Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

#!/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 ", Dum +per($response->decoded_content); print "Response code: ", $response->code, "\tHTTP Response Messag +e: ", $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 ", Dum +per($response->decoded_content); print "Response code: ", $response->code, "\tHTTP Response Messag +e: ", $response->message, " "; print threads->tid(); }); $b-> join;

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:

Process finished with exit code -1073741816 (0xC0000008) Process finished with exit code -1073741819 (0xC0000005) Process finished with exit code -1073741790 (0xC0000022)

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!

Edit: 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 non-thread GET request just to "plant the seed". Then the subsequent requests in threads or non-threads would run without problems. I'm using This is perl 5, version 24, subversion 3 (v5.24.3) built for MSWin32-x86-multi-thread-64int

Edit 2: Per @ikegami and @Corion's comments, I tried using an http uri instead of https 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 https.


In reply to HTTP Request and Threads by banhmi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found