From 090b8a89faa34cdc41c41e32845f1f5b444536e4 Mon Sep 17 00:00:00 2001 From: Omar Polo Date: Tue, 6 Jul 2021 10:54:27 +0000 Subject: gracefully shut down fastcgi backends we need to delete the events associated with the backends, otherwise the server process won't ever quit. Here, we add a pending counter to every backend and shut down immediately if they aren't handling any client; otherwise we try to close them as soon as possible (i.e. when they close the connection to the last connected client.) --- fcgi.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'fcgi.c') diff --git a/fcgi.c b/fcgi.c index fbe07ce..3facd2d 100644 --- a/fcgi.c +++ b/fcgi.c @@ -381,9 +381,16 @@ close_all(struct fcgi *f) start_reply(c, CGI_ERROR, "CGI error"); } + fcgi_close_backend(f); +} + +void +fcgi_close_backend(struct fcgi *f) +{ event_del(&f->e); close(f->fd); f->fd = -1; + f->pending = 0; f->s = FCGI_OFF; } @@ -413,6 +420,8 @@ handle_fcgi(int sock, short event, void *d) if (must_read(sock, (char*)&end, sizeof(end)) == -1) goto err; /* TODO: do something with the status? */ + + f->pending--; c->fcgi = -1; c->next = close_conn; event_once(c->fd, EV_WRITE, ©_mbuf, c, NULL); @@ -447,6 +456,10 @@ handle_fcgi(int sock, short event, void *d) if (!consume(sock, h.padding)) goto err; + + if (f->pending == 0 && shutting_down) + fcgi_close_backend(f); + return; err: @@ -462,6 +475,8 @@ send_fcgi_req(struct fcgi *f, struct client *c) struct tm tminfo; struct envlist *p; + f->pending++; + e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr), addr, sizeof(addr), NULL, 0, -- cgit v1.2.3