diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog
index 68a07be91..a8d8afa23 100644
--- a/winsup/cygwin/ChangeLog
+++ b/winsup/cygwin/ChangeLog
@@ -1,3 +1,14 @@
+2006-06-03  Christopher Faylor  <cgf@timesys.com>
+
+	* fhandler_tty.cc (fhandler_pty_master::close): Don't close handles if
+	we don't own them.
+	(fhandler_pty_master::setup): Make sure that original handle is closed
+	when changing inheritance.
+	(fhandler_pty_master::fixup_after_fork): Set from_master/to_master to
+	arch value always.
+	(fhandler_pty_master::fixup_after_exec): Clear from_master/to_master
+	when close_on_exec.
+
 2006-06-03  Christopher Faylor  <cgf@timesys.com>
 
 	* cygheap.cc (init_cygheap::close_ctty): Remove obsolete code.
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index bca148647..e006c4ced 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -1170,10 +1170,18 @@ fhandler_pty_master::close ()
     }
 
   fhandler_tty_master *arch = (fhandler_tty_master *) archetype;
-  if (!ForceCloseHandle (arch->from_master))
-    termios_printf ("error closing from_master %p, %E", arch->from_master);
-  if (!ForceCloseHandle (arch->to_master))
-    termios_printf ("error closing from_master %p, %E", arch->to_master);
+  if (arch->dwProcessId != GetCurrentProcessId ())
+    termios_printf ("not closing from_master(%p)/to_master(%p) since we don't own them(%d)",
+		    arch->from_master, arch->to_master, arch->dwProcessId);
+  else
+    {
+      termios_printf ("closing from_master(%p)/to_master(%p) since we own them(%d)",
+		      arch->from_master, arch->to_master, arch->dwProcessId);
+      if (!ForceCloseHandle (arch->from_master))
+	termios_printf ("error closing from_master %p, %E", arch->from_master);
+      if (!ForceCloseHandle (arch->to_master))
+	termios_printf ("error closing from_master %p, %E", arch->to_master);
+    }
   fhandler_tty_common::close ();
 
   if (!hExeced && get_ttyp ()->master_pid == myself->pid)
@@ -1390,14 +1398,14 @@ fhandler_pty_master::setup (tty& t)
     goto err;
 
   if (!DuplicateHandle (hMainProc, from_master, hMainProc, &from_master, 0, false,
-			DUPLICATE_SAME_ACCESS))
+			DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
     {
       errstr = "non-inheritable from_master";
       goto err;
     }
 
   if (!DuplicateHandle (hMainProc, to_master, hMainProc, &to_master, 0, false,
-			DUPLICATE_SAME_ACCESS))
+			DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
     {
       errstr = "non-inheritable to_master";
       goto err;
@@ -1452,6 +1460,8 @@ fhandler_pty_master::fixup_after_fork (HANDLE parent)
 	}
       arch->dwProcessId = wpid;
     }
+  from_master = arch->from_master;
+  to_master = arch->to_master;
   report_tty_counts (this, "inherited master", "");
 }
 
@@ -1460,4 +1470,6 @@ fhandler_pty_master::fixup_after_exec ()
 {
   if (!close_on_exec ())
     fixup_after_fork (spawn_info->parent);
+  else
+    from_master = to_master = NULL;
 }