oakserver / oak

A middleware framework for handling HTTP with Deno, Node, Bun and Cloudflare Workers 🐿️ 🦕
https://oakserver.org
MIT License
5.09k stars 231 forks source link

Http - connection closed before message completed #622

Closed AW0005 closed 7 months ago

AW0005 commented 10 months ago

The full error:

[uncaught application error]: Http - connection closed before message completed

request: { url: "http://localhost:8000/", method: "GET", hasBody: false }
response: { status: 200, type: "text/plain", hasBody: true, writable: false }

    at Object.respondWith (ext:deno_http/01_http.js:286:19)
    at eventLoopTick (ext:core/01_core.js:197:13)

And a minimal reproducible setup (on deno v1.38.0):

import { Application, Router } from "https://deno.land/x/oak@v12.6.1/mod.ts";
import { Timeout } from "https://deno.land/x/timeout@2.4/mod.ts"

const app = new Application();
const router = new Router();

router
  .get("/", async (ctx) => {
    await Timeout.wait(200);
    const html = "Some Text to Display"
    ctx.response.body = html;
    return ctx.response;
  })

app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 8000 });

If a request response takes any time at all, and you aggressively refresh the page, you'll get the connection error. In my actual use case I have some calls that take some time to return so it is completely plausible the user navigates away in the meantime, and it generates this error sometimes in the log. Is there anyway I can get rid of this error myself or is it something that needs to be fixed on Oak's side or is this something in deno and I should be reporting it over there? Any help appreciated.

Tangentially I have also experienced a similar "Http - connection error: broken pipe: broken pipe" and "BadResource - Bad resource ID" that I suspect comes from a similar situation but I have not been able to reproduce that as easily as this one.

kitsonk commented 7 months ago

This is working as designed. The remote end closed before the entire response was sent. Deno throws the error, and if your application needed to clean up some resources or invalidate something in the server because a response was not fully sent you could do so, otherwise it can be ignored. If you don't want any anything to print out, you need to implement your own error listener on the application and decide how you will respond to these.