If a browser disconnects halfway through an AI response, decide which work belongs to that connection. A preview may be disposable. A queued report that the user will retrieve later may still need to finish. Sending an abort signal to a preview saves work only in components that actually honor that signal.
Separate the observations
A Node HTTP server can observe a response closing. That event occurs after a normal completion as well as when the underlying connection closes early. On the server response, writableEnded indicates that the application called end(); writableFinished indicates that the outgoing data has been flushed to the underlying system. Neither proves that the browser received or displayed every byte. A proxy, rather than the browser, may be the server's direct peer. Node HTTP response properties
In a local Node 24.13.1 fixture, a normally ended response reported writableFinished === true in its close listener. Destroying the loopback client after the first chunk of an unfinished response gave false. That observation concerns the server and its immediate client only. The fixture made no provider requests and measured no token charges.
Steps
- For connection-bound previews, give each request its own cancellation signal. Attach the early-close listener before starting upstream work. Record normal finishes separately.
- Pass the signal through each wrapper into a model SDK or transport that documents support for cancellation. If the SDK ignores the signal, simply awaiting its promise does not stop waiting or cancel the remote work.
- Decide when a durable write may commit. Checking a cancellation flag immediately before a write can reduce unwanted work, but a disconnect may still race with the commit. Use a separate operation identity and explicit commit policy for persistent results.
- Release timers, stream readers and concurrency slots on every exit path. Record early-close and upstream-cancel observations separately. A local disconnect alone cannot establish how a provider bills the request.
Test with a controlled upstream
Make a stub that waits until told to finish and reports whether it received a signal. Start a preview, wait for the first chunk, close the client, and observe the handler and stub. Run a second case that finishes normally. Add a third in which a durable write commits before the connection closes; its record should follow the stated persistence policy. Inspect active requests once cleanup completes, rather than requiring process memory to remain exactly flat.
This article was created with AI assistance.
Top comments (0)