Effect HttpApiMiddleware Bug

Found a weird bug where HttpApiMiddleware closures from previous test runs execute instead of newly created ones, despite layers being properly recreated.

The Issue:
- Test 1: Creates middleware with random ID variable 0.123 + DB path /uuid1
- Test 2: Creates NEW middleware with ID variable 0.456 + DB path /uuid2
- When Test 2 runs: Executes OLD middleware (0.123) with OLD DB (/uuid1) ❌

Evidence from logs:
# Test 1
Creating middleware 0.41260 with DB .../uuid1

# Test 2  
Creating middleware 0.16476 with DB .../uuid2

# Test 2 execution (WRONG!)
UU 0.41260 with DB .../uuid1  # Old closure!


export class Authorization extends HttpApiMiddleware.Tag<Authorization>()(
    "Authorization",
    {//...},
) {}

export const AuthorizationLive = Layer.effect(
  Authorization,
  Effect.gen(function* () {
    const middlewareId = Math.random(); // Unique id for debugging
    const {
      makeQuery,
      db,
      schema: { userTable, sessionTable },
    } = yield* DatabaseService;
    
    yield* Effect.log(`Creating middleware ${middlewareId} with DB ${db.$client.filename}`);

    return {
      cookie: (token) => {
        return Effect.gen(function* () {
          // BUG: This logs the id from previous test
          console.log(`Executing middleware ${middlewareId} with DB ${db.$client.filename}`);
          
          const sessionId = ...
            db.select().from(sessionTable).where(eq(sessionTable.id, sessionId))
          );
          // ...
        });
      },
    };
  }),
);


Issue: https://github.com/Effect-TS/effect/issues/4960
Repo: https://github.com/your-username/effect-bug-repro
Steps: git clone bun install bun test

Each test creates fresh:
- ✅ DatabaseService (unique file paths)
- ✅ AuthorizationLive middleware (logs confirm)
- ✅ HTTP server layers
- ❌ But old middleware closures still execute
GitHub
What version of Effect is running? @effect/platform: ^0.82.7 | @effect/platform-bun: ^0.65.4 | effect: ^3.15.4 What steps can reproduce the bug? Explain the bug and provide a code snippet that can ...
HttpApiMiddleware closures from previous layer instances are execut...
Was this page helpful?