Sfoglia il codice sorgente

Fix networking stalls after a busy browser page

Close idle fetch workers when navigating to a different origin instead of carrying the previous site's TLS/TCP connections into the next fetch; same-origin reloads still reuse warm helpers.  Retry a page fetch once after a DNS failure.

Keep ARP entries for five minutes with a sliding age so a long page load cannot expire the gateway or DNS entry.

Drain the NIC in bursts in DNS/ARP/TCP waits: a driver returns -1 for an empty ring and after an overflow reset, so one net_pump() pass could leave a wait blind.  Raise the caller budgets from 8 to 64.

Update AGENTS.md and the network/userland pages.
Johannes Zellner 1 settimana fa
parent
commit
d7fae551d8
8 ha cambiato i file con 91 aggiunte e 23 eliminazioni
  1. 20 12
      AGENTS.md
  2. 46 1
      browser.c
  3. 2 2
      dns.c
  4. 2 2
      docs/network.html
  5. 1 1
      docs/userland.html
  6. 5 2
      net.c
  7. 12 0
      netcore.h
  8. 3 3
      tcp.c

+ 20 - 12
AGENTS.md

@@ -598,7 +598,9 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   the boot DHCP is skipped while it returns 0.  `net.c` — ARP + ICMP echo, IPv4/UDP
   send + parse helpers,
   shared ARP resolution (`net_arp_resolve`) and routing (`net_next_hop`), frame
-  decode, `net_checksum()`/`net_checksum_tcp()`.  `dhcp.c` — minimal DHCP client
+  decode, `net_checksum()`/`net_checksum_tcp()`.  ARP entries expire after five
+  minutes and their age slides on every hit, so a gateway or DNS entry is not
+  lost during a long page load.  `dhcp.c` — minimal DHCP client
   (DISCOVER →
   OFFER → REQUEST → ACK; runs at boot when a NIC is present, 4 s budget, keeps
   the static address when no server answers; `dhcp` re-runs it; learns the DNS
@@ -1017,12 +1019,15 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   but parsed/executed in document order; one above-fold image starts downloading
   before the script phase so it overlaps the script fetches and execution.
   Each helper keeps its own
-  TCP/TLS connection (TLS stays per-process).  Idle helpers stay warm across
-  navigations so a second page on the same origin reuses the TCP/TLS
-  connection instead of paying a spawn and handshake; they exit after ten
-  idle seconds and the browser stops them at once when its window loses
-  focus, so raw commands can acquire the NIC.  The helper sends
-  `Accept-Encoding: gzip`; the browser inflates with a
+  TCP/TLS connection (TLS stays per-process).  Idle helpers stay warm for a
+  same-origin navigation (reload or another page on the same host) so it
+  reuses the TCP/TLS connection instead of paying a spawn and handshake; a
+  navigation to another origin stops them first, so the previous page's image
+  and TLS connections cannot stall the new fetch.  Helpers also exit after
+  ten idle seconds, and the browser stops them at once when its window loses
+  focus, so raw commands can acquire the NIC.  A page fetch that fails with a
+  DNS error is torn down and tried once more after half a second.  The helper
+  sends `Accept-Encoding: gzip`; the browser inflates with a
   progress hook that pumps the UI.  The status bar reports the page fetch
   (headers and first-byte milliseconds).  Page, CSS and
   image buffers come from the ring-3 libc heap; the DOM uses fixed static
@@ -1153,7 +1158,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   provide the route to the peer.  Every stack syscall drains the NIC through
   `net_pump()` (`netcore.c/h`), which dispatches each frame to the client
   table, the WebDAV listener, a pending DNS query, the ARP cache and
-  `netkexec_capture_frame()`, and answers ARP/ICMP.  The pump runs under the
+  `netkexec_capture_frame()`, and answers ARP/ICMP.  Stack waits use
+  `net_pump_burst()`, repeated pump passes, because drivers return -1 both for
+  an empty ring and after an overflow reset, and one pass would otherwise
+  leave DNS/ARP/TCP waiting on a full ring.  The pump runs under the
   BKL, so the shared receive scratch and protocol state stay serialized; waits
   yield with `process_sleep_ms()` and re-enable interrupts around driver TX
   timeouts (the syscall stub enters with them off).  A raw acquire fails while
@@ -1855,10 +1863,10 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     and private `/share/bf*N` files; images are prefetched in parallel and
     decoded as jobs complete.  Stylesheet and external-script bodies are
     downloaded in parallel but parsed/executed in document order, and one
-    image starts before the script phase.  Idle
-    helpers stay warm across navigations and are stopped when the browser
-    loses focus (or after ten idle seconds) so their sockets do not block raw
-    commands.  The helper requests gzip; a page
+    image starts before the script phase.  Idle helpers stay warm for
+    same-origin navigations; a cross-origin navigation stops them first, as
+    does losing focus (or ten idle seconds), so their sockets do not block raw
+    commands or the new page.  The helper requests gzip; a page
     that is only small when compressed can exceed the 2 MB page/832 KB CSS
     caps.  Video/audio elements are placeholders.  Bare `&` characters are
     consumed by the entity decoder instead of looping forever.  A page with

+ 46 - 1
browser.c

@@ -2107,6 +2107,38 @@ 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;
+
+	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))
+			fetch_worker_stop(w);
+	}
+}
+
 /* The browser is no longer the active window and no fetch is running: give
  * the NIC back so raw commands (ping/netrecv/websrv) can acquire it. */
 static void fetch_workers_stop_if_unfocused(void)
