noxxi / p5-io-socket-ssl

IO::Socket::SSL Perl Module
36 stars 59 forks source link

Trouble with Mojo::IOLoop::TLS #59

Closed Logioniz closed 7 years ago

Logioniz commented 7 years ago

I have error when trying to do two concurrent requests which upgrades to tls.

#!/usr/bin/perl
use Mojo::Base -strict;

use Mojo::IOLoop;
use Mojo::IOLoop::TLS;
use Mojo::IOLoop::Client;
use Mojo::IOLoop::Server;

#$IO::Socket::SSL::DEBUG = 3;

my $server = Mojo::IOLoop::Server->new;
my $client = Mojo::IOLoop::Client->new;

my ($server_handle, $client_handle);
my ($client_stream, $server_stream);

sub upgrade_handle {
  my ($handle, $is_server, $cb) = @_;
  my $tls = Mojo::IOLoop::TLS->new($handle);
  $tls->on(upgrade => sub { $cb->(pop) });
  $tls->on(error => sub { warn pop });
  $tls->negotiate(server => $is_server);
}

sub upgrade_handles {
  Mojo::IOLoop->delay(
    sub {
      my $d = shift;
      upgrade_handle($server_handle, 1, $d->begin(0));
      upgrade_handle($client_handle, 0, $d->begin(0));
    }, sub {
      my ($d, $server_handle, $client_handle) = @_;

      say 'YEAH';

      Mojo::IOLoop->singleton->reactor->io($server_handle => sub {})->watch($server_handle, 1, 0);
      Mojo::IOLoop->singleton->reactor->io($client_handle => sub {})->watch($client_handle, 1, 0);
    }
  );
}

$client->on(connect => sub {
  $client_handle = pop;
  #upgrade_handles();
  Mojo::IOLoop->timer(0.1 => sub { upgrade_handles() });
});

$client->on(error => sub { warn pop });

$server->on(accept => sub {
  $server_handle = pop;
  $client->connect(address => 'www.yandex.ru', port => 443);
});

$server->listen(port => 8443);
$server->start;
Mojo::IOLoop->start;

I have this errors when run this script

$ perl 9.pl 
SSL accept attempt failed error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
SSL accept attempt failed error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
SSL accept attempt failed error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
SSL accept attempt failed error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
SSL accept attempt failed error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
SSL accept attempt failed error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
SSL accept attempt failed error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
SSL accept attempt failed error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
SSL accept attempt failed error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
YEAH
^C
$ 

Client send message with that request

$ curl -k "https://127.0.0.1:8443"

I try to discuss this problem in mojo irc channel, but nobody understand me.

When i enable debug mode for IO::Socket::SSL, i see output like that:

connect -> -1
accept -> -1
accept -> -1
accept -> 1
handshake done, socket ready
connect -> -1
local error: SSL accept attempt failed error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
accpet -> -1
local error: SSL accept attempt failed error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
accept -> -1
connect -> -1
connect -> 1
ssl handshake done

So, after accept ssl handshake IO::Socket::SSL begin accept it again.

If comment client upgrade or server upgrade then all will work perfectly. So, problem occure when exists two concurrent requests.

I don't konw what to do...

noxxi commented 7 years ago

This looks for me like a bug in Mojolicious. I've filed an issue: https://github.com/kraih/mojo/issues/1102

Logioniz commented 7 years ago

Very very big thanks. You are my hero! Sorry for your time.