LibTmux/ProcessJob.pm

filter: Statementstmt 72.6% 109 / 150 Branchbran 37.5% 27 / 72 Conditioncond 21.7% 10 / 46 MC/DCmcdc 3.3% 1 / 30 Subroutinesub 89.4% 17 / 19 Podpod 100% 8 / 8 totaltotal 52.9% 172 / 325 CC 60 SCAR 37.1
CC
60
Cov
54%
__ANON__
106.4
cancel
56.0
finish
45.5
CRAP 40.9
« LibTmux/Filter.pm ↑ Summary LibTmux/Snapshot.pm »
1 package LibTmux::ProcessJob;
2 5 ▶use v5.44;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use LibTmux::Transport ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use LibTmux::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Scalar::Util qw(refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Errno qw(EINTR ECHILD);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use POSIX qw(_exit);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use MIME::Base64 qw(decode_base64 encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 my %active;
11  
12 7 ▶sub start ($class, %input) {
Subroutine start: called CC=6 SCAR=24.7
Pod documented
13 7 my %args = LibTmux::Transport->_arguments(%input);
14 7 my ($result_r, $result_w) = LibTmux::Transport::_pipe();
15 7 my ($alive_r, $alive_w) = LibTmux::Transport::_pipe();
16 7 my ($cancel_r, $cancel_w) = LibTmux::Transport::_pipe();
17 7 LibTmux::Transport::_nonblock($cancel_w);
18 7 $args{_cancel_r} = $cancel_r;
19 my $job = bless {
20 result_r => $result_r,
21 alive_w => $alive_w,
22 cancel_w => $cancel_w,
23 external_reaper => exists($args{spawner}),
24 7 max_wire => 3 * $args{max_output} + 131072,
25 wire => '',
26 finished => false
27 }, $class;
28 3 ▶ my $child = sub ($prepare = undef) {
Subroutine __ANON__: called CC=15 SCAR=46.7
29 3 close $result_r;
30 3 close $alive_w;
31 3 close $cancel_w;
32 3 for my $foreign (values %active) {
33 0 ▶ next unless $foreign;
Branch: 1 branch
BranchTrueFalseTotal
unless $foreign 0 0 0
34 0 for my $key (qw(result_r alive_w cancel_w)) {
35 0 my $fh = $foreign->{$key};
36 0 ▶ close $fh if $fh && defined fileno($fh);
Branch: 1 branch
BranchTrueFalseTotal
if $fh and defined fileno $fh 0 0 0
Condition0%0/3
$fh and defined fileno $fh
!ll&&!rl&&r
000
MC/DC0%0/2
$fh and defined fileno $fh
$fh defined fileno $fh
Truth table0%0/3
A and B
A= $fhB= defined fileno $fh
ABresult
0X0
100
111
37 }
38 }
39 3 $SIG{PIPE} = 'IGNORE';
40 3 $SIG{CHLD} = 'DEFAULT';
41 3 my $record;
42 3 my $preparing = true;
43 3 try {
44 3 ▶ $prepare->() if $prepare;
Branch: 1 branch
BranchTrueFalseTotal
if $prepare 0 3 3
45 3 $preparing = false;
46 3 $record =
47 LibTmux::Transport::_supervise(\%args, $alive_r, $result_w)
48 }
49 catch ($error) {
50 0 ▶ my $type =
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($error) && $error->isa('LibTmux::X') ? : 0 0 0
Condition0%0/3
builtin::blessed($error) and $error->isa("LibTmux::X")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($error) and $error->isa("LibTmux::X")
builtin::blessed($error) $error->isa("LibTmux::X")
Truth table0%0/3
A and B
A= builtin::blessed($error)B= $error->isa("LibTmux::X")
ABresult
0X0
100
111
51 Scalar::Util::blessed($error)
52 && $error->isa('LibTmux::X')
53 ? ref($error) =~ s/^LibTmux::X:://r
54 : 'Transport';
55 0 $record = {
56 cleanup_complete => $preparing,
57 error =>
58 {type => $type, message => "$error", dispatched => false}
59 };
60 }
61 0 my $wire = JSON::PP->new->utf8->encode($record);
62 0 my $offset = 0;
63 0 ▶ while ($offset < length($wire)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $wire) 0 0 0
64 0 my $n =
65 syswrite($result_w, $wire, length($wire) - $offset, $offset);
66 0 ▶ next if !defined($n) && $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n and $! == 4 0 0 0
Condition0%0/3
not defined $n and $! == 4
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $n and $! == 4
not defined $n $! == 4
Truth table0%0/3
not(A) and B
A= not defined $nB= $! == 4
ABresult
0X0
100
111
67 0 ▶ last unless defined($n) && $n > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n > 0 0 0 0
Condition0%0/3
defined $n and $n > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $n and $n > 0
defined $n $n > 0
Truth table0%0/3
A and B
A= defined $nB= $n > 0
ABresult
0X0
100
111
68 0 $offset += $n;
69 }
70 0 close $result_w;
71 0 _exit(0);
72 7 };
73 7 my $pid;
74 7 ▶ if ($args{spawner}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'spawner'}) { } 0 7 7
75 0 try { $pid = $args{spawner}->($child) }
76 0 catch ($error) { close $alive_w; die $error }
77 }
78 else {
79 7 $pid = fork();
80 7 ▶ LibTmux::X::Spawn->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 7 7
81 message => "fork supervisor: $!",
82 dispatched => false
83 ) unless defined $pid;
84 7 ▶ $child->() unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 3 4 7
85 }
86 4 ▶ LibTmux::X::Spawn->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid and $pid =~ /\A[1-9][0-9]*\z/u 0 4 4
Condition33%1/3
defined $pid and $pid =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
004
MC/DC0%0/2
defined $pid and $pid =~ /\A[1-9][0-9]*\z/u
defined $pid $pid =~ /\A[1-9][0-9]*\z/u
Truth table33%1/3
A and B
A= defined $pidB= $pid =~ /\A[1-9][0-9]*\z/u
ABresult
0X0
100
111
87 message => 'spawn adapter returned an invalid PID',
88 dispatched => false
89 ) unless defined($pid) && $pid =~ /\A[1-9][0-9]*\z/;
90 4 $job->{pid} = $pid;
91 4 close $result_w;
92 4 close $alive_r;
93 4 close $cancel_r;
94 4 $active{refaddr($job)} = $job;
95 4 weaken($active{refaddr($job)});
96 4 return $job;
97 }
98 4 ▶sub pid ($self) { $self->{pid} }
Subroutine pid: called CC=1 SCAR=0.0
Pod documented
99 0 ▶sub cleanup_complete ($self) { !!$self->{cleanup_complete} }
Subroutine cleanup_complete: not called CC=1 SCAR=6.9
Pod documented
100  
101 4 ▶sub take_result_handle ($self) {
Subroutine take_result_handle: called CC=2 SCAR=7.2
Pod documented
102 LibTmux::X::InvalidArgument->throw(
103 message => 'result handle was already transferred')
104 4 ▶ unless $self->{result_r};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"result_r"} 0 4 4
105 4 return delete $self->{result_r};
106 }
107  
108 0 ▶sub cancel ($self) {
Subroutine cancel: not called CC=7 SCAR=40.3
Pod documented
109 0 ▶ return if $self->{finished} || $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} or $self->{"cancelled"} 0 0 0
Condition0%0/3
$self->{"finished"} or $self->{"cancelled"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"finished"} or $self->{"cancelled"}
$self->{"finished"} $self->{"cancelled"}
Truth table0%0/3
A or B
A= $self->{"finished"}B= $self->{"cancelled"}
ABresult
1X1
011
000
110 0 $self->{cancelled} = true;
111 0 ▶ if ($self->{cancel_w} && defined fileno($self->{cancel_w})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cancel_w"} and defined fileno $self->{"cancel_w"}) 0 0 0
Condition0%0/3
$self->{"cancel_w"} and defined fileno $self->{"cancel_w"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"cancel_w"} and defined fileno $self->{"cancel_w"}
$self->{"cancel_w"} defined fileno $self->{"cancel_w"}
Truth table0%0/3
A and B
A= $self->{"cancel_w"}B= defined fileno $self->{"cancel_w"}
ABresult
0X0
100
111
112 0 local $SIG{PIPE} = 'IGNORE';
113 0 my $written;
114 0 ▶ do { $written = syswrite($self->{cancel_w}, 'c') }
Branch: 1 branch
BranchTrueFalseTotal
not defined $written and $! == 4 and do { do { $written = syswrite($self->{"cancel_w"}, "c") } } 0 0 0
Condition0%0/3
not defined $written and $! == 4
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $written and $! == 4
not defined $written $! == 4
Truth table0%0/3
not(A) and B
A= not defined $writtenB= $! == 4
ABresult
0X0
100
111
115 while (!defined($written) && $! == EINTR);
116 }
117 0 return;
118 }
119  
120 4 ▶sub consume ($self, $bytes) {
Subroutine consume: called CC=2 SCAR=7.2
Pod documented
121 LibTmux::X::Transport->throw(
122 message => 'supervisor result exceeds its wire limit',
123 dispatched => true
124 4 ▶ ) if length($self->{wire}) + length($bytes) > $self->{max_wire};
Branch: 1 branch
BranchTrueFalseTotal
if length($self->{"wire"}) + length($bytes) > $self->{"max_wire"} 0 4 4
125 4 $self->{wire} .= $bytes;
126 }
127  
128 4 ▶sub finish ($self) {
Subroutine finish: called CC=22 SCAR=38.2
Pod documented
129 4 ▶ if ($self->{finished}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"finished"}) 0 4 4
130 0 ▶ die $self->{error} if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
131 0 return $self->{result};
132 }
133 4 $self->{finished} = true;
134 4 for my $key (qw(alive_w cancel_w result_r)) {
135 12 my $fh = delete $self->{$key};
136 12 ▶ close $fh if $fh && defined fileno($fh);
Branch: 1 branch
BranchTrueFalseTotal
if $fh and defined fileno $fh 8 4 12
Condition66%2/3
$fh and defined fileno $fh
!ll&&!rl&&r
408
MC/DC50%1/2
$fh and defined fileno $fh
$fh defined fileno $fh
Truth table66%2/3
A and B
A= $fhB= defined fileno $fh
ABresult
0X0
100
111
137 }
138 4 delete $active{refaddr $self};
139 4 my $record;
140 4 try {
141 $record = JSON::PP->new->utf8->max_size($self->{max_wire})
142 4 ->decode(delete $self->{wire});
143 4 ▶ LibTmux::X::Transport->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $record eq "HASH" 0 4 4
144 message => 'invalid supervisor result',
145 dispatched => true
146 ) unless ref($record) eq 'HASH';
147 4 $self->{cleanup_complete} = !!$record->{cleanup_complete};
148 4 ▶ if ($record->{error} && !exists $record->{stdout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{"error"} and not exists $record->{"stdout"}) 0 4 4
Condition66%2/3
$record->{"error"} and not exists $record->{"stdout"}
!ll&&!rl&&r
220
MC/DC0%0/2
$record->{"error"} and not exists $record->{"stdout"}
$record->{"error"} not exists $record->{"stdout"}
Truth table66%2/3
A and not(B)
A= $record->{"error"}B= not exists $record->{"stdout"}
ABresult
0X0
100
111
149 0 my $error = $record->{error};
150 0 ▶ my $type = $error->{type} // 'Transport';
Condition0%0/2
$error->{"type"} // "Transport"
l!l
00
MC/DC0%0/1
$error->{"type"} // "Transport"
$error->{"type"}
Truth table0%0/2
A
A= $error->{"type"}
Aresult
11
00
151 0 ▶ $type = 'Transport' unless "LibTmux::X::$type"->isa('LibTmux::X');
Branch: 1 branch
BranchTrueFalseTotal
unless ("LibTmux::X::$type")->isa("LibTmux::X") 0 0 0
152 0 "LibTmux::X::$type"->throw(%$error);
153 }
154 4 for my $key (qw(stdout stderr exit_code signal raw_status)) {
155 LibTmux::X::Transport->throw(
156 message => "missing supervisor result $key",
157 dispatched => true
158 20 ▶ ) unless exists($record->{$key}) && !ref($record->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $record->{$key} and not ref $record->{$key} 0 20 20
Condition33%1/3
exists $record->{$key} and not ref $record->{$key}
!ll&&!rl&&r
0020
MC/DC0%0/2
exists $record->{$key} and not ref $record->{$key}
exists $record->{$key} not ref $record->{$key}
Truth table33%1/3
A and not(B)
A= exists $record->{$key}B= not ref $record->{$key}
ABresult
0X0
100
111
159 }
160 4 my %bytes;
161 4 for my $key (qw(stdout stderr)) {
162 LibTmux::X::Transport->throw(
163 message => "invalid supervisor $key encoding",
164 dispatched => true
165 8 ▶ ) unless defined($record->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record->{$key} 0 8 8
166 8 $bytes{$key} = decode_base64($record->{$key});
167 LibTmux::X::Transport->throw(
168 message => "invalid supervisor $key encoding",
169 dispatched => true
170 8 ▶ ) unless encode_base64($bytes{$key}, '') eq $record->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless &encode_base64($bytes{$key}, "") eq $record->{$key} 0 8 8
171 }
172 $self->{result} = bless {
173 stdout => $bytes{stdout},
174 stderr => $bytes{stderr},
175 exit_code => $record->{exit_code},
176 signal => $record->{signal},
177 raw_status => $record->{raw_status}
178 },
179 4 'LibTmux::CommandResult';
180 4 ▶ if ($record->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{"error"}) 2 2 4
181 2 my $error = $record->{error};
182 2 ▶ my $type = $error->{type} // 'Transport';
Condition50%1/2
$error->{"type"} // "Transport"
l!l
20
MC/DC0%0/1
$error->{"type"} // "Transport"
$error->{"type"}
Truth table50%1/2
A
A= $error->{"type"}
Aresult
11
00
183 2 ▶ $type = 'Transport' unless "LibTmux::X::$type"->isa('LibTmux::X');
Branch: 1 branch
BranchTrueFalseTotal
unless ("LibTmux::X::$type")->isa("LibTmux::X") 0 2 2
184 2 "LibTmux::X::$type"->throw(%$error, result => $self->{result});
185 }
186 }
187 catch ($error) {
188 $self->{error} =
189 2 ▶ Scalar::Util::blessed($error) && $error->isa('LibTmux::X')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($error) && $error->isa('LibTmux::X') ? : 2 0 2
Condition33%1/3
builtin::blessed($error) and $error->isa("LibTmux::X")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($error) and $error->isa("LibTmux::X")
builtin::blessed($error) $error->isa("LibTmux::X")
Truth table33%1/3
A and B
A= builtin::blessed($error)B= $error->isa("LibTmux::X")
ABresult
0X0
100
111
190 ? $error
191 : LibTmux::X::Transport->new(
192 message => 'supervisor exited without a valid result',
193 dispatched => true
194 );
195 2 die $self->{error};
196 }
197 2 return $self->{result};
198 }
199  
200 4 ▶sub wait ($self) {
Subroutine wait: called CC=6 SCAR=19.7
Pod documented
201 LibTmux::X::InvalidArgument->throw(
202 message => 'loop-owned jobs cannot use blocking wait')
203 4 ▶ if $self->{external_reaper};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"external_reaper"} 0 4 4
204 4 local ($!, $?);
205 4 my $fh = $self->take_result_handle;
206 4 try {
207 4 while (true) {
208 8 my $n = sysread($fh, my $chunk, 65536);
209 8 ▶ next if !defined($n) && $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n and $! == 4 0 8 8
Condition33%1/3
not defined $n and $! == 4
!ll&&!rl&&r
800
MC/DC0%0/2
not defined $n and $! == 4
not defined $n $! == 4
Truth table33%1/3
not(A) and B
A= not defined $nB= $! == 4
ABresult
0X0
100
111
210 8 ▶ LibTmux::X::Transport->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 8 8
211 message => "read supervisor: $!",
212 dispatched => true
213 ) unless defined $n;
214 8 ▶ last unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 4 4 8
215 4 $self->consume($chunk);
216 }
217 }
218 0 catch ($error) { $self->cancel; close $fh; $self->_reap; die $error }
219 4 close $fh;
220 4 $self->_reap;
221 4 return $self->finish;
222 }
223  
224 4 ▶sub _reap ($self) {
Subroutine _reap: called CC=4 SCAR=25.0
225 4 ▶ while (waitpid($self->pid, 0) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid($self->pid, 0) < 0) 0 4 4
226 0 ▶ last if $! == ECHILD;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 10 0 0 0
227 0 ▶ next if $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 0 0
228 0 last;
229 }
230 }
231  
232 4 ▶sub DESTROY ($self) {
Subroutine DESTROY: called CC=4 SCAR=15.2
233 4 delete $active{refaddr $self};
234 4 for my $key (qw(result_r alive_w cancel_w)) {
235 12 my $fh = $self->{$key};
236 12 ▶ close $fh if $fh && defined fileno($fh);
Branch: 1 branch
BranchTrueFalseTotal
if $fh and defined fileno $fh 0 12 12
Condition33%1/3
$fh and defined fileno $fh
!ll&&!rl&&r
1200
MC/DC0%0/2
$fh and defined fileno $fh
$fh defined fileno $fh
Truth table33%1/3
A and B
A= $fhB= defined fileno $fh
ABresult
0X0
100
111
237 }
238 }
239  
240 =head1 NAME
241  
242 LibTmux::ProcessJob - Shared owned process-supervision boundary
243  
244 =head1 METHODS
245  
246 =head2 start
247  
248 Start a supervised literal command using the same arguments as
249 L<LibTmux::Transport/run>. This explicit operation performs I/O.
250  
251 =head2 pid
252  
253 Return the supervisor PID. It differs from the program PID.
254  
255 =head2 cleanup_complete
256  
257 True only after a valid supervisor result proves owned program cleanup.
258  
259 =head2 take_result_handle
260  
261 Transfer the result pipe exactly once. Its reader owns closing that handle.
262  
263 =head2 consume
264  
265 Accumulate bounded result octets. Malformed or excessive data is a failure.
266  
267 =head2 finish
268  
269 Decode the complete result after EOF and supervisor exit. Return one
270 L<LibTmux::CommandResult|LibTmux::Transport/RESULTS> or throw a typed error.
271  
272 =head2 cancel
273  
274 Request cleanup without blocking. Cancellation can follow dispatch and does
275 not imply rollback or completed reaping.
276  
277 =head2 wait
278  
279 The synchronous reader/reaper. A loop-owned job refuses blocking wait.
280 Async adapters supply a spawn/reap callback and retain jobs through EOF plus
281 the matching exit notification. Dropping a job closes its parent guard but
282 does not synchronously wait; owners must complete their cleanup protocol.
283  
284 =cut
« LibTmux/Filter.pm ↑ Summary LibTmux/Snapshot.pm »