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



commit 6203874cd598290f73b5fbfd972a7fd6629acecc
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, 26 insertions(+), 7 deletions(-)
---
diff --git a/src/chess-engine.vala b/src/chess-engine.vala
index 8dee767..945e2f9 100644
--- a/src/chess-engine.vala
+++ b/src/chess-engine.vala
@@ -18,6 +18,7 @@ public abstract 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) {}
 
@@ -82,8 +83,10 @@ public abstract 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;
@@ -91,6 +94,7 @@ public abstract class ChessEngine : Object
 
     private void engine_stopped_cb (Pid pid, int status)
     {
+        Process.close_pid (pid);
         stopped ();
     }
 
@@ -104,17 +108,32 @@ public abstract class ChessEngine : Object
 
     public void stop ()
     {
-        if (stdout_watch_id > 0)
+        if (!started)
+            return;
+
+        Source.remove (stdout_watch_id);
+
+        try
         {
-            Source.remove (stdout_watch_id);
-            stdout_watch_id = 0;
+            stdout_channel.shutdown (false);
         }
-
-        if (pid != 0)
+        catch (IOChannelError e)
         {
-            Posix.kill (pid, Posix.SIGTERM);
-            Process.close_pid (pid);
+            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]