28 Adaptive HTTP responses and worker lifetime🔗ℹ

With #:sse? #f, make-http-handler and serve-http prefer JSON for requests that finish within #:stream-after seconds (default 0.05). If that window expires, or a request emits a notification, the response becomes a request-scoped SSE stream. The same worker and event queue continue execution; the handler is not restarted. Explicit #:sse? #t, progress requests and subscriptions continue to select SSE immediately. #:stream-after must be a finite nonnegative number; #:sse? #f is not a promise to return only JSON. MCP clients must support both response types.

An initial SSE comment flushes headers promptly. Later keep-alive comments expose stream closure during otherwise quiet computation. Detection latency depends on the configured keep-alive interval and network buffering. Once stream delivery fails, cleanup signals cooperative cancellation, waits up to 200 milliseconds, and shuts down the request custodian. A TCP input half-close is not treated as cancellation: the client may still be waiting to receive the response.

serve-http associates request workers with the web server’s connection custodian, which differs from its longer-lived servlet custodian. A delivery scope also cleans up requests if response headers fail before the body writer runs. Runtime-owned task workers retain their independent ownership. Integrators using make-http-handler directly must supply an appropriate enclosing custodian and response-delivery lifecycle; serve-http installs that adapter.

Quick JSON results use the same encoded bytes that passed the response-size limit. Both paths apply the configured size limit to each encoded JSON-RPC message. This change satisfies the previously recorded disconnect probe through adaptive SSE; it does not interpret socket input EOF as proof that a client cannot receive JSON, or introduce informational HTTP heartbeat responses.

When serve-http selects an ephemeral port with #:port 0, its default Origin policy uses the configured host and the actual listener port obtained from the connection. It does not derive authority from the request’s Host header. An explicit #:origins list replaces this default, including for ephemeral listeners.