Эх сурвалжийг харах

Fix stalled keep-alive requests and image decode bursts

Treat an HTTP/1.0 response as connection-close unless it explicitly opts into keep-alive: reusing the half-closed socket discarded the next request, so after the first worker batch later images waited for a response that could not arrive until the retry timeout.  A reused connection now waits 3 s for response headers (a fresh one keeps the 10 s idle timeout) so a silently closed keep-alive is retried promptly.  Decode at most one image per main-loop pass so the TCP pump, timers and input run between JPEG decodes on slow CPUs, and keep the four idle fetch helpers across navigations instead of killing and respawning all of them while images are still loading.
Johannes Zellner 1 долоо хоног өмнө
parent
commit
c1edf1b343
2 өөрчлөгдсөн 39 нэмэгдсэн , 43 устгасан
  1. 19 36
      browser.c
  2. 20 7
      uhttp.c

+ 19 - 36
browser.c

@@ -2112,34 +2112,23 @@ static void fetch_workers_cancel_busy(void)
 			fetch_worker_stop(&fetch_workers[i]);
 }
 
-/* A kept-alive worker is only useful for the next page if its last URL was
- * on the same origin.  Closing the others before a navigation avoids leaving
- * a pile of idle TLS connections from the previous site. */
-static int worker_same_origin(const struct fetch_worker *w,
-			      const struct uhttp_url *u)
-{
-	struct uhttp_url wu;
-
-	if (w->pid <= 0 || !w->m.final[0])
-		return 0;
-	if (uhttp_parse_url(w->m.final, &wu) != 0)
-		return 0;
-	return wu.secure == u->secure && wu.port == u->port &&
-	       s_eq(wu.host, u->host);
-}
-
 static void fetch_workers_prepare_nav(const char *url)
 {
-	struct uhttp_url u;
-	int have = url && uhttp_parse_url(url, &u) == 0;
 	int i;
 
+	(void)url;
 	for (i = 0; i < FETCH_WORKERS; i++) {
 		struct fetch_worker *w = &fetch_workers[i];
 
 		if (w->pid <= 0)
 			continue;
-		if (w->busy || !have || !worker_same_origin(w, &u))
+		/* Only an in-flight transfer must be aborted.  Idle helpers
+		 * stay warm across origins: there are just FETCH_WORKERS of
+		 * them, and uhttp_open drops the old connection when the
+		 * next job is for another host.  Killing and respawning all
+		 * four on every navigation churned processes and sockets
+		 * while images were still loading. */
+		if (w->busy)
 			fetch_worker_stop(w);
 	}
 }
@@ -3429,7 +3418,7 @@ static int preload_fill_jobs(void)
 
 static void preload_step(void)
 {
-	unsigned int batch = 0;
+	int i;
 
 	if (!preload_active)
 		return;
@@ -3440,24 +3429,18 @@ static void preload_step(void)
 		return;
 	}
 
-	for (;;) {
-		int started = preload_fill_jobs();
-		int done = 0;
-		int i;
+	preload_fill_jobs();
 
-		for (i = 0; i < FETCH_WORKERS; i++) {
-			if (!fetch_workers[i].busy)
-				continue;
-			if (preload_finish_worker(&fetch_workers[i]))
-				done++;
-			if (batch + done >= 4)
-				break;
-		}
-		batch += (unsigned int)done;
-		if (batch >= 4)
+	/* Decode at most one completed image per main-loop pass.  A full-size
+	 * JPEG decode blocks this single-threaded UI for a long time, and
+	 * harvesting a batch back-to-back starved the TCP pump (and therefore
+	 * every other image download) on slow CPUs: requests looked stuck
+	 * while four decodes ran in a row. */
+	for (i = 0; i < FETCH_WORKERS; i++) {
+		if (!fetch_workers[i].busy)
+			continue;
+		if (preload_finish_worker(&fetch_workers[i]))
 			return;
-		if (!started && !done)
-			break;
 	}
 
 	if (preload_active && preload_node >= doc.nnode &&

+ 20 - 7
uhttp.c

@@ -574,7 +574,8 @@ static unsigned int req_app_dec(char *buf, unsigned int pos, unsigned int cap,
 
 /* Read the status line and headers of the current response.  Returns 0 and
  * sets redirect/location when the response is a redirect with a target. */
-static int read_response_head(struct uhttp *h, char *location, int *have_loc)
+static int read_response_head(struct uhttp *h, char *location, int *have_loc,
+			      unsigned int timeout_ms)
 {
 	char line[UHTTP_LINE_MAX];
 	unsigned int status = 0;
@@ -597,7 +598,7 @@ static int read_response_head(struct uhttp *h, char *location, int *have_loc)
 	*have_loc = 0;
 	location[0] = '\0';
 
-	rv = reader_readline(h, line, sizeof(line), UHTTP_IDLE_TIMEOUT_MS);
+	rv = reader_readline(h, line, sizeof(line), timeout_ms);
 	if (rv < 0)
 		return (h->error == UHTTP_ERR_RESET) ? UHTTP_ERR_RESET :
 						       UHTTP_ERR_TIMEOUT;
@@ -605,9 +606,17 @@ static int read_response_head(struct uhttp *h, char *location, int *have_loc)
 	h->resp.status = (int)status;
 	if (h->hooks && h->hooks->status)
 		h->hooks->status(h->hooks->ctx, (int)status, line);
+	/* HTTP/1.0 has no persistent connections unless the response
+	 * explicitly opts in.  Ignoring this kept a half-closed socket for
+	 * the next request, which then waited for a response that could not
+	 * arrive: the next batch of images appeared to hang. */
+	if (line[0] == 'H' && line[1] == 'T' && line[2] == 'T' &&
+	    line[3] == 'P' && line[4] == '/' && line[5] == '1' &&
+	    line[6] == '.' && line[7] == '0')
+		h->peer_close = 1;
 
 	for (;;) {
-		rv = reader_readline(h, line, sizeof(line), UHTTP_IDLE_TIMEOUT_MS);
+		rv = reader_readline(h, line, sizeof(line), timeout_ms);
 		if (rv < 0)
 			return (h->error == UHTTP_ERR_RESET) ? UHTTP_ERR_RESET :
 							       UHTTP_ERR_TIMEOUT;
@@ -633,9 +642,11 @@ static int read_response_head(struct uhttp *h, char *location, int *have_loc)
 		} else if (hdr_match(line, "location")) {
 			copy_after_colon(line, location, UHTTP_LOC_MAX);
 			*have_loc = location[0] != '\0';
-		} else if (hdr_match(line, "connection") &&
-			   contains_ci(line, "close")) {
-			h->peer_close = 1;
+		} else if (hdr_match(line, "connection")) {
+			if (contains_ci(line, "close"))
+				h->peer_close = 1;
+			else if (contains_ci(line, "keep-alive"))
+				h->peer_close = 0;
 		} else if (h->jar && hdr_match(line, "set-cookie")) {
 			char *v = line + 11;    /* past "set-cookie:" */
 
@@ -808,7 +819,9 @@ int uhttp_open(struct uhttp *h, const char *url, unsigned int flags,
 			rv = send_request(h, flags);
 			if (rv == 0)
 				rv = read_response_head(h, location,
-							&have_loc);
+							&have_loc,
+							used_reuse ? 3000 :
+							UHTTP_IDLE_TIMEOUT_MS);
 			if (rv == 0)
 				break;
 			if (!used_reuse)