found location of hang; make annotations; added more log lines
[scpubgit/Object-Remote.git] / lib / Object / Remote / ReadChannel.pm
CommitLineData
12fb4a80 1package Object::Remote::ReadChannel;
2
3use CPS::Future;
4use Scalar::Util qw(weaken);
0511910e 5use Object::Remote::Logging qw(:log);
12fb4a80 6use Moo;
7
8has fh => (
9 is => 'ro', required => 1,
10 trigger => sub {
11 my ($self, $fh) = @_;
12 weaken($self);
0511910e 13 log_trace { "Watching filehandle via trigger on 'fh' attribute in Object::Remote::ReadChannel" };
12fb4a80 14 Object::Remote->current_loop
15 ->watch_io(
16 handle => $fh,
17 on_read_ready => sub { $self->_receive_data_from($fh) }
18 );
19 },
20);
21
22has on_close_call => (
23 is => 'rw', default => sub { sub {} },
24);
25
26has on_line_call => (is => 'rw');
27
28has _receive_data_buffer => (is => 'ro', default => sub { my $x = ''; \$x });
29
2d81cf18 30#TODO confirmed this is the point of the hang - sysread() is invoked on a
31#socket inside the controller that blocks and deadlocks the entire system.
32#The remote nodes are all waiting to receive data at that point.
33#Validated this behavior exists in an unmodified Object::Remote from CPAN
34#by wrapping this sysread() with warns that have the pid in them and pounding
35#my local machine with System::Introspector via ssh and 7 remote perl instances
36#It looks like one of the futures is responding to an event regarding the ability
37#to read from a socket and every once in a while an ordering issue means that
38#there is no actual data to read from the socket
12fb4a80 39sub _receive_data_from {
40 my ($self, $fh) = @_;
0511910e 41 log_trace { "Preparing to read data" };
2d81cf18 42 #use Carp qw(cluck); cluck();
12fb4a80 43 my $rb = $self->_receive_data_buffer;
2d81cf18 44 #TODO is there a specific reason sysread() and syswrite() aren't
45 #a part of ::MiniLoop? It's one spot to handle errors and other
46 #logic involving filehandles
47 #TODO why are the buffers so small? BUFSIZ is usually 32768
12fb4a80 48 my $len = sysread($fh, $$rb, 1024, length($$rb));
49 my $err = defined($len) ? '' : ": $!";
50 if (defined($len) and $len > 0) {
0511910e 51 log_trace { "Read $len bytes of data" };
12fb4a80 52 while (my $cb = $self->on_line_call and $$rb =~ s/^(.*)\n//) {
53 $cb->(my $line = $1);
54 }
55 } else {
0511910e 56 log_trace { "Got EOF or error, this read channel is done" };
12fb4a80 57 Object::Remote->current_loop
58 ->unwatch_io(
59 handle => $self->fh,
60 on_read_ready => 1
61 );
62 $self->on_close_call->($err);
63 }
64}
65
66sub DEMOLISH {
67 my ($self, $gd) = @_;
68 return if $gd;
0511910e 69 log_trace { "read channel is being demolished" };
12fb4a80 70 Object::Remote->current_loop
71 ->unwatch_io(
72 handle => $self->fh,
73 on_read_ready => 1
74 );
75}
76
771;