mojoshaneman has asked for the wisdom of the Perl Monks concerning the following question:

Here is my code:
--------------------------------------- perl1.pl --------------------------------------- use strict; use warnings; use v5.8; use threads; use threads::shared; use HTTP::Cookies; use LWP::UserAgent; use HTTP::Request::Common qw(POST); use HTTP::Request::Common qw(POST $DYNAMIC_FILE_UPLOAD); use URI::Escape; use perl_module1; ##################################################### # main() ##################################################### print("Starting LetsRock() \n"); LetsRock(); print("LetsRock() ended \n"); exit(0); ###################################################################### +############################# # LetsRock ###################################################################### +############################# sub LetsRock { my (@kids); for (my $x=0; $x < 5; $x++) { my ($kid) = Thread(\&ThreadFunc, 'https://www.wellsfargo.com') +; if (defined($kid)) { push(@kids, $kid); } sleep(1); } WaitForThreads(@kids); } ###################################################################### +############################# # ThreadFunc ###################################################################### +############################# sub ThreadFunc { my ($url) = @_; GetIt($url); } ###################################################################### +####### # Function: int Thread(ref function, string[] params) # # Description: # Spawn a thread to execute the specified function with optiona +l parameters # # Scope: public # # Parameters: # ref function - reference to function to call in thread # string[] params - optional params to pass to function # # Possible return values: id of spawned thread or undef ###################################################################### +####### sub Thread { my ($function, @params) = @_; my ($thread); if (defined(&{$function})) { $thread = threads->create($function, @params); } return($thread); } ###################################################################### +####### # Function: void WaitForThreads(int[] threads) # # Description: # Wait for all threads to end and return # # Scope: public # # Parameters: # int[] threads - array of thread ids # # Possible return values: none ###################################################################### +####### sub WaitForThreads { my (@threadArray) = @_; print("WaitForThreads() started \n"); foreach my $thread (@threadArray) { print("waiting for " . $thread->tid . "\n"); $thread->join(); } print("WaitForThreads() finished \n"); } ###################################################################### +####### # Function: void WaitForThread(int thread) # # Description: # Wait for a thread to end and return # # Scope: public # # Parameters: # int thread - thread id # # Possible return values: none ###################################################################### +####### sub WaitForThread { my ($thread) = @_; $thread->join; } --------------------------------------- perl_module1.pm --------------------------------------- package perl_module1; use strict; use v5.8; use HTTP::Cookies; use LWP::UserAgent; #use HTTP::Request::Common qw(POST); use HTTP::Request::Common; use URI::Escape; use vars qw(@ISA @EXPORT); use Exporter (); our @ISA = qw(Exporter); our @EXPORT = qw( GetIt ); our $DEBUG = 0; ###################################################################### +# # GetIt ###################################################################### +# sub GetIt { my ($url) = @_; my ($ua) = LWP::UserAgent->new(keep_alive => 1); $ua->timeout(60); # allow redirects for POST push @{ $ua->requests_redirectable }, 'POST'; # set the cookie my ($cookie) = './ac_cookie.txt' . $$; $ua->cookie_jar(HTTP::Cookies->new('file' => $cookie)); my ($response) = $ua->get($url); if ($response->is_success) { my ($page) = $response->content; print(); } } 1;
--------------------------------------- Here's my stack trace after the exception:
---------------------------------------

libeay32.dll!02858123()
ssleay32.dll!028055db()
ssleay32.dll!0280216e()
SSLeay.dll!027e1408()
perl58.dll!28041ee4()
perl58.dll!28060417()
perl58.dll!28025e9c()
perl58.dll!28025d44()
perl58.dll!28025ca4()
ntdll.dll!7c82f9dd()
msvcrt.dll!77bbcef6()
perl58.dll!280625fd()
perl58.dll!28067553()
perl58.dll!28067b2a()
perl58.dll!28062086()
perl58.dll!28062016()
perl58.dll!28061fb4()
perl58.dll!28023b0d()
perl58.dll!28023a23()
perl58.dll!28067897()
threads.dll!003e1158()
threads.dll!003e12be()
perl58.dll!28066e41()
threads.dll!003e1c60()
threads.dll!003e2322()
perl58.dll!28041ee4()
perl58.dll!28060417()
perl58.dll!28025e9c()
perl58.dll!28025d44()
perl58.dll!28025ca4()
perl58.dll!280625fd()
perl58.dll!28067553()
perl58.dll!28067b2a()
perl58.dll!28067814()
perl58.dll!28067b2a()
perl58.dll!28001bb5()
perl58.dll!28061a6b()
perl58.dll!280606c6()
perl58.dll!28041260()
perl58.dll!28060417()
perl58.dll!280257ac()
perl58.dll!28025663()
perl58.dll!2802550c()
perl58.dll!2808c909()
perl58.dll!2808c918()
ntdll.dll!7c832b78()
ntdll.dll!7c832b7f()
kernel32.dll!77e68e4c()
ntdll.dll!7c82fb23()
msvcrt.dll!77bbd08c()
msvcrt.dll!77bcbe2a()
perl.exe!00401012()
perl.exe!004010f9()
kernel32.dll!77e523cd()

