File Coverage

blib/lib/AnyEvent/Task/Util.pm
Criterion Covered Total %
statement 22 23 95.6
branch 5 8 62.5
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 31 36 86.1


line stmt bran cond sub pod time code
1             package AnyEvent::Task::Util;
2              
3 29     29   116 use common::sense;
  29         30  
  29         178  
4              
5 29     29   1207 use AnyEvent::Util;
  29         41  
  29         7540  
6              
7              
8             our @children_sockets;
9              
10             sub fork_anyevent_subprocess {
11 28     28 0 56 my ($code, %args) = @_;
12              
13 28         106 my ($socka, $sockb) = AnyEvent::Util::portable_socketpair;
14              
15 28 50       1224 die "No AnyEvent watchers should be created prior to forking (AE model: $AnyEvent::MODEL)" if defined $AnyEvent::MODEL;
16 28         19387 my $pid = fork;
17              
18 28 50       926 die "couldn't fork: $!" if !defined $pid;
19              
20 28 100       809 if (!$pid) {
21 14         494 close($socka);
22              
23 14         144 AnyEvent::Util::close_all_fds_except 0, 1, 2, fileno($sockb), @{$args{dont_close_fds}};
  14         762  
24              
25             ## If parent closes its side of the socket we should exit
26 14     14   7745 my $watcher = AE::io $sockb, 0, sub { exit };
  14         19396705  
27              
28 14         60489 $code->();
29              
30 0         0 die "AnyEvent::Task::Server->run should never return";
31             }
32              
33 14         242 close $sockb;
34              
35 14 50       158 return ($socka, $pid) if wantarray;
36              
37 14         171 push @children_sockets, $socka; # keep reference alive
38 14         730 return;
39             }
40              
41              
42              
43             1;