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

Hi I'm a very new perl programmer and I'm writing my first perl code, however I'm having a few problems. The first was related with my actual problem, I have a code that connects to different ports and check for SOCKS, however it took very long to return if the server was not a SOCKS server, also I need to use threads to make it less slow and scan multiple services at the same time. The suggestion was to use alarm() and it worked fine. http://www.perlmonks.org/index.pl?node_id=838963 Well, it worked fine in a single test script without threads, when I start to use threads on my real code all get messed up. I get all the time a error "Alarm clock" and the process die / killed, however I never defined a entry with this output "Alarm clock". My code is like that:
#!/usr/bin/perl use Socket; use IO::Socket; use Net::SOCKS; use threads; use threads::shared; my ($host, $port) = @ARGV; mfunc(); $port = 1080; mfunc(); $port = 80; mfunc(); for my $lthrd (1..7){ my $nthrd = threads->new(\&mfunc); foreach (threads->list){ $_->join; } } $port = 22; mfunc(); sub mfunc{ eval { local $SIG{ALRM} = sub { die "alarm\n" }; # NB \n required alarm 7; my $sock = new Net::SOCKS(socks_addr => $host, socks_port => $port, pr +otocol_version => 4); my $rsocks = $sock->connect(peer_addr => 'www.google.com', peer_port = +> 80); my $status = $sock->param('status_num'); if ($status == SOCKS_OKAY){ print "\nWorked $host:$port\n"; $sock->close(); } alarm 0; }; die if $@ && $@ ne "alarm\n"; # propagate errors if ($@) { print("\nTimeout $host:$port\n"); } }
If you try you will get ./test.pl www.google.com 1080 Test 01 Timeout www.google.com:1080 Timeout www.google.com:3128 Alarm clock It proves that the code works fine out of the thread, but on the threads it get completely crazy. I saw this possible suggestion to fix the problem: http://www.mail-archive.com/perl5-porters@perl.org/msg89360.html However, now the code gives this error and back to be very slow again, probable when the first alarm() happen it get unhanded and never more work. SIGALRM handler "a" not defined. I can't believe there is no way to fix it in perl, can someone please help me in fix this code (examples are very welcome) to work with the threads and with the alarm()? Or any other approach that make the code fast and not waiting 5 minutes per connection that is not SOCKS... Thank you

Replies are listed 'Best First'.
Re: Threads and ALARM() - There is a solution or workaround?
by ikegami (Patriarch) on May 10, 2010 at 17:02 UTC

    Alarm uses signals. Signals are sent to processes, not threads. Also, it means you can only have one alarm in effect at a time. If you want to use alarm, you can't use threads.

    In your case, when one thread exits, it resets the signal handler for everyone, making the alarm fatal. Your shell reports that your program died from the alarm signal by printing "Alarm clock".

    $ perl -e'alarm(1); sleep' Alarm clock
      Thanks for answer. But there is no solution? I need to have a slow program? It's kind of disappointing. There is another option instead of alarm() to timeout my call to Net::SOCKS? Some workaround as select() in C? Maybe threads queue? I'm very new to perl, but I can't believe that a simple task like that is unsolved. Thank you

        In general, you could avoid alarm by using select with a timeout, or you could avoid using threads by forking instead.

        The Net::SOCKS object might by a selectable file handle.