fclose(): 18 is not a valid stream resource

14,185

What about checking if the resource is still a resource before trying to close it:

foreach ($pipes as $pipe) {
  // Finds whether a variable is a resource
  if(is_resource($pipe)) {
    fclose($pipe);
  }
}
Share:
14,185
Kapil Kaushik
Author by

Kapil Kaushik

Updated on June 26, 2022

Comments

  • Kapil Kaushik
    Kapil Kaushik almost 2 years

    I am trying to execute a process using proc_open. The I/O for the process is handled by the pipes !!

    $descriptorspec = array(
        0 => array("pipe", "r"),
        1 => array("pipe", "w"),
        2 => array("pipe", "w")
    );
    

    Now, as it happens, sometimes the "c program" that I have opened does get stuck, and I have added a max_time_limit check which would forcibly shut down the process. I have added callback function -- namely onExit -- (using "call_user_function") to process the information whenever the "process exits" in a valid manner or by force.

    In the "exit" function, I am closing the i/o pipes

     foreach ($pipes as $pipe) {
        fclose($pipe);
     }
    

    The above works perfectly fine if the process has had a valid exit. However, in the case when I forcibly killed my process, I called proc_terminate to do so. I understand that the termination of process would also go ahead and close any I/O pipes, but the problem is that due to callback, my "onExit" function process always gets called (and this is the way I want it to be...as I need to do some more processing). Now, in this case, as I try to close the pipes, I get the following error:

    fclose(): 18 is not a valid stream resource 
    

    I tried using "ftell" to check whether the pipe is valid or not, but that too errored out. How do I check whether the pipes have already been closed or not??