diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index e91b3e3..95bf445 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -1309,13 +1309,37 @@ fhandler_pty_master::close ()
 	}
     }
 
+  HANDLE output_handle_local;
+  if (!DuplicateHandle (GetCurrentProcess (), get_output_handle (),
+		       GetCurrentProcess (), &output_handle_local,
+		       0, TRUE, DUPLICATE_SAME_ACCESS))
+    {
+      termios_printf ("DuplicateHandle (output_handle), %E");
+      output_handle_local = NULL;
+      SetEvent (input_available_event);
+    }
+
   fhandler_pty_common::close ();
 
   /* Check if the last master handle has been closed.  If so, set
      input_available_event to wake up potentially waiting slaves. */
-  if (!PeekNamedPipe (from_master, NULL, 0, NULL, NULL, NULL)
-      && GetLastError () == ERROR_BROKEN_PIPE) 
-    SetEvent (input_available_event);
+  if (output_handle_local)
+    {
+      OBJECT_BASIC_INFORMATION obi;
+      NTSTATUS status;
+      ULONG hdl_cnt = 0;
+
+      status = NtQueryObject (output_handle_local, ObjectBasicInformation,
+	  &obi, sizeof obi, NULL);
+      if (!NT_SUCCESS (status))
+	debug_printf ("NtQueryObject: %y", status);
+      else
+	hdl_cnt = obi.HandleCount;
+      termios_printf("HandleCount: %d", hdl_cnt);
+      if (hdl_cnt == 1)
+	SetEvent (input_available_event);
+      CloseHandle (output_handle_local);
+    }
 
   if (!ForceCloseHandle (from_master))
     termios_printf ("error closing from_master %p, %E", from_master);
