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



commit 8e79a1777cac3d8cb45c985fe125cb51e360aaca
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 |   32 +++++++++++++++++++++++++-------
 1 files changed, 25 insertions(+), 7 deletions(-)
---
diff --git a/src/chess-engine.vala b/src/chess-engine.vala
index 07ca797..fba1926 100644
--- a/src/chess-engine.vala
+++ b/src/chess-engine.vala
@@ -19,6 +19,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) {}
 
@@ -83,8 +84,10 @@ public abstract class ChessEngine : Object
         {
             warning ("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;
@@ -106,17 +109,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);
-            pid = 0;
+            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]