Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

From Discord: Unexpected Behavior in HTTP Middleware: Unhandled Errors Cause Request Hang #3391

Closed
effect-bot opened this issue Jul 31, 2024 · 3 comments · Fixed by #3408
Closed

Comments

@effect-bot
Copy link

Summary

The conversation revolves around an issue with HTTP middleware in the Effect-TS ecosystem, where unhandled errors cause the request to hang without any compiler warnings or default server responses. The user provided a reproducible example, and another user, datner_, identified the problem in the HttpApp.toHandled function, specifically in how it handles middleware failures. Datner_ suggested moving the middleware to HttpRouter.use instead of HttpServer.serve as a workaround. Key takeaways include:

  1. Unhandled errors in middleware can cause requests to hang.
  2. The compiler does not currently warn about unhandled errors in this context.
  3. A potential solution is to move middleware to HttpRouter.use.
  4. The issue was identified in the HttpApp.toHandled function"s error handling logic.

Discord thread

https://discord.com/channels/795981131316985866/1268169538047049729

@datner
Copy link
Member

datner commented Jul 31, 2024

recreation

import { HttpRouter, HttpServer, HttpServerResponse } from "@effect/platform";
import { BunHttpServer, BunRuntime } from "@effect/platform-bun";
import { Console, Effect, Layer } from "effect";

const Server = HttpRouter.empty.pipe(
  HttpRouter.all("*", HttpServerResponse.empty()),
  HttpRouter.use(() => Effect.fail("BOOM!")), // fine
  HttpServer.serve(() => Effect.fail("KAPAW!")), // drops the request
  Layer.provide(BunHttpServer.layer({ port: 3000 })),
);

Layer.launch(Server).pipe(
  Effect.catchAllCause(Console.error),
  BunRuntime.runMain,
);

The key takeaways for real are

  1. Errors in serve middleware never effectively skip the request processing, dropping the request entirely. I think thats what happens
  2. The compiler does not warn of this, but this is expected, the behavior is the one that is wrong
  3. bot is right
  4. Identified by me, so double check please

@datner
Copy link
Member

datner commented Jul 31, 2024

playground by user (not sure it"s useful since it"s a server)
https://effect.website/play#9da574e1b33a

@colelawrence
Copy link

Possibly related, but I get server hangs from the following as well:

import {
  HttpApp,
  HttpMiddleware,
  HttpRouter,
  HttpServerResponse,
} from "@effect/platform";
import { Effect, flow, pipe } from "effect";

export async function brokenFetchHandler(request: Request): Promise<Response> {
  console.log("enter broken fetch handler", { request });
  const myBrokenMiddleware = <E, R>(
    httpApp: HttpApp.Default<E, R>,
  ): HttpApp.Default<E, R> =>
    Effect.succeed(HttpServerResponse.empty({ status: 208 }));
  return pipe(
    request,
    HttpApp.toWebHandler(
      HttpRouter.empty,
      flow(myBrokenMiddleware, HttpMiddleware.logger),
    ),
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants