Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

IO::Socket::SSL + fork problem

by mrhyde (Novice)
on Nov 20, 2014 at 14:40 UTC ( [id://1107935]=perlquestion: print w/replies, xml ) Need Help??

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

I am writing a network client application which simply receives some signals from the server and returns responses.

The problem is when a forked child process is trying to write to the socket on the second call, IO::Socket::SSL simply closes the connection. Though, the first time it's called ( tick() ), it doesn't break the connection and returns a healthy response.

The problem exists:
1) only on Linux, it works fine on Windows.
2) only with IO::Socket::SSL, it works fine with IO::Socket::INET

minimized code to reproduce the problem:
use strict; use warnings; use IO::Socket::SSL qw(debug4); my $_pid; my $connection = IO::Socket::SSL->new( PeerHost => 'local.btc-node', PeerPort => 7566, Proto=> 'tcp', Timeout => 8, SSL_verify_mode => 0x00 ) or die($!); while(<$connection>) { if(/action1/) { # do something, no problem } elsif(/tick\s+([^\d\s\t]+)/) { my $currency = $1; tick ($connection, $currency); } } sub tick { my $socket = shift; my $currency = shift; if ($_pid = fork) { waitpid($_pid, 0); } else { if (fork) { exit; } else { my $response =`tick $currency 2>&1 3>&1`; $socket->print($response . "\n"); # without this line $con +nection doesn't break on the second call exit; } } }
I suppose there is a socket reference being missed, or something similar. What am I doing wrong?

Replies are listed 'Best First'.
Re: IO::Socket::SSL + fork problem
by Anonymous Monk on Nov 20, 2014 at 17:51 UTC

    Well, you cannot fork an SSL socket. It's a protocol layer with internal state. The first write would modify the state but that isn't reflected in your main process. You're going to need some event-driven framework.

      Is it possible to realize with standard IO modules?

        The probable reason it works on windows and not linux is that fork under windows is really just a thread, so the "forked child" is actually still a part of the same process, (which according to anonymonk above is important to ssl connections?).

        That suggests a possible way forward would be to use threads rather than fork, which would (might) get around the problem on both platforms. Of course, it might create others.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: IO::Socket::SSL + fork problem
by mrhyde (Novice) on Nov 21, 2014 at 04:50 UTC
    use strict; use warnings; use IO::Select; use IO::Socket::SSL; use threads; use threads::shared; my %misc :shared; my @dout :shared; my %outputs :shared; threads->create('pool'); my $connection = IO::Socket::SSL->new(PeerHost => 'local.btc-node', PeerPort => 7566, Proto=> 'tcp') + or die($!); my $_sel = IO::Select->new; $_sel->add($connection); $connection->blocking(0); while(1) { my ($readable, undef, undef) = IO::Select->select($_sel, undef, $_se +l, 0.21); if(scalar keys %outputs > 0) { foreach my $__a (keys %outputs){ foreach my $_line (@dout){ $_line =~ s/[\r\n]+//g; next if $_line eq ''; $connection->print("$_line\n"); } delete $outputs{$__a}; } } foreach my $socket (@$readable) { my $n = sysread($socket,my $msg,4096); if(defined $n) { if($msg =~ /tick\s+([^\d\s\t]+)/) { my $currency = $1; tick ($currency); } } } } sub tick { my $currency = shift; $misc{arg} = $currency; } sub pool { while(1){ my $thread = threads->create(t) if defined $misc{arg}; select(undef, undef, undef, 0.52); foreach(threads->list){ undef $misc{arg}; $_->detach(); } } } sub t{ @dout="hi $misc{arg}"; $outputs{int(rand 500)}=1; }
Re: IO::Socket::SSL + fork problem
by noxxi (Pilgrim) on Nov 23, 2014 at 09:44 UTC
    Since the issue was also reported as a bug for IO::Socket::SSL: You can find a detailed explanation what the reason for the behavior is and why this is not a bug in IO::Socket::SSL but a bug in the application at the github repository for IO::Socket::SSL.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1107935]
Approved by jfroebe
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-24 04:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found