Преглед на файлове

Drain the fetch helpers' log tty so cross-origin loads cannot hang

The curl __serve helpers write their DNS/TLS log lines to the private tty
the browser creates with u_tty_create(), but nothing ever read it.  A full
4 KB tty ring blocks its writers while the owner is alive, so after a page
that opened a dozen or so TLS connections (tagesschau.de) every helper hung
in the next log line of a fresh connection, and the following cross-origin
navigation (cloudron.io) sat at "Loading..." until it timed out.

The browser now drains and discards that tty from every fetch poll loop and
the idle main loop.  tty_write also drops output instead of sleeping when
the ring's owner writes to its own full ring (the browser's [js] lines go
there too), since only the owner can drain it.  The docs now describe the
helpers staying warm across origins, which the previous commit introduced.
Johannes Zellner преди 1 седмица
родител
ревизия
a1e381e370
променени са 4 файла, в които са добавени 42 реда и са изтрити 10 реда
  1. 13 9
      AGENTS.md
  2. 21 0
      browser.c
  3. 1 1
      docs/network.html
  4. 7 0
      tty.c

+ 13 - 9
AGENTS.md

@@ -1025,11 +1025,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 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
+  TCP/TLS connection (TLS stays per-process).  Idle helpers stay warm across
+  navigations, so a same-origin load reuses the TCP/TLS connection and a
+  cross-origin job just drops the old one in `uhttp_open`; only busy helpers
+  are killed when the page changes.  The helpers' DNS/TLS log lines (and the
+  browser's own `[js]` lines, since `u_tty_create()` rebinds its output) go to
+  a private 4 KB tty that nobody renders; the browser drains and discards it
+  from every poll loop (`fetch_tty_drain`), because a full tty ring blocks its
+  writers, and `tty_write` drops rather than sleeps when the ring's owner
+  writes to its own full ring.  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
@@ -1869,10 +1873,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 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
+    image starts before the script phase.  Idle helpers stay warm across
+    navigations (a cross-origin job drops the old connection); losing focus
+    or ten idle seconds stops them, so their sockets do not block raw
+    commands.  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

+ 21 - 0
browser.c

@@ -1971,6 +1971,7 @@ struct fetch_worker {
 
 static struct fetch_worker fetch_workers[FETCH_WORKERS];
 static int fetch_tty = -2;
+static void fetch_tty_drain(void);
 static unsigned int fetch_session;
 static unsigned int fetch_seq;
 static struct fetch_meta fetch_meta;
@@ -2139,6 +2140,7 @@ static void fetch_workers_stop_if_unfocused(void)
 {
 	int i;
 
+	fetch_tty_drain();
 	if (ui.focused || page_loading || preload_active)
 		return;
 	for (i = 0; i < FETCH_WORKERS; i++)
@@ -2148,6 +2150,24 @@ static void fetch_workers_stop_if_unfocused(void)
 		}
 }
 
+/* The helpers (and this process, since u_tty_create() rebinds our own
+ * output) write their DNS/TLS/[js] log lines to fetch_tty.  Nobody renders
+ * it, and a full 4 KB tty ring blocks its writers: after a page that opened
+ * a dozen TLS connections every helper hung in its next log line, so the
+ * following cross-origin navigation sat at "Loading..." until it timed out.
+ * Drain and discard it from every wait/poll loop. */
+static void fetch_tty_drain(void)
+{
+	char sink[512];
+	int i;
+
+	if (fetch_tty <= 0)
+		return;
+	for (i = 0; i < 16; i++)
+		if (u_tty_read(fetch_tty, sink, sizeof(sink)) <= 0)
+			break;
+}
+
 static int fetch_worker_start(struct fetch_worker *w, char *err,
 			      unsigned int errcap)
 {
@@ -2289,6 +2309,7 @@ static int fetch_worker_poll(struct fetch_worker *w, char *err,
 	char *p;
 	int rn, mseq;
 
+	fetch_tty_drain();
 	if (!w->busy)
 		return -1;
 	if (w->ready)

+ 1 - 1
docs/network.html

@@ -396,7 +396,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; 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>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 (a cross-origin job just drops the old connection, and only busy helpers are killed), 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 helpers' DNS/TLS log lines go to a private tty that the browser drains and discards from every poll loop; a full tty ring blocks its writers, so an undrained one used to hang every helper after about sixteen TLS connections.  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>

+ 7 - 0
tty.c

@@ -115,8 +115,15 @@ int tty_write(int id, const char *buf, unsigned int len)
 		unsigned int next = (t->head + 1) % TTY_BUF;
 
 		if (next == t->tail) {
+			struct process *cur = current_proc();
+
 			if (!process_is_alive(t->owner))
 				return -1;
+			/* The owner is the only reader: sleeping on its own
+			 * full ring can never make progress.  Drop instead
+			 * (the browser rebinds itself to its helpers' tty). */
+			if (cur && cur->pid == t->owner)
+				return -1;
 			process_sleep_ms(1);
 			continue;
 		}