[pve-devel] [PATCH common v2] run_fork_with_timeout: allow returning undef

Thomas Lamprecht t.lamprecht at proxmox.com
Tue Sep 12 12:09:36 CEST 2017


On 09/12/2017 11:35 AM, Dietmar Maurer wrote:
> 
> would it be more generic to decode/encode the data as JSON?
> 
> print encode_json({ res => $res });
> ...
> 
> That way we can also return complex data structures, for example
> exceptions.
> 
> 
> 	eval {
> 	    $res = $sub->();
> 	    print {$pipe_out} encode_json({ data => $res });
>          }
>          if (my $err = $@) {
> 	    print {$pipe_out} encode_json({ error => "$err" });
>          }
> 
> We can completely avoid the additional $pipe_err that way.
> 

Yes, seems much nicer, thanks for the idea!

>> On September 12, 2017 at 10:28 AM Thomas Lamprecht <t.lamprecht at proxmox.com>
>> wrote:
>>
>>
>> allow also to return undef without any warnings to our caller.
>> This avoids a "use of unitialised variable ..." warning
>>
>> Add a single char at the start which allows us to differ between
>> returning undef and returning all other values.
>>
>> Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
>> ---
>>
>> changes v1 -> v2:
>>   * methods should be allowed to return undef when run with
>>     run_fork_with_timeout so fix the problem at the source
>>
>>   src/PVE/Tools.pm | 4 +++-
>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
>> index 81662b1..3c73782 100644
>> --- a/src/PVE/Tools.pm
>> +++ b/src/PVE/Tools.pm
>> @@ -926,7 +926,8 @@ sub run_fork_with_timeout {
>>   
>>   	eval {
>>   	    $res = $sub->();
>> -	    print {$pipe_out} "$res";
>> +	    # allows to pass undef to parent
>> +	    print {$pipe_out} defined($res) ? "1$res" : "0";
>>   	    $pipe_out->flush();
>>   	};
>>   	if (my $err = $@) {
>> @@ -943,6 +944,7 @@ sub run_fork_with_timeout {
>>       my $readvalues = sub {
>>   	local $/ = undef;
>>   	$res = <$pipe_out>;
>> +	$res = undef if !substr ($res, 0, 1, '');
>>   	$error = <$pipe_err>;
>>       };
>>       eval {
>> -- 
>> 2.11.0
>>
>>
>> _______________________________________________
>> pve-devel mailing list
>> pve-devel at pve.proxmox.com
>> https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel





More information about the pve-devel mailing list