From 828001ecc51d94f8e7a7f0da1f42204984ede2e9 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 13 Dec 2019 06:55:12 +0000 Subject: [PATCH 1/2] Do not spin waiting for exit, instead check in the write callback. --- client.c | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/client.c b/client.c index db3ad07c..11b13dc2 100644 --- a/client.c +++ b/client.c @@ -45,6 +45,7 @@ static enum { CLIENT_EXIT_EXITED, CLIENT_EXIT_SERVER_EXITED, } client_exitreason = CLIENT_EXIT_NONE; +static int client_exitflag; static int client_exitval; static enum msgtype client_exittype; static const char *client_exitsession; @@ -209,6 +210,27 @@ client_exit_message(void) return ("unknown reason"); } +/* Exit if all streams flushed. */ +static void +client_exit(void) +{ + struct client_file *cf; + size_t left; + int waiting = 0; + + RB_FOREACH (cf, client_files, &client_files) { + if (cf->event == NULL) + continue; + left = EVBUFFER_LENGTH(cf->event->output); + if (left != 0) { + waiting++; + log_debug("file %u %zu bytes left", cf->stream, left); + } + } + if (waiting == 0) + proc_exit(client_proc); +} + /* Client main loop. */ int client_main(struct event_base *base, int argc, char **argv, int flags) @@ -451,6 +473,9 @@ client_write_callback(__unused struct bufferevent *bev, void *arg) RB_REMOVE(client_files, &client_files, cf); file_free(cf); } + + if (client_exitflag) + client_exit(); } /* Open write file. */ @@ -724,29 +749,6 @@ client_signal(int sig) } } -/* Exit if all streams flushed. */ -static void -client_exit(__unused int fd, __unused short events, __unused void *arg) -{ - struct client_file *cf; - size_t left; - int waiting = 0; - - RB_FOREACH (cf, client_files, &client_files) { - if (cf->event == NULL) - continue; - left = EVBUFFER_LENGTH(cf->event->output); - if (left != 0) { - waiting++; - log_debug("file %u %zu bytes left", cf->stream, left); - } - } - if (waiting == 0) - proc_exit(client_proc); - else - event_once(-1, EV_TIMEOUT, client_exit, NULL, NULL); -} - /* Callback for client read events. */ static void client_dispatch(struct imsg *imsg, __unused void *arg) @@ -799,7 +801,8 @@ client_dispatch_wait(struct imsg *imsg) memcpy(&retval, data, sizeof retval); client_exitval = retval; } - event_once(-1, EV_TIMEOUT, client_exit, NULL, NULL); + client_exitflag = 1; + client_exit(); break; case MSG_READY: if (datalen != 0) From 6ce943f4d979423290662f691e3d8a3cded79a42 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 13 Dec 2019 07:00:22 +0000 Subject: [PATCH 2/2] Need to check in the error callback also. --- client.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/client.c b/client.c index 11b13dc2..4bebb977 100644 --- a/client.c +++ b/client.c @@ -459,6 +459,9 @@ client_write_error_callback(__unused struct bufferevent *bev, close(cf->fd); cf->fd = -1; + + if (client_exitflag) + client_exit(); } /* File write callback. */