[gnome-chess/gnome-3-8] ChessEngine: fix minor file descriptor leaks



commit 8e42a1ffcf6d1b0a772aba64c849252bd5e6edc4
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Sat Jan 11 15:43:12 2014 -0600

    ChessEngine: fix minor file descriptor leaks

 src/chess-engine.vala |   33 +++++++++++++++++++++++++++++----
 1 files changed, 29 insertions(+), 4 deletions(-)
---
diff --git a/src/chess-engine.vala b/src/chess-engine.vala
index 1283313..4b63df3 100644
--- a/src/chess-engine.vala
+++ b/src/chess-engine.vala
@@ -8,6 +8,7 @@ public class ChessEngine : Object
     private int stderr_fd;
     private IOChannel stdout_channel;
     private uint stdout_watch_id;
+    private bool started = false;
 
     protected virtual void process_input (char[] data) {}
 
@@ -59,8 +60,10 @@ public class ChessEngine : Object
         {
             stderr.printf ("Failed to set input from chess engine to non-blocking: %s", e.message);
         }
+        stdout_channel.set_close_on_unref (true);
         stdout_watch_id = stdout_channel.add_watch (IOCondition.IN, read_cb);
 
+        started = true;
         starting ();
 
         return true;
@@ -68,6 +71,7 @@ public class ChessEngine : Object
 
     private void engine_stopped_cb (Pid pid, int status)
     {
+        Process.close_pid (pid);
         stopped ();
     }
 
@@ -89,11 +93,32 @@ public class ChessEngine : Object
 
     public void stop ()
     {
-        if (stdout_watch_id > 0)
-            Source.remove (stdout_watch_id);
+        if (!started)
+            return;
 
-        if (pid != 0)
-            Posix.kill (pid, Posix.SIGTERM);        
+        Source.remove (stdout_watch_id);
+
+        try
+        {
+            stdout_channel.shutdown (false);
+        }
+        catch (IOChannelError e)
+        {
+            warning ("Failed to close channel to engine's stdout: %s", e.message);
+        }
+
+        if (FileUtils.close (stdin_fd) == -1)
+            warning ("Failed to close pipe to engine's stdin: %s",
+                     strerror (errno));
+
+        if (FileUtils.close (stderr_fd) == -1)
+            warning ("Failed to close pipe to engine's stderr: %s",
+                     strerror (errno));
+
+        if (Posix.kill (pid, Posix.SIGTERM) == -1)
+            warning ("Failed to kill engine: %s", strerror (errno));
+
+        started = false;
     }
 
     private bool read_cb (IOChannel source, IOCondition condition)


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