mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: mwrap-perl@80x24.org
Subject: [PATCH 14/19] mwrap_httpd: do not abort on fork if out-of-resources
Date: Thu, 15 Dec 2022 20:52:50 +0000	[thread overview]
Message-ID: <20221215205255.27840-15-e@80x24.org> (raw)
In-Reply-To: <20221215205255.27840-1-e@80x24.org>

Instead, we'll leak memory if the pthread_atfork handlers can't
stop httpd at fork due to resource limitations.
---
 mwrap_httpd.h | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/mwrap_httpd.h b/mwrap_httpd.h
index f484bdd..fc096c0 100644
--- a/mwrap_httpd.h
+++ b/mwrap_httpd.h
@@ -1094,6 +1094,12 @@ static int h1d_init(struct mw_h1d *h1d, const char *menv)
 	memcpy(h1d->pid_str, p, h1d->pid_len);
 	if (unlink(sa.un.sun_path) < 0 && errno != ENOENT)
 		return fprintf(stderr, "unlink(%s): %m\n", sa.un.sun_path);
+	/*
+	 * lfd may be >=0 if h1d_stop_join failed in parent and we're now
+	 * running in a forked child
+	 */
+	if (h1d->lfd >= 0)
+		(void)close(h1d->lfd);
 	h1d->lfd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
 	if (h1d->lfd < 0)
 		return fprintf(stderr, "socket: %m\n");
@@ -1204,25 +1210,28 @@ static void h1d_stop_join(struct mw_h1d *h1d)
 	socklen_t len = (socklen_t)sizeof(sa);
 	int e, sfd;
 	void *ret;
+#define ERR ": %m (can't stop mwrap-httpd before fork)\n"
 
 	mwrap_assert(uatomic_read(&h1d->alive) == 0);
 	if (getsockname(h1d->lfd, &sa.any, &len) < 0) {
-		fprintf(stderr, "getsockname: %m\n");
-		abort(); /* TODO: graceful fallback (ENOBUFS) */
+		fprintf(stderr, "getsockname"ERR);
+		return;
 	}
 	sfd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
 	if (sfd < 0) {
-		fprintf(stderr, "socket: %m\n");
-		abort(); /* TODO: graceful fallback (ENOMEM, EMFILE, ...) */
+		fprintf(stderr, "socket"ERR);
+		return;
 	}
 	if (connect(sfd, &sa.any, len) < 0) {
-		fprintf(stderr, "connect: %m\n");
-		/* TODO: graceful fallback (EAGAIN, ...) */
+		fprintf(stderr, "connect"ERR);
+		close(sfd);
+		return;
 	}
+#undef ERR
 	(void)close(sfd);
 	e = pthread_join(h1d->tid, &ret);
 	if (e) {
-		fprintf(stderr, "pthread_join: %s\n", strerror(e));
+		fprintf(stderr, "BUG? pthread_join: %s\n", strerror(e));
 		abort();
 	}
 	(void)close(h1d->lfd);

  parent reply	other threads:[~2022-12-15 20:52 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-15 20:52 [PATCH 00/19] another round of httpd improvements Eric Wong
2022-12-15 20:52 ` [PATCH 01/19] mwrap_httpd: show current bytes consistently Eric Wong
2022-12-15 20:52 ` [PATCH 02/19] introduce AUTO_FREE macro to simplify cleanup Eric Wong
2022-12-15 20:52 ` [PATCH 03/19] httpd: rework httpd to use auto-free for memstream Eric Wong
2022-12-15 20:52 ` [PATCH 04/19] httpd: avoid newline if not using bt: >= 1 Eric Wong
2022-12-15 20:52 ` [PATCH 05/19] mwrap_httpd: flesh out /$PID/ and /$PID/trim endpoints Eric Wong
2022-12-15 20:52 ` [PATCH 06/19] mwrap_httpd: add info about src_file and src_loc stats Eric Wong
2022-12-15 20:52 ` [PATCH 07/19] use uatomic_inc where appropriate Eric Wong
2022-12-15 20:52 ` [PATCH 08/19] httpd: drop unnecessary AND ops from base-64 Eric Wong
2022-12-15 20:52 ` [PATCH 09/19] mymalloc: add notes on the malloc implementation Eric Wong
2022-12-15 20:52 ` [PATCH 10/19] rproxy: link to mwrap_httpd /$PID/ root without each, too Eric Wong
2022-12-15 20:52 ` [PATCH 11/19] httpd: shrink `mean_life' field to `double' Eric Wong
2022-12-15 20:52 ` [PATCH 12/19] httpd: support CSV output Eric Wong
2022-12-15 20:52 ` [PATCH 13/19] rproxy: enable deflater by default Eric Wong
2022-12-15 20:52 ` Eric Wong [this message]
2022-12-15 20:52 ` [PATCH 15/19] httpd: pause forking thread on resource limitations Eric Wong
2022-12-15 20:52 ` [PATCH 16/19] rename mwrap_httpd.h to httpd.h Eric Wong
2022-12-15 20:52 ` [PATCH 17/19] httpd: describe simple and naive buffering scheme Eric Wong
2022-12-15 20:52 ` [PATCH 18/19] httpd: drop TODO item for pipelining Eric Wong
2022-12-15 20:52 ` [PATCH 19/19] avoid -Warray-bounds warning, avoid stack overallocation Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221215205255.27840-15-e@80x24.org \
    --to=e@80x24.org \
    --cc=mwrap-perl@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mwrap-perl.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).