- Fix hang on logout if processes are still using the pty. Needs
   further testing.
diff --git a/serverloop.c b/serverloop.c
index a5ecfe9..2afca76 100644
--- a/serverloop.c
+++ b/serverloop.c
@@ -36,10 +36,15 @@
 /*
  * This SIGCHLD kludge is used to detect when the child exits.  The server
  * will exit after that, as soon as forwarded connections have terminated.
+ *
+ * After SIGCHLD child_has_selected is set to 1 after the first pass
+ * through the wait_until_can_do_something() select(). This ensures
+ * that the child's output gets a chance to drain before it is yanked.
  */
 
 static int child_pid;			/* Pid of the child. */
 static volatile int child_terminated;	/* The child has terminated. */
+static volatile int child_has_selected; /* Child has had chance to drain. */
 static volatile int child_wait_status;	/* Status from wait(). */
 
 void 
@@ -56,6 +61,7 @@
 		if (WIFEXITED(child_wait_status) ||
 		    WIFSIGNALED(child_wait_status))
 			child_terminated = 1;
+			child_has_selected = 0;
 	}
 	signal(SIGCHLD, sigchld_handler);
 	errno = save_errno;
@@ -300,6 +306,9 @@
 		else
 			goto retry_select;
 	}
+	
+	if (child_terminated)
+		child_has_selected = 1;
 }
 
 /*
@@ -438,6 +447,7 @@
 	/* Initialize the SIGCHLD kludge. */
 	child_pid = pid;
 	child_terminated = 0;
+	child_has_selected = 0;
 	signal(SIGCHLD, sigchld_handler);
 
 	/* Initialize our global variables. */
@@ -533,8 +543,11 @@
 		 * descriptors, and we have no more data to send to the
 		 * client, and there is no pending buffered data.
 		 */
-		if (fdout_eof && fderr_eof && !packet_have_data_to_write() &&
-		    buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) {
+		if (((fdout_eof && fderr_eof) || 
+		    (child_terminated && child_has_selected)) && 
+		    !packet_have_data_to_write() &&
+		    (buffer_len(&stdout_buffer) == 0) && 
+			 (buffer_len(&stderr_buffer) == 0)) {
 			if (!channel_still_open())
 				goto quit;
 			if (!waiting_termination) {