Skip to content

Commit

Permalink
retriever: Gracefully handle incomplete/broken request files
Browse files Browse the repository at this point in the history
Apparently the way the dCache ENDIT plugin writes request files can
cause the tsmretriever to barf on an invalid JSON file, for example if
the request file is opened for writing but content isn't written yet.

Work around this by catching errors and retrying a few times with 100ms
delay inbetween.
  • Loading branch information
ZNikke committed Mar 20, 2023
1 parent 530051e commit f4337e2
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions tsmretriever.pl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use JSON;
use File::Temp qw /tempfile/;
use File::Basename;
use Time::HiRes qw(usleep);

# Be noisy when JSON::XS is missing, consider failing hard in the future
BEGIN {
Expand Down Expand Up @@ -80,13 +81,24 @@ ($)
my $req_filename = $conf{dir} . '/request/' . $req;
my $state;

{
local $/; # slurp whole file
# If open failed, probably the request was finished or cancelled
open my $rf, '<', $req_filename or return undef;
my $json_text = <$rf>;
$state = decode_json($json_text);
close $rf;
return undef unless(-f $req_filename);

for(1..10) {
$state = undef;
eval {
local $SIG{__WARN__} = sub {};
local $SIG{__DIE__} = sub {};
local $/; # slurp whole file
# If open failed, probably the request was finished or
# cancelled.
open my $rf, '<', $req_filename or return undef;
my $json_text = <$rf>;
close $rf;
die "Zero-length string" if(length($json_text) == 0);
$state = decode_json($json_text);
};
last if(!$@);
usleep(100_000); # Pace ourselves
}

if(!$state || $state->{parent_pid} && getpgrp($state->{parent_pid})<=0)
Expand Down

0 comments on commit f4337e2

Please sign in to comment.