Environment:
Windows 2003
Activestate Perl v5.8.7 built for MSWin32-x86-multi-thread
ActivePerl-Config 0.1: up to date.
ActivePerl-DocTools 1.0: up to date.
ActiveState-Utils 1.23: up to date.
ActiveState-WSProxy 1.8: up to date.
Archive-Tar 1.26.0.1: up to date.
Archive-Zip 1.16: up to date.
Compress-Zlib 1.41: up to date.
Crypt-SSLeay 0.51: up to date.
DBD-mysql 3.0002: up to date.
DBI 1.49: new version 1.50 available in ActiveState Package Repository
Data-Dump 1.6: up to date.
Digest-HMAC 1.1: up to date.
Digest-MD2 2.3: up to date.
Digest-MD4 1.5.0.1: up to date.
Digest-SHA1 2.10: up to date.
File-CounterFile 1.4: up to date.
Font-AFM 1.19: up to date.
HTML-Parser 3.46: up to date.
HTML-Tagset 3.4: up to date.
HTML-Tree 3.18: up to date.
IO-String 1.7: up to date.
IO-Zlib 1.4: up to date.
MD5 2.3: up to date.
MIME-Base64-Scripts 1.0: up to date.
NTLM 1.02: up to date.
SOAP-Lite 0.55.0.1: up to date.
Term-ReadLine-Perl 1.2.3.2: new version 1.0203 available in ActiveState Package Repository
TermReadKey 2.30: up to date.
Text-Autoformat 1.13: up to date.
Text-Reform 1.11: up to date.
Tk 804.27.0.4: up to date.
URI 1.35: up to date.
Unicode-String 2.9: up to date.
WebService-Perl 0.03: up to date.
XML-Parser 2.34.0.1: up to date.
XML-Simple 2.14: up to date.
libwww-perl 5.803.0.1: up to date.

The exception happens when returning from the LetsRock() function.

Edit: g0n - readmore tags

Replies are listed 'Best First'.
Re: Threads calling LWP causes exception
by BrowserUk (Patriarch) on Mar 10, 2006 at 01:22 UTC

    Does it still fail for you if you use a non-secure (https:) url?

    I ran your code against the home page of the beeb and got this output:

    C:\test>535553 Starting LetsRock() WaitForThreads() started waiting for 1 waiting for 2 waiting for 3 waiting for 4 waiting for 5 WaitForThreads() finished Scalars leaked: 1 Scalars leaked: 1 Scalars leaked: 1 Scalars leaked: 1 Scalars leaked: 1 LetsRock() ended

    I've had trouble with SSLeay on my system in the past without threads, but it's possible there is some interaction between them.

    If you can fetch non https urls without problems, it will give you (and us) somewhere to concentrate on looking for the problem.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks for the reply. I tried http://www.google.com and got the exception as well.

        Having just installed Crypt::SSleay from

        C:\Perl\packages>ppm install http://theoryx5.uwinnipeg.ca/ppms/Crypt-S +SLeay.ppd ==================== Install 'Crypt-SSLeay' version 0.51 in ActivePerl 5.8.6.811. ==================== [snip ...] Successfully installed Crypt-SSLeay version 0.51 in ActivePerl 5.8.6.8 +11.

        I ran my cut dwon version of your script against the https url you embedded in your code, and it ran through clean:

        C:\test>535553 https://www.wellsfargo.com Starting LetsRock() Waiting for 1 Waiting for 2 Waiting for 3 Waiting for 4 Waiting for 5 LetsRock() ended

        If you got your version of C::SSLeay from somewhere else or built it yourself you could try re-installing as above. If not, you could try regressing to AS811. I had various problems with AS815 and regressed to fix them. I'm awaiting 5.8.8 before I ungrade.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        Hmm. I'm using 5.8.6 and a simplified version of your script (simplified to try and track down where the scalar was being leaked from), and google also gives me no problems:

        C:\test>535553 http://google.com Starting LetsRock() Waiting for 1 Waiting for 2 Waiting for 3 Waiting for 4 Waiting for 5 LetsRock() ended

        Perhaps you could try my code and see what results you get?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.