Do not close panes until process has exited and any outstanding data
authornicm <nicm@openbsd.org>
Mon, 3 Jul 2017 08:16:03 +0000 (08:16 +0000)
committernicm <nicm@openbsd.org>
Mon, 3 Jul 2017 08:16:03 +0000 (08:16 +0000)
has been written to the pipe-pane event if there is one. GitHub issue 991.

usr.bin/tmux/cmd-pipe-pane.c
usr.bin/tmux/server.c
usr.bin/tmux/tmux.h
usr.bin/tmux/window.c

index 1f0a795..501b858 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd-pipe-pane.c,v 1.42 2017/05/01 12:20:55 nicm Exp $ */
+/* $OpenBSD: cmd-pipe-pane.c,v 1.43 2017/07/03 08:16:03 nicm Exp $ */
 
 /*
  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -35,6 +35,7 @@
 
 static enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmdq_item *);
 
+static void cmd_pipe_pane_write_callback(struct bufferevent *, void *);
 static void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
 
 const struct cmd_entry cmd_pipe_pane_entry = {
@@ -68,6 +69,11 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item)
                bufferevent_free(wp->pipe_event);
                close(wp->pipe_fd);
                wp->pipe_fd = -1;
+
+               if (window_pane_destroy_ready(wp)) {
+                       server_destroy_pane(wp, 1);
+                       return (CMD_RETURN_NORMAL);
+               }
        }
 
        /* If no pipe command, that is enough. */
@@ -131,8 +137,9 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item)
                wp->pipe_fd = pipe_fd[0];
                wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
 
-               wp->pipe_event = bufferevent_new(wp->pipe_fd,
-                   NULL, NULL, cmd_pipe_pane_error_callback, wp);
+               wp->pipe_event = bufferevent_new(wp->pipe_fd, NULL,
+                   cmd_pipe_pane_write_callback, cmd_pipe_pane_error_callback,
+                   wp);
                bufferevent_enable(wp->pipe_event, EV_WRITE);
 
                setblocking(wp->pipe_fd, 0);
@@ -142,13 +149,28 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item)
        }
 }
 
+static void
+cmd_pipe_pane_write_callback(__unused struct bufferevent *bufev, void *data)
+{
+       struct window_pane      *wp = data;
+
+       log_debug("%%%u pipe empty", wp->id);
+       if (window_pane_destroy_ready(wp))
+               server_destroy_pane(wp, 1);
+}
+
 static void
 cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev,
     __unused short what, void *data)
 {
        struct window_pane      *wp = data;
 
+       log_debug("%%%u pipe error", wp->id);
+
        bufferevent_free(wp->pipe_event);
        close(wp->pipe_fd);
        wp->pipe_fd = -1;
+
+       if (window_pane_destroy_ready(wp))
+               server_destroy_pane(wp, 1);
 }
index 5aa9b0a..29a2abe 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: server.c,v 1.171 2017/06/04 08:25:57 nicm Exp $ */
+/* $OpenBSD: server.c,v 1.172 2017/07/03 08:16:03 nicm Exp $ */
 
 /*
  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -406,7 +406,12 @@ server_child_exited(pid_t pid, int status)
                TAILQ_FOREACH(wp, &w->panes, entry) {
                        if (wp->pid == pid) {
                                wp->status = status;
-                               server_destroy_pane(wp, 1);
+
+                               log_debug("%%%u exited", wp->id);
+                               wp->flags |= PANE_EXITED;
+
+                               if (window_pane_destroy_ready(wp))
+                                       server_destroy_pane(wp, 1);
                                break;
                        }
                }
index 37fa4e9..345da8a 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmux.h,v 1.789 2017/06/30 22:24:08 nicm Exp $ */
+/* $OpenBSD: tmux.h,v 1.790 2017/07/03 08:16:03 nicm Exp $ */
 
 /*
  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -768,6 +768,8 @@ struct window_pane {
 #define PANE_FOCUSPUSH 0x20
 #define PANE_INPUTOFF 0x40
 #define PANE_CHANGED 0x80
+#define PANE_ERROR 0x100
+#define PANE_EXITED 0x200
 
        int              argc;
        char           **argv;
@@ -2133,6 +2135,7 @@ u_int              window_count_panes(struct window *);
 void            window_destroy_panes(struct window *);
 struct window_pane *window_pane_find_by_id_str(const char *);
 struct window_pane *window_pane_find_by_id(u_int);
+int             window_pane_destroy_ready(struct window_pane *);
 int             window_pane_spawn(struct window_pane *, int, char **,
                     const char *, const char *, const char *, struct environ *,
                     struct termios *, char **);
@@ -2154,7 +2157,6 @@ void               window_pane_key(struct window_pane *, struct client *,
 int             window_pane_outside(struct window_pane *);
 int             window_pane_visible(struct window_pane *);
 u_int           window_pane_search(struct window_pane *, const char *);
-
 const char     *window_printable_flags(struct winlink *);
 struct window_pane *window_pane_find_up(struct window_pane *);
 struct window_pane *window_pane_find_down(struct window_pane *);
index 9330bbc..2d3645a 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: window.c,v 1.199 2017/06/28 11:36:40 nicm Exp $ */
+/* $OpenBSD: window.c,v 1.200 2017/07/03 08:16:03 nicm Exp $ */
 
 /*
  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -388,6 +388,19 @@ window_destroy(struct window *w)
        free(w);
 }
 
+int
+window_pane_destroy_ready(struct window_pane *wp)
+{
+       if (wp->pipe_fd != -1 && EVBUFFER_LENGTH(wp->pipe_event->output) != 0)
+               return (0);
+
+       if (~wp->flags & PANE_EXITED)
+               return (0);
+       if (~wp->flags & PANE_ERROR)
+               return (0);
+       return (1);
+}
+
 void
 window_add_ref(struct window *w, const char *from)
 {
@@ -1000,7 +1013,11 @@ window_pane_error_callback(__unused struct bufferevent *bufev,
 {
        struct window_pane *wp = data;
 
-       server_destroy_pane(wp, 1);
+       log_debug("%%%u error", wp->id);
+       wp->flags |= PANE_ERROR;
+
+       if (window_pane_destroy_ready(wp))
+               server_destroy_pane(wp, 1);
 }
 
 void