@@ -3487,6 +3519,7 @@ static void load_page(const char *url, int local, int push)
 	unsigned int post_len = pending_post_len;
 	unsigned int page_open_ms = 0, page_first_ms = 0, page_total_ms = 0;
 	int upgraded = nav_https_upgraded;
+	int retried = 0;
 	int rv;
 
 	nav_https_upgraded = 0;
@@ -3495,7 +3528,7 @@ static void load_page(const char *url, int local, int push)
 	cancel_fetch = 0;
 	preload_active = 0;
 	preload_pending = 0;
-	fetch_workers_cancel_busy();
+	fetch_workers_prepare_nav(url);
 
 	js_browser_stop();
 	js_last_status[0] = '\0';
@@ -3534,6 +3567,18 @@ static void load_page(const char *url, int local, int push)
 	rv = read_url(url, BR_PAGE_MAX, &body, &len, &local, ctype,
 		     sizeof(ctype), final, sizeof(final), err, sizeof(err),
 		     &truncated, post, post_len);
+	if (rv != 0 && rv != FETCH_ABORT && !local && !retried &&
+	    fetch_last_error == UHTTP_ERR_DNS) {
+		/* A busy previous page can leave the receiver ring full just
+		 * long enough to lose a DNS reply.  Tear the old helpers down
+		 * and try once more before giving up. */
+		retried = 1;
+		fetch_workers_stop_all();
+		u_sleep_ms(500);
+		rv = read_url(url, BR_PAGE_MAX, &body, &len, &local, ctype,
+			     sizeof(ctype), final, sizeof(final), err,
+			     sizeof(err), &truncated, post, post_len);
+	}
 	if (rv == FETCH_ABORT)
 		return;
 	/* Capture the page timing before CSS/script/image fetches overwrite the

+ 2 - 2
dns.c

@@ -374,7 +374,7 @@ int dns_resolve(const char *name, unsigned char ip[4])
 				net_stack_leave();
 			return -1;
 		}
-		net_pump(8);
+		net_pump_burst(64);
 		process_sleep_ms(1);
 	}
 	dns_busy = 1;
@@ -469,7 +469,7 @@ int dns_resolve(const char *name, unsigned char ip[4])
 				r = 0;
 				goto out;
 			}
-			net_pump(8);
+			net_pump_burst(64);
 			if (dns_pending_result > 0) {
 				for (i = 0; i < 4; i++)
 					ip[i] = dns_pending_ip[i];

+ 2 - 2
docs/network.html

@@ -227,7 +227,7 @@
 		<h2>Link &amp; IP layer</h2>
 		<ul>
 			<li><strong>Ethernet decode</strong> recognises IPv4, ARP and IPv6 frame types (IPv6 is not implemented).</li>
-			<li><strong>ARP</strong> resolves a next hop with three tries, one second apart; <code>net_arp_resolve()</code> polls the active NIC and the caller must own it.</li>
+			<li><strong>ARP</strong> resolves a next hop with three tries, one second apart; <code>net_arp_resolve()</code> polls the active NIC and the caller must own it.  Cache entries live five minutes and their age slides on every hit, so a DNS or gateway entry does not expire during a long page load.  Stack waits drain the receiver in short bursts so a full driver ring can reset and recover.</li>
 			<li><strong>Routing</strong> sends off-subnet traffic to the gateway when one is known; <code>net_next_hop()</code> is shared by ping, DNS and TCP.</li>
 			<li><strong>ICMP echo</strong> backs <code>ping</code>, with ARP resolution and round-trip timing.</li>
 			<li><strong>UDP</strong> has send and parse helpers; DNS, DHCP, NTP and netlog are its users.</li>
@@ -390,7 +390,7 @@
 			<li><strong>curl</strong> — <code>http://</code> or <code>https://</code> GET with DNS, ARP, redirects (up to five hops), chunked decoding, status/headers/body dump and a TLS warning.</li>
 			<li><strong>uhttp</strong> — shared ring-3 HTTP/1.1 client used by curl, the browser and chat: URL parsing, redirects (up to eight hops; 1 KB paths and 1.5 KB header lines, enough for OIDC URLs), chunked decoding, timeouts and TLS.  Requests and bodies of any size are split into pieces the transport accepts (1024-byte TLS records, one peer MSS for plain TCP).  Optional per-request fields set the <code>Accept</code> header, extra header lines and a cookie jar; 1xx/204/304 responses are treated as bodyless so keep-alive does not wait for an idle timeout.</li>
 			<li><strong>uhttp_cookie</strong> — the cookie jar (<span class="file">uhttp_cookie.c</span>, 24 cookies of up to 768 bytes).  Every <code>Set-Cookie</code>, including those on redirect responses, is stored; matching cookies are sent on every request and redirect hop.  Cookies are host-only (<code>Domain</code> is ignored), <code>Path</code> is a prefix match, <code>Secure</code> cookies only go over https, and <code>Max-Age&lt;=0</code> or a 1970 <code>Expires</code> deletes a cookie.  The jar outlives the connection, so a cross-origin redirect that reconnects keeps the session.</li>
-			<li><strong>browser</strong> — runs network fetches in up to four headless <code>curl __serve</code> workers, each with its own request/body/meta files under <code>/share/bf&lt;session&gt;_&lt;slot&gt;*</code>.  The session id comes from the browser pid, so multiple browser windows do not share queue files.  Stylesheet and external-script bodies are downloaded in parallel but parsed/executed in document order; one image starts before the script phase, and the remaining images are prefetched in parallel and decoded as jobs complete, so the UI keeps drawing and scrolling.  Each worker keeps its own TCP/TLS connection; idle helpers stay warm across navigations and exit after ten idle seconds, and losing window focus stops them so raw commands can acquire the NIC.  The status bar reports the page's total, header and first-byte times.</li>
+			<li><strong>browser</strong> — runs network fetches in up to four headless <code>curl __serve</code> workers, each with its own request/body/meta files under <code>/share/bf&lt;session&gt;_&lt;slot&gt;*</code>.  The session id comes from the browser pid, so multiple browser windows do not share queue files.  Stylesheet and external-script bodies are downloaded in parallel but parsed/executed in document order; one image starts before the script phase, and the remaining images are prefetched in parallel and decoded as jobs complete, so the UI keeps drawing and scrolling.  Each worker keeps its own TCP/TLS connection; same-origin navigations reuse idle helpers, while a cross-origin navigation stops them first, and helpers also exit after ten idle seconds or when the window loses focus.  A DNS failure is retried once after tearing the helpers down.  The status bar reports the page's total, header and first-byte times.</li>
 			<li><strong>websrv</strong> — the server side, speaking HTTP/1.1 + WebDAV over the TCP listener; see <a href="filesystems.html#webdav">Filesystems → WebDAV</a>.</li>
 		</ul>
 		<p>

+ 1 - 1
docs/userland.html

@@ -385,7 +385,7 @@
 			<li><strong>Not supported.</strong> Inline event attributes (<code>onclick=</code>), <code>MouseEvent</code>/<code>CustomEvent</code> and <code>once</code>/<code>passive</code> listener options, <code>type="module"</code>, <code>defer</code>/<code>async</code>, <code>MutationObserver</code>/<code>IntersectionObserver</code>, <code>XMLHttpRequest</code>/<code>fetch</code>, <code>DOMParser</code>, canvas/2D contexts and pseudo-class/element selectors. Basic controls render, focus, edit, toggle, select, submit GET queries and POST <code>application/x-www-form-urlencoded</code> bodies (see the compatibility ladder); <code>getComputedStyle</code> covers the engine's properties, and <code>outerHTML</code> only returns the string last assigned to <code>innerHTML</code>.</li>
 			<li><strong>CSS gaps.</strong> Multiple background layers and <code>background-attachment</code> (a non-uniform <code>background-size</code> uses the width scale), 3D/perspective transforms and transformed text/descendants (scale/rotate paint the box and inline images), pseudo-classes/elements, border-radius clipping of descendants, gradients/box-shadow/transitions and <code>color-mix</code>. Flex rows, card grids, tables with rowspan/colspan, inline SVG and CSS variables are supported.</li>
 			<li><strong>Caps.</strong> 2 MB page body, 832 KB CSS, 6144 DOM nodes, 4608 rules, 16 images. Each image gets an <code>img_estimate()</code>-sized decode arena that is freed after the scaled bitmap is made; navigation calls <code>heap_reset()</code> and unmaps the page heap.</li>
-			<li><strong>Fetching.</strong> Up to four worker processes prefetch images in parallel within the viewport plus 160 px; each worker keeps its own TCP/TLS connection and its own request/body/meta files (keyed by browser pid and worker slot, so multiple windows do not collide).  Completed images are decoded as jobs finish.  Stylesheet and external-script bodies are downloaded in parallel but parsed/executed in document order, and one image starts before the script phase so it overlaps script loading.  Idle helpers stay warm across navigations (they exit after ten idle seconds, and losing window focus stops them), so a second page on the same origin reuses its connection.  The status bar reports the page fetch's header and first-byte times.</li>
+			<li><strong>Fetching.</strong> Up to four worker processes prefetch images in parallel within the viewport plus 160 px; each worker keeps its own TCP/TLS connection and its own request/body/meta files (keyed by browser pid and worker slot, so multiple windows do not collide).  Completed images are decoded as jobs finish.  Stylesheet and external-script bodies are downloaded in parallel but parsed/executed in document order, and one image starts before the script phase so it overlaps script loading.  Idle helpers stay warm for same-origin navigations (they exit after ten idle seconds, and losing window focus stops them), while a cross-origin navigation closes them first so the old page's connections cannot stall the new fetch.  A DNS failure is retried once.  The status bar reports the page fetch's header and first-byte times.</li>
 			<li><strong>URL entry.</strong> A dotted name without a scheme that is not a known local file extension is treated as a hostname, so <code>cloudron.io</code> does not hit the local-file stat. A typed hostname is tried as <code>https://</code> first, skipping the common <code>http://host</code> → <code>https://host</code> hop; if the https connection is refused, reset, times out or fails the TLS handshake, the browser retries once over <code>http://</code> and replaces the history entry. Origin-level redirects (such as <code>https://host</code> → <code>https://www.host</code>) are cached per process and in <code>/share/borigins</code>; the first visit still follows them, later visits go straight to the canonical origin. Remote stylesheets are cached under <code>/share/bcss-*</code> for the current boot, so a reload or a fresh browser launch skips the CSS downloads.</li>
 			</ul>
 		<p>

+ 5 - 2
net.c

@@ -426,7 +426,7 @@ static unsigned int arp_build_request(const unsigned char *my_mac,
 /* ---- ARP cache (shared by the stack pump and the raw poll path) ---- */
 
 #define ARP_CACHE_MAX    8
