While debugging a failed test for AnyEvent on Windows, I created the following script based on test 80_ssltest.t:
use feature qw(say);
use strict;
use warnings;
use AnyEvent::Socket;
use AnyEvent::Handle;
use AnyEvent::TLS;
my $ctx = AnyEvent::TLS->new( cert_file => $0 );
my $server_done = AnyEvent->condvar;
my $client_done = AnyEvent->condvar;
my $server_port = AnyEvent->condvar;
my $host = "127.0.0.1";
my $server = tcp_server(
$host,
undef, # service: must be either a service name or a numeric port number
# (or 0 or undef, in which case an ephemeral port will be used).
#
sub { # This is the accept callback..
my ($fh, $host, $port) = @_;
say "server_accept";
my $hd; $hd = AnyEvent::Handle->new(
#tls => "accept",
#tls_ctx => $ctx,
fh => $fh,
timeout => 8,
on_error => sub {
say "server_error <$_[2]>";
$server_done->send; undef $hd;
},
on_eof => sub {
say "server_eof";
$server_done->send; undef $hd;
}
);
$hd->push_read (
line => sub {
say "server got line <$_[1]>";
}
);
}, # end of accept callback..
sub { # This is the prepare callback
say "server_listen";
$server_port->send ($_[2]);
}
);
my $port = $server_port->recv; # At this point the server should be listening...
my $hd; $hd = AnyEvent::Handle->new(
connect => [$host, $port],
#tls => "connect",
#tls_ctx => $ctx,
timeout => 8,
on_connect => sub {
say "client_connect";
},
on_error => sub {
say "client_error <$_[2]>";
$client_done->send; undef $hd;
},
on_eof => sub {
say "client_eof";
$client_done->send; undef $hd;
}
);
$hd->push_write ("1\n");
say "Sleeping..";
sleep 1;
$hd->on_drain (sub {
say "client_drain";
$client_done->send;
undef $hd; # For some reason this does not send EOF to the server
});
say "Waiting for client done..";
$client_done->recv;
say "Waiting for server done..";
$server_done->recv;
__END__
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA02VwAqlQzCrPenkxUjawHcXzJreJ9LDhX7Bkg3E/RB6Ilm4D
LBeilCmzkY7avp57+WCiVw2qkg+kH4Ef2sd+r10UCGPh/1diLehRAzp3Ho1bixyg
w+zkDm79OnN3uHxuKigkAxx3GGz9HhQA83U+RUns+39/OnFh0RY6/f5rV2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AQH/MA0G
CSqGSIb3DQEBBQUAA4IBAQA/vY+qg2xjNeOuDySW/VOsStEwcaiAm/t24z3TYoZG
2ZzyKuvFXolhXsalCahNPcyUxZqDAekODPRaq+geFaZrOn41cq/LABTKv5Theukv
H7IruIFARBo1pTPFCKMnDqESBdHvV1xTOcKGxGH5I9iMgiUrd/NnlAaloT/cCNFI
OwhEPsF9kBsZwJBGWrjjVttU2lzMzizS7vaSIWLBuEDObWbSXiU+IdG+nODOe2Dv
W7PL43yd4fz4HQvN4IaZrtwkd7XiKodRR1gWjLjW/3y5kuXL+DA/jkTjrRgiH8K7
lVjm7gvkULRV2POQqtc2DUVXLubQmmGSjmQmxSwFX65t
-----END CERTIFICATE-----
This script hangs (Ubuntu 20.04, perl version 5.30), but if I use TLS handshake (uncomment the lines in the code with tls
and tls_ctx
) it works fine.
The output without TLS is:
server_listen
Sleeping..
Waiting for client done..
client_drain
server_accept
Waiting for server done..
server got line <1>
server_error <Connection timed out>
The output with TLS (after uncommenting the 4 lines starting with tls
and tls_ctx
) is:
server_listen
Sleeping..
Waiting for client done..
client_connect
server_accept
client_drain
Waiting for server done..
server got line <1>
server_eof
- Note that
on_connect
is not called (client_connect
is missing in the output) when not using TLS, - Even if the client is not connected when not using TLS, the server receives the data and prints
server got line <1>
... Very strange.
Any idea what is the problem when not using TLS?
From the looks of it, you are not reading from the socket/handle. Without reading from the socket, there is no way detect an EOF condition.
The reason why you get different behaviour with TLS is that TLS does not emulate socket semantics and AnyEvent::Handle must essentially always read, even if you don't have explicit read requests, therefore it will detect the EOF condition instantly.
You can check for this by adding an on_read handler, e.g. after or before your push_read in the server:
With this present, it should behave more or less the same with or without TLS.