Changeset 6692

Show
Ignore:
Timestamp:
06/27/09 11:22:15 (9 months ago)
Author:
dbaelde
Message:

More exception handling in speech synthesis. We've had a case where fork failed with ENOMEM, it shouldn't crash liquidsoap anymore.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/liquidsoap/src/protocols/say.ml

    r6344 r6692  
    4343let say s ~log maxtime = 
    4444  let local = Filename.temp_file "say" ".wav" in 
     45  (* Note that if liquidsoap gets killed while resolving this URI, 
     46   * the empty tempfile remains. It is only cleaned if a temporary 
     47   * indicator is successfully created from it and added to the request. *) 
    4548  let cmd = Configure.tts_program in 
    4649  let voice,s = parse_arg s in 
    47   let pid,ret = 
    48     dlog#f 3 "Synthetizing %S to %S" s local ; 
    49     let pid = flush_all () ; fork () in 
    50       if pid = 0 then ( 
    51         try 
    52           Sys.set_signal 
    53             Sys.sigalrm (Sys.Signal_handle (fun _ -> core_exit 2)) ; 
    54           assert (0 = Unix.alarm (int_of_float (maxtime -. time ()))) ; 
    55           if voice <> "" then 
    56             execv cmd [| cmd;s;local;voice |] 
    57           else 
    58             execv cmd [| cmd;s;local |] 
    59         with 
    60           | _ -> core_exit 1 
    61       ) else 
    62         waitpid [] pid 
    63   in 
    64     if ret = Unix.WEXITED 0 then ( 
    65       [Request.indicator ~temporary:true local] 
    66     ) else ( 
    67       log "Speech synthesis failed !" ; 
    68       ( try Unix.unlink local with _ -> () ) ; 
    69       [] 
    70     ) 
     50    try 
     51      let pid,ret = 
     52        dlog#f 3 "Synthetizing %S to %S" s local ; 
     53        let pid = flush_all () ; fork () in 
     54          if pid = 0 then begin 
     55            try 
     56              Sys.set_signal 
     57                Sys.sigalrm (Sys.Signal_handle (fun _ -> core_exit 2)) ; 
     58              assert (0 = Unix.alarm (int_of_float (maxtime -. time ()))) ; 
     59              if voice <> "" then 
     60                execv cmd [| cmd;s;local;voice |] 
     61              else 
     62                execv cmd [| cmd;s;local |] 
     63            with 
     64              | _ -> core_exit 1 
     65          end else 
     66            waitpid [] pid 
     67      in 
     68        if ret = Unix.WEXITED 0 then 
     69          [Request.indicator ~temporary:true local] 
     70        else 
     71          failwith "synthesis script returned an error" 
     72    with 
     73      | e -> 
     74          (* This could for example be ENOMEM raised by Unix.fork. *) 
     75          dlog#f 3 
     76            "Failed to synthetize speech: %s!" 
     77            (match e with Failure s -> s | _ -> Printexc.to_string e) ; 
     78          log "Speech synthesis failed!" ; 
     79          (try Unix.unlink local with _ -> ()) ; 
     80          [] 
    7181 
    7282let () =