feat(net,gui,sched): async HTTP client + Web browser app, and the scheduler fix that makes background work possible

The visible goal was a Web app: type a URL, see the page, fetched over the
TCP stack SUB-OS built. Getting there surfaced -- and forced fixes to --
two deeper problems.

Async HTTP client (net/http_client.c)
  tcp_connect/recv all block, so fetching from the GUI would freeze the
  compositor. Fetches now run on one long-lived kernel worker thread that
  services a single-slot mailbox; the requester posts a job and polls its
  state. Ownership follows the state, and the two hand-off points (worker
  completion, requester release) run under interrupts-off, so a window can
  close mid-fetch with neither leak nor use-after-free. do_fetch returns
  its terminal state rather than publishing it, closing a window where the
  requester could free the job before finalize read it.

Cooperative scheduling for the idle thread (the core fix)
  The login prompt, the shell and the GUI compositor all run on the pid-0
  idle thread, which the scheduler by design never preempts (sched_tick
  skips pid 0) and never requeues on a yield (weave_on clear). So it could
  neither be preempted to let the worker run, nor cleanly yield -- the
  worker was simply starved, and an early attempt to yield from it froze
  the shell outright. The idle thread now joins the weave rotation once
  hardware interrupts are live, and the network wait loops (tcp, dns, ping,
  the worker's idle) yield through a new net_wait() instead of halting the
  CPU. The compositor spends its frame-budget slack yielding too. Result:
  a background fetch progresses while the desktop holds 54 FPS.

Web app (gui/gui_app_web.c)
  A text browser: URL bar, Go, and a Raw/Reader toggle. Raw shows the
  response verbatim; Reader strips markup (dropping <script>/<style>
  bodies), decodes the common entities, and word-wraps the body. The line
  index is cached against content/view/width so scrolling and idle
  repaints are free. It only ever polls the worker, so it never stalls.

TCP throughput (the limit flagged last time)
  The 32-slot table with a 2 s TIME_WAIT linger capped sustained serving
  at ~16 req/s. The table is now 64 slots, a connection this end closed
  after serving a request lingers 250 ms rather than 2 s, and expired
  TIME_WAIT entries are reaped from the count path so a quiet table still
  drains. netstat/netmon gained served-since-boot and peak-slots counters.

Also: a `webget` CLI (the async client from a shell), listen/accept
introspection already present, and a globe icon for the app.

Verified on x86_64: `webget` fetched an 812-byte page (HTTP 200, 10 ms) on
the worker thread; the GUI Web app rendered the same page in Reader mode
while the desktop held 54 FPS; an 80-request burst through the 64-slot
table returned 80/80 (was 39/40 before) and netstat reported "80 served,
peak 33 of 64 slots" -- a peak that would have overflowed the old table.
Builds clean on x86_64, aarch64 and armv8i.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Fh5z6V7nGPbHvqVG5p4CXX
This commit is contained in:
subhobhai943andClaude Opus 4.8 committed 2026-08-27 02:21:48 +05:30
1 parent eb92502deb
commit dd4922767a
16 files changed
+898 -22

No files matched your search

+1
View File
@@ -16,5 +16,6 @@ void gui_app_regedit_launch(int x, int y, int w, int h);
void gui_app_analytics_launch(int x, int y, int w, int h);
void gui_app_sorry_launch(int x, int y, int w, int h);
void gui_app_netmon_launch(int x, int y, int w, int h);
void gui_app_web_launch(int x, int y, int w, int h);
#endif // _GUI_APPS_H
+1
View File
@@ -27,6 +27,7 @@ typedef enum {
GUI_ICON_WARNING,
GUI_ICON_HEART,
GUI_ICON_NETWORK,
GUI_ICON_GLOBE,
GUI_ICON_COUNT
} gui_icon_id_t;
+65
View File
@@ -0,0 +1,65 @@
#ifndef _NET_HTTP_CLIENT_H
#define _NET_HTTP_CLIENT_H
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
// Asynchronous HTTP/1.0 client.
//
// The TCP client calls (tcp_connect/tcp_send/tcp_recv) all busy-wait against
// the timer tick, so calling them from an interactive context -- the GUI
// compositor above all -- would freeze it for the length of the fetch. This
// module runs every fetch on one long-lived kernel worker thread instead: the
// requester posts a job and polls its state, and the compositor keeps painting
// at full rate while the worker blocks.
//
// Ownership follows the state. While a job is HTTP_STATE_RUNNING the worker
// owns it; once it is DONE or ERROR the requester owns it again. The requester
// hands a job back with http_fetch_release, which frees it now if the fetch has
// finished, or marks it abandoned so the worker frees it when it does -- so a
// window can close mid-fetch without leaking or use-after-free.
typedef enum {
HTTP_STATE_RUNNING = 0, // worker owns the job
HTTP_STATE_DONE, // completed; buf holds the response
HTTP_STATE_ERROR // failed; err holds a short reason
} http_fetch_state_t;
typedef struct http_fetch {
// Request, filled in by http_fetch_start from the parsed URL.
char host[128];
char path[256];
uint16_t port;
// Result. `state` is the only field the requester may read while the fetch
// is in flight; everything else is valid once state != RUNNING.
volatile int state; // http_fetch_state_t
volatile bool abandoned; // requester released it mid-flight; worker frees
char* buf; // response bytes (status line, headers, body)
int len; // bytes stored
int cap; // buffer capacity
uint32_t ip; // resolved peer address
int status_code; // parsed HTTP status, 0 if not found
uint32_t elapsed_ms; // wall time of the fetch
char err[64]; // human-readable failure reason
} http_fetch_t;
// Start the worker thread. Call once at boot, after the TCP stack is up.
void http_client_init(void);
// Begin fetching `url` (forms: "host", "host/path", "host:port/path", with an
// optional "http://" prefix). `cap` bounds the stored response. Returns a job
// to poll, or NULL if the URL is unusable or a fetch is already in flight.
http_fetch_t* http_fetch_start(const char* url, int cap);
// Give a job back. Frees it if the fetch has finished; otherwise marks it so
// the worker frees it on completion. The pointer must not be used afterwards.
void http_fetch_release(http_fetch_t* f);
// True while the worker thread is busy with a fetch.
bool http_client_busy(void);
#endif // _NET_HTTP_CLIENT_H
+9
View File
@@ -103,6 +103,15 @@ arp_entry_t* net_get_arp_table(int* count_out);
uint32_t ip_parse(const char* ip_str);
void ip_to_str(uint32_t ip, char* buf);
// Yield the CPU once while blocked in a network wait loop (a handshake, a
// receive, a ping reply). Inbound packets are delivered from the NIC interrupt
// regardless of which task runs, so cooperatively handing the CPU to other
// tasks -- the GUI compositor above all -- keeps the desktop live during a
// fetch instead of freezing it for the duration. When the caller is the only
// runnable task it returns at once, so a shell-context wait still just spins to
// its timeout.
void net_wait(void);
// One's-complement checksum, summed a byte at a time so the loads cannot be
// reordered around a caller's store into the buffer being summed, and so no
// alignment is assumed of a packed header. Words are read big-endian;
+9
View File
@@ -139,4 +139,13 @@ size_t tcp_get_connections_count(void);
const tcp_conn_t* tcp_get_connection(size_t index);
const char* tcp_state_name(uint8_t state);
// Size of the connection table, so callers iterate the right range rather than
// a hardcoded constant.
int tcp_conn_table_size(void);
// Cumulative count of connections that have reached ESTABLISHED, and the
// high-water mark of simultaneously-live table entries. Diagnostic.
uint64_t tcp_get_total_opened(void);
int tcp_get_peak_connections(void);
#endif // _NET_TCP_H