-#define ARP_CACHE_TTL_MS 60000
+#define ARP_CACHE_TTL_MS 300000
 
 struct arp_entry {
 	unsigned char ip[4];
@@ -501,6 +501,9 @@ static int arp_cache_lookup(const unsigned char *ip, unsigned char mac[6])
 			arp_cache[i].used = 0;
 			return 0;
 		}
+		/* An entry that is still being used must not expire during a
+		 * long page load; slide its age on every hit. */
+		arp_cache[i].age_ms = now;
 		for (j = 0; j < 6; j++)
 			mac[j] = arp_cache[i].mac[j];
 		return 1;
@@ -576,7 +579,7 @@ int net_arp_resolve(const unsigned char *dst_ip, unsigned char mac[6])
 					}
 				}
 			} else {
-				net_pump(8);
+				net_pump_burst(64);
 				if (arp_cache_lookup(dst_ip, mac)) {
 					net_stack_leave();
 					return 0;

+ 12 - 0
netcore.h

@@ -18,6 +18,18 @@
  * frames consumed.  No-op while a packet-level command owns the NIC. */
 int net_pump(unsigned int budget);
 
+/* Make several pump passes before a caller sleeps.  A driver returns -1 both
+ * for an empty ring and after an overflow reset, and one pass stops at the
+ * first -1; repeated passes drain the ring and let the link recover. */
+static inline int net_pump_burst(unsigned int budget)
+{
+	int n = 0, i;
+
+	for (i = 0; i < 4; i++)
+		n += net_pump(budget);
+	return n;
+}
+
 /* Save/disable interrupts, then enable them.  Paired with net_irq_exit().
  * Used around driver calls in the cooperative stack paths so a stuck TX
  * descriptor can still time out on the PIT. */

+ 3 - 3
tcp.c

@@ -540,7 +540,7 @@ int tcp_client_connect(unsigned int pid, const unsigned char mac[6],
 		}
 
 		while ((int)(get_ticks_ms() - deadline) < 0) {
-			net_pump(8);
+			net_pump_burst(64);
 			if (c->state == TCP_STATE_ESTABLISHED)
 				return 0;
 			if (c->state == TCP_STATE_CLOSED) {
@@ -602,7 +602,7 @@ int tcp_client_send(unsigned int pid, const void *buf, unsigned int len)
 		}
 
 		while ((int)(get_ticks_ms() - deadline) < 0) {
-			net_pump(8);
+			net_pump_burst(64);
 			if (c->state == TCP_STATE_CLOSED)
 				return cl->err ? cl->err : -2;
 			if ((int)(c->snd_una - (seq + len)) >= 0)
@@ -647,7 +647,7 @@ int tcp_client_recv(unsigned int pid, void *vbuf, unsigned int max,
 			return cl->err ? cl->err : -2;
 		if ((int)(get_ticks_ms() - deadline_ms) >= 0)
 			return -1;
-		net_pump(8);
+		net_pump_burst(64);
 		if (!rxq_len(c) && c->state != TCP_STATE_CLOSED)
 			process_sleep_ms(1);
 	}