Re: [Vala] FileStream not closed after going out of scope



ok, here's a bit more meat. Hopefully this help to tackle the issue:

currently I'm using Vala 0.17.6. Obviously some files are left open as
the open file count is increasing (checking with lsof | wc -l ) until I
get the span error:

Error spawning Uptime process - Failed to create pipe for communicating
with child process (Too many open files)

thanks,



/* pipetest.vala
*  valac --pkg gio-2.0 --pkg posix pipetest.vala
*/

using GLib;


public class Pipetest.Main : Object {
        public MainLoop main_loop;
        public int counter = 0; 
        
        
        public  Main () {
                main_loop = new GLib.MainLoop (null, false);
        }

        
        
        public void run () {
                Timeout.add_seconds (1, get_uptime);
                }

        public bool get_uptime () {
                string uptime = "";
                int [] pipefd = {0, 0};
                
                
                if (Posix.pipe(pipefd) < 0) {
                        debug ("Error - unable to open pipe");
                }
                
                Posix.close(pipefd[1]); //closing the write end of pipe
                try {
                        string[] runme = {"/usr/bin/uptime"};
                        int input;
                        int output;
                        int error;
                        Pid child_pid;
                        bool res = Process.spawn_async_with_pipes (null, 
                                                                   runme,
                                                                   null, 
                                                                   SpawnFlags.DO_NOT_REAP_CHILD, 
                                                                   null, 
                                                                   out child_pid,
                                                                   out input,
                                                                   out pipefd[0],
                                                                   out error );

                        var filestream = FileStream.fdopen (pipefd[0], "r");
                        uptime = filestream.read_line ();
                        if (res) {
                                int child_status = 0;
                                int ret_waitpid = Posix.waitpid (child_pid, out child_status, 0);
                                if (ret_waitpid < 0 ) {
                                        debug ("Waitpid returned error code : %d", ret_waitpid);
                                }
                
                        Posix.close (pipefd[0]);
                        Process.close_pid (child_pid);
                                        
                        } 
                } catch (SpawnError e) {
                        debug ("Error spawning Uptime process - %s", e.message);
                }
        
                debug ("%d : Uptime : %s", counter, uptime);
                counter++;
                
                return true;
        }
        
                
        static int main (string[] args) {
                var main = new Main ();
                main.run ();
                main.main_loop.run ();
                return 0;
        }

}

-- 
tomw <tomw ubilix com>




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]